Saturday, October 6, 2012

What is Inheritance in Java and OOPS Tutorial - Example

Inheritance in Java is an Object oriented or  OOPS concepts, which allows to emulate real world Inheritance behavior, Inheritance allows code reuse in Object oriented programming language e.g. 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. Inheritance in Java can be best understand in terms of Parent and Child class, also known as Super class and Sub class in Java programming language. The class which extends another class becomes Child of the class it extends and inherit all its functionality which is not private or package-private given where Child class is created. Inheritance is the most easy way to reuse already written and tested code but not always best way to do so, which we will see in this article. There are lot of example of Inheritance in Object oriented world e.g. Child inherit properties from parent, Dog inherit properties of Animal etc. In this Java and OOPS tutorial we will learn What is Inheritance in Java, How to use Inheritance in Java, Simple Example of Java Inheritance and some important points about Inheritance in Java.


What is Inheritance in Java
What is Inheritance in Java with ExampleInheritance in Java is way to define relationship between two classes. Inheritance defines Parent and Child relationship between two classes. Similar to real world where a Child inherit his parents Surname and other qualities, In Java programming language, Child class inherit its Parent’s property and code. In Java programming, terms Super class and Sub Class is used to represent Parent and Child class, while in other object oriented language like C++, terms base and derived class is used to denote Inheritance. Inheritance is also simplest but not optimal way to reuse code and When one class use Inheritance to extend another class it get access to all non private code written in Super class. In Context of Inheritance in Java, following are legal :

1) Super class reference variable can point to Sub Class Object e.g.

SuperClass parent = new SubClass();

is legal at compile time because of IS-A relationship between Super class and Sub Class. In Java Sub Class IS-A Super class like Mango IS-A Fruit.

Similarly you can pass Sub class object in method arguments where Super class is expected, return Sub Class instance where return type of method is Super Class etc.

On the other hand if an you want to store object of Sub class, which is stored in super class reference variable, back on Sub class reference variable you need to use casting in Java, as shown below :

SubClass child = (SubClass) parent; //since parent variable pointing to SubClass object

This code will throw ClassCastException if parent reference variable doesn't point to a SubClass object.

Important points about Inheritance in Java

let’s see some worth noting points about Inheritance concepts in Java programming language. This tips are very helpful while using Inheritance in Java program.

1) As I said earlier Inheritance in Java is supported using extends and implements keyword, extends keyword is used to inherit from another Java Class and allow to reuse functionality of Parent class. While implements keyword is used to implement Interface in Java. Implementing an interface in Java  doesn't actually meant for code reuse but provides Type hierarchy support. You can also use extends keyword when one interface extends another interface in Java.

2)If you do not want to allow Inheritance for your class than you can make it final.  final classes can not be extended in Java and any attempt to inherit final class will result in compile time error.

2)Constructor in Java are not inherited by Sub Class. In face Constructors are chained, first statement in constructor is always a call to another constructor, either implicitly or explicitly. If you don't call any other constructor compiler will insert super(), which calls no argument constructor of super class in Java. this keyword represent current instance of class and super keyword represent instance of super class in Java.

3) Inheritance in Java represents IS-A relationship. If you see IS-A relationship between your domain Objects and Classes than consider using Inheritance e.g. if you have a class called ProgrammingLanguage than Java IS-A ProgrammingLanguage and should inherit from ProgrammingLanguage class.

4) Private members of Super class is not visible to Sub class even after using Inheritance in Java. Private members include any private field or method in Java.

5) Java has a special access modifier known as protected which is meant to support Inheritance in Java. Any protected member including protected method and field are only accessible in Child class or Sub class outside the package on which they are declared.

6)One of the risk of Inheritance in Java is that derived class can alter behavior of base class by overriding methods, which can compromise variants of base class e.g. if a malicious class overrides String's equals method in Java to change the comparison logic, you may get different behavior when String reference variable points to that class. to prevent such malicious overriding, you can make your class final to disallow inheritance. But beware making a class final severely implements its client's ability to reuse code. It make more sense from security perspective and that's one of the reason Why String is final in Java.

7) Use @Override annotation while overriding super class's method in subclass. This will ensure a compile time check on whether overriding method actually overrides super class method or not. Its common mistake to overload a method instead of overriding it mostly when super class method accept Object type, common examples are equals method, compareTo method and compare() method in Java.

When to use Inheritance in Java

Many programmer says favor composition over Inheritance which is true but there are cases where Inheritance is a natural choice, Even in Java API there are many places where inheritances is used e.g. 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. My general policy to decide whether to use Inheritance or not is to check "IS-A" relationship. For example all above example of Inheritance satisfy IS-A rule e.g. HashSet IS-A Set. Similarly if you have class called Fruit and want to create another class called Mango, its best to use inheritance and Mango should extend Fruit because Mango is a Fruit. By extending Fruit class it gets common state and behavior of Fruit object. Conversely 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 here. Another general rule of Inheritance is that if you are creating a class which adds more feature into existing class, you can extend it to reuse all of its code. That’s the reason of using  Runnable interface over Thread class for creating Thread in Java.


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 one interface extend another interface. On the other hand implements keyword is used when a class implement an interface which is also a form of Abstraction in Java. Interestingly, Inheritance facilitate Polymorphism in Java. By using Inheritance Sub class gets all property of Super class, except private,  and can represent Super class i.e. you can store sub class instance in a Super class reference variable, which is a form of Polymorphism in Java. All flexibility which is provided by interface based design is achieved using polymorphism. Common example of this Factory design pattern in Java where return type of Factory method should be base interface, which allows Factory method to return any implementation of base interface, which is actually created using Inheritance in Java. In next section we will an example of Inheritance, which will show How to code for inheritance.

Inheritance Example in Java

Here is a simple example of Inheritance in Java where we have a class called Server which represent any Server has common functionality e.g. uptime(), start() and stop(). Since every Server has own way of starting and stopping, it can override those method but any Server class can reuse common code which is applicable to all type of Server e.g. uptime.

/**
 *
 * Java program to demonstrate Inheritance in Java programming language.
 * Inheritance is used to reuse code and Java programming language allows you
 * to either extend class or implements Interface. In this Program we have a
 * Super Class Server and a Sub Class Tomcat, which is a Server.
 * Tomcat inherit start() and stop() method of Server Super Class.
 *
 * @author Javin
 */

public class Inheritance{

    public static void main(String args[]) {
        //Super class reference variable can hold Sub Class instance
        Server server = new Tomcat();
     
       //we need to cast to get actual Server instance back in reference variable.
       Tomcat tomcat = (Tomcat) server;     
       tomcat.start(); //starting Server
     
       System.out.println( "Uptime of Server in nano: " + server.uptime());
     
       tomcat.stop();
    }
 
 
}

class Server{
    private long uptime;
 
    public void start(){
        uptime = System.nanoTime();
    }
 
    public void stop(){
        uptime = 0;
    }
 
    public long uptime(){
        return uptime;
    }
}

class Tomcat extends Server{
 
    @Override
    public void start(){
        super.start();
        //Tomcat Server specific task
        System.out.println("Tomcat Server started");
    }
 
    @Override
    public void stop(){
        super.stop(); //you can call super class method using super keyword
        System.out.println("Tomcat Server Stopped");
    }
}

Output:
Tomcat Server started
Uptime of Server : 105898370823666
Tomcat Server Stopped


That’s all on What is Inheritance in Java, When to use Inheritance and How to use Inheritance in Java programming language. If you are coming from C++ background than only surprise for you is that Java does not support multiple Inheritance, other than that Java Inheritance is similar to Inheritance in any other programming language e.g. C++.


Related Java programming tutorial from Javarevisited Blog

4 comments :

k kondal said...

Hi,
I have question regrading Hibernate "What is n+1 problem and what is the best feasible solutions to over come this problem"?
can you please explain me and do the needful. thanks advance,

Anonymous said...

Thanks for the great post! I also did not know that Java does not support multiple inheritance like C++. This is a good reference which discusses that fact:


http://www.programmerinterview.com/index.php/java-questions/multiple-inheritance/

Suresh atta said...

But multiple inheritance still supports with Interfaces.


http://codeinventions.blogspot.in/2014/07/can-interface-extend-multiple.html

Anonymous said...

Inheritance should not be used just for code reuse purpose, there are better ways to do that e.g. composition and delegation. Instead, you should only use Inheritance when substitution is possible. In another word Derived class take stand in place of Base class, this is also known as liskov subtituion principle. If you use Inhritance just for code reuse and your class voilate Liskov subtituion principle, then your API will not be clean and you will face several issues, when a derived class object is passed to a method expecting object of base class.

Post a Comment