INHERITANCE

                               inheritance in java

What is Inheritance in Java?

1
2
3
4
public  class Vehicle {}
public  class FourWheeler extends Vehicle {}
public  class Car extends FourWheeler{}
public  class BMW extends Car{}
 
Inheritance in Java is an Object oriented or OOPS concepts, which is same as real world Inheritance behavior,
Inheritance allows code reuse in Java. Along with Abstraction, Polymorphism and Encapsulation, Inheritance forms basis of Object oriented programming.
Inheritance is implemented using extends keyword in Java and When one Class extends another Class it inherit all non private members including fields and methods.
Parent class called as Super class and child class as Subclass in Java programming language.
The class which extends another class becomes Child of the class it extends(Parent Class) and inherit all its behavior which is not private.
There are lot of example of Inheritance in Object oriented world e.g. Dog inherit properties of Animal, Car inherit properties of Vehicle etc.
In Java programming language, Child class inherit its Parent class's property and behavior like real world where child inherit the property of parent.
 
When to use Inheritance in Java
If the objects pass the 'IS-A' relationship test, Inheritance is the natural choice.
In Java collection framework most of concrete collection classes inherit from there Abstract counterpart e.g. HashSet extends AbstractSet, LinkedHashSet extends HashSet, ArrayList extends AbstractList etc.
Whether to use Inheritance or not is to check 'IS-A' relationship, if objects pass 'IS-A' test then those objects are good candidate for inheritance.
For example all above example of Inheritance satisfy IS-A rule e.g. HashSet IS-A Set.
Similarly if you have class called Car and want to create another class called BMW,its best to use inheritance and BMW should extend Car because BMW is a car.
By extending Car class it gets common state and behavior of car object.
Similarly if you find HAS-A relationship between two classes than use Composition e.g. Car HAS-A Seat, So Car class should be composed with a Seat and Seat should not extend Car.
In other situation also you can use Inheritance if you are creating a class which adds more feature into existing class, you can extend it to reuse all of its code.
 
How to use Inheritance in Java
You can use Inheritance in Java by using two keywords, extends and implements.
extends keyword is used when one Class inherit from other Class or an interface extend another interface.
implements is used when one class implements an interface.
 
eg:1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class B
        int  i;class C extends B
    {
        int  j;
     }
class Inheritance1
     {
        public  static void main(String[] args)
            {
              B  b1 = new B();
              System.out.println(b1.i);
              C  c1 = new C();
              System.out.println(c1.i);
              System.out.println(c1.j);
                b1.i=10;
              c1.i=20;
              c1.j=30;
              System.out.println(c1.i);
              System.out.println(c1.j);
              System.out.println(b1.i);
            }
    }

O/P:

inheritance

eg:2

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
package in.java4all.inheritance;
class  D 
{
    void test1()
        {
            System.out.println("test1");
        }
}
class  E extends D
{
    void test2()
            System.out.println("test2");
        }
}
class  Inheritance2 
{
    public static void main(String[]  args)  
        {
            D d1 = new D();
            d1.test1();
            System.out.println("----------");
            E e1 =new E();
            e1.test1();
            e1.test2();
        }
}

O/P:

inheritance

eg:3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package in.java4all.inheritance;
class Vehicle {}
class FourWheeler extends Vehicle {}
class Car extends FourWheeler{}
public class BMW extends Car{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FourWheeler fourwheeler =new FourWheeler();
        Car car=new Car();
        BMW bmw=new BMW();
        System.out.println(car instanceof FourWheeler);
        System.out.println(bmw instanceof BMW);
    }
}

O/P:

1
2
3
4
5
6
7
8
9
package in.java4all.inheritance;
class SuperClass{
}
public class SubClass extends SuperClass{
    public static void main(String[] args) {
        SuperClass su=new SubClass();
        SubClass sub=(SubClass) new SuperClass();
    }
}