Search

Java - Inheritance

In simple words, Inheritance is way to define new a class, using classes which have already been defined.

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.
 An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then morespecific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or super class or base class) and the specific classes as children (or sub classes or derived classes).
The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process.

A subclass inherits all instance variables and methods from its super class and also has its own variables and methods.
One can inherit the class using keyword extends.
Syntax:

class subclass-name extends super class-name
{
            // body of class.
}


Why Inheritance:
-          When a “Is-A” relationship exists between two classes we use Inheritance
-          The parent class is termed super class and the inherited class is the sub class
-          The keyword extends is used by the sub class to inherit the features of super class
-          Inheritance is important since it leads to reusability of code

IS-A relationship:
"IS-A" relationship describes inheritance relationship between objects. If you can talk something with word "is". They can be described with keyword extends in coding. "is-a" relationship is also called classification.

Example:

public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}

public class Dog extends Mammal{
}



Now, based on the above example, In Object Oriented terms, the following are true:
-          Animal is the superclass of Mammal class.
-          Animal is the superclass of Reptile class.
-          Mammal and Reptile are subclasses of Animal class.
-          Dog is the subclass of both Mammal and Animal classes.
Now, if we consider the IS-A relationship, we can say:
-          Mammal IS-A Animal
-          Reptile IS-A Animal
-          Dog IS-A Mammal
-          Hence : Dog IS-A Animal as well
Example:

public 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 Mammal);
      System.out.println(d instanceof Animal);
   }
}

Output:
true
true
true

Has-A relationship:
HAS-A relationships are based on usage, rather than inheritance. In other words class Test HAS-A HasATest if class Test has a reference to an instance of class HasATest.

Example:

public class Animal{}
public class Horse{
 private Animal animalClass;
}



In preceding code Horse HAS-A Animal. In other words, Horse has a reference to a Animal.

What is not possible using java class Inheritance?
-          Private members of the super class are not inherited by the subclass and can only be indirectly accessed.
-          Members that have default accessibility in the super class are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the super class.
-          Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.
-          A subclass can extend only one super class
-          Java does not support multiple inheritance.


Instanceof:
Basically, you check if an object is an instance of a specific class. You normally use it, when you have a reference parameter to an object that is of a super class of interface type and need to know the actual object has some other type.

Example:

public class Animal{}

public class Main{
 public static void main(String args[]){
  Animal animal = new Animal();
  String str = new String();
  if(animal instanceof Animal){
   System.out.println(?animal is a instance of Animal class?);
  }

  if(animal instanceof Object){
   System.out.println(?animal is a instance of Object class?);
   // animal is a instace of Object class because Object is super class for all type of class
  }

  if(str instanceof String){
   System.out.println(?str is a instace of String class?);
  }
}
}


}

this and super keyword:
Sometimes a method will need to refer to the object that invoked it. “this” keyword can be used inside any method refer to the current object. You can also use “this” keyword for access current class variable and method.

Example:

public class Box{
 double width;
 double height;
 public Box(double width, double height){
  this.width = width;
  this.height = height;
 }
}



super() keyword use to call parent constructor with no arguments. It can be used also with arguments. i.e. super(argument 1). Also it can be used to call methods from the parent class.
e.g super.testMethod();

Note: this and super keyword both are not use together because both keywords are the first statement of constructor or method.


Why does not support multiple inheritance?
I want to share a definition for java given by James Gosling.
“Java a simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.”

In order to enforce simplicity should be the main reason for omitting multiple inheritance.

No comments:

Post a Comment