Inheritance

Inheritance
  • Inheritance allows a class to use the properties and methods of another class. 
  • In other words, the derived class inherits the states and behaviors from the base class. 
  • The derived class is called subclass and the base class is known as super-class.
  • The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.
  • Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.

Inheritance in Java

Syntax:

class Subclass-name extends Super class-name  
{  
       //methods and fields  
}  

The extends keyword indicates that you are making a new class that derives from an existing class.

Example:
1
2
3
4
5
6
public class ParentClass{

 public void parent_Method(){
  System.out.println("Parent method");
 }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class ChildClass extends ParentClass {

 public void child_Method(){
  System.out.println("Child method");
 }

 public static void main(String[] args){
  ChildClass c = new ChildClass();
  c.child_Method(); // method of Child class
  c.parent_Method(); // method of Parent class
 }
}

Aggregation in Java
Aggregation is a special form of association. It is a relationship between two classes, however its a directional association, which means it is strictly a one way association. It represents a HAS-A relationship.

For example consider two classes Student class and Address class. Every student has an address so the relationship between student and address is a Has-A relationship. But if you consider its vice versa then it would not make any sense as an Address doesn’t need to have a Student necessarily. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Address
{
   int streetNum;
   String city;
   String state;
   String country;
   Address(int street, String c, String st, String coun)
   {
       this.streetNum=street;
       this.city =c;
       this.state = st;
       this.country = coun;
   }
}
class StudentClass
{
   int rollNum;
   String studentName;
   //Creating HAS-A relationship with Address class
   Address studentAddr; 
   StudentClass(int roll, String name, Address addr){
       this.rollNum=roll;
       this.studentName=name;
       this.studentAddr = addr;
   }
   public static void main(String args[]){
       Address ad = new Address(55, "Agra", "UP", "India");
       StudentClass obj = new StudentClass(123, "Chaitanya", ad);
       System.out.println(obj.rollNum);
       System.out.println(obj.studentName);
       System.out.println(obj.studentAddr.streetNum);
       System.out.println(obj.studentAddr.city);
       System.out.println(obj.studentAddr.state);
       System.out.println(obj.studentAddr.country);
   }
}
Output:

123
Chaitanya
55
Agra
UP
India

The above example shows the Aggregation between Student and Address classes. You can see that in Student class I have declared a property of type Address to obtain student address. Its a typical example of Aggregation in Java.

Why we need Aggregation?
To maintain code re-usability. To understand this lets take the same example again. Suppose there are two other classes College and Staff along with above two classes Student and Address. In order to maintain Student’s address, College Address and Staff’s address we don’t need to use the same code again and again. We just have to use the reference of Address class while defining each of these classes like:

Student Has-A Address (Has-a relationship between student and address)
College Has-A Address (Has-a relationship between college and address)
Staff Has-A Address (Has-a relationship between staff and address)

Association in Java. 

Association establishes relationship between two separate classes through their objects. The relationship can be one to one, One to many, many to one and many to many.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class CarClass{
   String carName;
   int carId;
   CarClass(String name, int id)
   {
 this.carName = name;
 this.carId = id;
   }
}
class Driver extends CarClass{
   String driverName;
   Driver(String name, String cname, int cid){
 super(cname, cid);
 this.driverName=name;
   }
}
class TransportCompany{
   public static void main(String args[])
   {
 Driver obj = new Driver("Andy", "Ford", 9988);
 System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
   }
}


In the above example, there is a one to one relationship(Association) between two classes: CarClass and Driver. Both the classes represent two separate entities.



Composition 
It is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. 

<-- Previous || Next -->

1 comment: