Pages

Saturday 23 February 2013

INHERITANCE IN JAVA

INHERITANCE IN JAVA

Inheritance  can be defined as a process where an object acquires a property of  another .Different objects often have   a certain amount in common with each other . for example mountain bike , road bike and motor-bike all share the common characteristics of a bicycle ( current speed,current pedal  cadence,current gear) , yet each one of them have some of there own additional features which makes them different . Thus inheritance in Java is an object oriented or OOPS concept which helps us to mimic the real world inheritance behavior  .




Inheritance in java is a way to way to define relationship between two java classes . Inheritance in java defines parent and child relations hip between two java classes . This is similar to the real word scenario of a child acquiring the traits of his or her parents. In java Super Class represents parent and the Sub class is used to represent the child . 
When we talk about Inheritance in java then we use the term extends and implements.  By using these terms we can make an object acquire a property of another .

class MountainBike extends Bicycle {
    // new fields and methods defining 
    // a mountain bike would go here
}

IS-A Relationship:

Using this relationship we can say that this this object is a type of that object.

IS-A Relationship using extends keyword.
public class Animal{}
public class Mammal extends Animal{}
public class Reptile extends Animal{}
public class Dog extends Mammal{}
Now we can say that:-
  • Animal is a super class of Mammal Class.
  • Animal is a super class of Reptile Class.
  • Mammal and reptile are the subclass of Animal class.
  • Dog is a subclass of both Mammal class and Animal Class.Now If we consider is a relationship we can say that,
and
  • Mammal IS-A Animal.
  • Reptile IS-A Animal.
  • Dog IS-A Mammal.
  • Hence: Dog IS-A Animal as well.With use of the extends keyword a subclass will be able to inherit all the properties of the superclass excluding the private properties of the superclass.
Example:

pubic class Dog extends Mammal{
public static void main(String args[]){
  Animal a = new Animal();
  Mammal m = new Mammal();
          Dog d = new Dog();

          System.out.println(m instanceof Animal);
          System.out.println(d instanceof Mannal);
          System.out.println(d instanceof Animal);
}
}
The output would be the following:-
true
true
true

IS-A Relationship using implements keyword.

The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended by the classes
Declaring an interface:-

public interface Animal {}

Now we implement the interface

public class Mammal implements Animal{}
public class Dog extends Mammal{}

Let us now check that mammal is actually an animal and dog is actually an animal.
class Mammal implements Animal{}
public class Dog extends Mammal{
       public static void main(String args[]){
           Mammal m = new Mammal();
           Dog d = new Dog();
           System.out.println(m instanceof Animal);
           System.out.println(d instanceof Mammal);
           System.out.println(d instanceof Animal);
   }
}
If we run this code the output would be :-
true
true
true

HAS-A Relationship :-

These relationship are mainly based on the usage . This determines whether a certain class HAS-A certain thing . This helps to reduce the duplication of code and bugs.

Example:

public class Vehicle{}
public class Speed{}
public class Van extends Vehicle{
               private Speed sp;
}


This shows that class Van HAS-A Speed.By having a separate class for speed we do not have to put the entire code that belong to speed inside Van class. This helpsto use the Speed class in multiple programs or applications.

An important point to note is that java does not support multiple inheritance .
multiple inheritance is the ability for a single class to inherit from a multipleclasses. java do not support this capability.
The designers of java considered multiple inheritance to be too complex. They wanted the java to be as simple as possible.

Multiple inheritance can cause  the diamond problem , however  since java does not support multiple inheritance , a java programmer should feel relieved of any such consequences.

A diamond problem can be explained as :

consider we have two classes , Class B and Class C that derives from the same class Class A.We also have class D which derives form Class B and Class C by using multiple inheritance . The problem in having an inheritance hierarchy like this is that when we instantiate an object of class D , any call to method definition in class A will be ambiguous , because its not sure  whether to call the version of the method derived from class B or class C .







Alternatives of Multiple Inheritance in java
Java has interfaces which do allow to mimic multiple inheritance . Although what actually gained from  inheritance through interfaces is somewhat similar to multiple inheritance ,the implementation of those interfaces  is singly  (Not like multiple inheritance). This means the problems like diamond problem in which the compiler get confused as to which method to use will not occur in Java.

In Java multiple inheritance would look like this 

public class Dog implements Wolf, Canine {
/*...*/
}

No comments:

Post a Comment