Inheritance represents the IS-A relationship.
************************ Syntax of Inheritance ***************************
class Subclass-Name extends Superclass-Name
{
//methods and fields
}
The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called super-class. The new class is called subclass.
************************ Simple example of Inheritance ************************

As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
========================== Output =============================
Programmer salary is:40000.0
Bonus of programmer is:10000
In the above example Programmer object can access the field of own class as well as of Employee class.
------------------------------------------------------------------------------------------------------------------
Aggregation represents HAS-A relationship.
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains information such as id, name, email etc. It contains one more object named address, which contains its own information such as city, state, country, zip code etc. as given below.
class Employee{
int id;
String name;
Address address;//Address is a class
...
}
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.
********************** Example of HAS-A relationship *************************

In the following example, Employee has an object of Address, address object contains its own information such as city, state, country etc. In such case relationship is Employee HAS-A address.
========================= Address.java ==========================
public class Address {
String city,state,country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
========================= Emp.java ==========================
public class Emp {
int id;
String name;
Address address;
public Emp(int id, String name,Address address) {
this.id = id;
this.name = name;
this.address=address;
}
void display(){
System.out.println(id+" "+name);
System.out.println(address.city+" "+address.state+" "+address.country);
}
public static void main(String[] args) {
Address address1=new Address("gzb","UP","india");
Address address2=new Address("gno","UP","india");
Emp e=new Emp(111,"varun",address1);
Emp e2=new Emp(112,"arun",address2);
e.display();
e2.display();
}
}