2011-12-06

How inheritance is implemented in java technically ?

As the name suggests inheritance means to take something that is already made.It is one of the most important features of Object Oriented Programming. It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derive classes from another classes.
Inheritance is the process of reusing class members from another class as if they were defined int that class. Through inheritance we can obtain one object property to another object.
Basically by implementing inheritance we are establishing a relation between two classes and then extending one class scope to another class.So that other class members can be accessed directly from this class by their names as if they were developed in this class.

Inheritance can be implemented in java using below two keywords:
1.extends
2.implements

Differences between extends and implements keywords

Although, Implements and Extends are two keywords that provide a mechanism to inherit attributes and behavior to a class in Java programming language, they are used for two different purposes. Implements keyword is used for a class to implement a certain interface, while Extends keyword is used for a subclass to extend from a super class. When a class implements an interface, that class needs to implement all the methods defined in the interface, but when a subclass extends a super class, it may or may not override the methods included in the parent class. Finally, another key difference between Implements and Extends is that, a class can implement multiple interfaces but it can only extend from one super class in Java. In general, usage of Implements (interfaces) is considered more favorable compared to the usage of Extends (inheritance), for several reasons like higher flexibility and the ability to minimize coupling. Therefore in practice, programming to an interface is preferred over extending from base classes.

Syntax:

class Example { }
class Sample extends Example { }

The class followed by extends keyword is called super class, here Example is super class, and the class that follows extends keyword is called subclass, here Sample is sub class.
Super class is also called as Parent / Base class
Sub class is also called as Child / Derived class

//The below program explains implementing inheritance

//Example.java
class Example 
{
   static int a=10;
   int x=20;
   static void m1()
   {
      System.out.println("Example m1");
   }
   void m2()
   {
      System.out.println("Example m2");
   }
}

//Sample.java
class Sample extends Example
{
   public static void main(String[] args) 
   {
      System.out.println(a);
      m1();
      Sample s = new Sample();
      System.out.println(s.x);
      s.m2();
   }
}

In the above program we accessed super class static members variable 'a' and m1() directly by their names and without creating super class object we also accessed non-static members variable 'x' and method m2() with sub class object.

Loading

Enter your Email here: