What is Inheritance?

In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support.

Defining a new class based on an existing class is called derivation. The derived class is also called the direct subclass of the base or super class. You can also derive classes from the derived class and so on

class B extends A{
  //definition of class B
}
  • The keyword extends identifies that class B is a direct subclass of class A
  • The class B can have additional members in addition to the inherited members of class A
  • An inherited member of a base class is one that is accessible within the derived class
  • Base class members that are not inherited still form part of a derived class object
  • An inherited member of a derived class is a full member of that class and is freely accessible to any method in the class

Inheriting Data Members

  • The inheritance rules apply to class variables as well as instance variables

  • You can define a data member in a derived class with the same name as data member in the base class.
  • The data member of the base class is still inherited but is hidden by the derived class member with the same name
  • The hiding will occur irrespective if the type or access modifiers are the same or not.
  • Any use of the derived member name will always refer to the member defined in derived class
  • To refer to the inherited base class member, you must qualify it with the keyword super
  • Note that you cannot use super.super.something

Inheriting Methods

  • Methods in a base class excluding constructors are inherited in a derived class in the same way as the data members of the base class
  • Methods declared as private in a base class are not inherited
  • Note: Constructors in the base class are never inherited regardless of their attributes
  • Though the base class constructors are not inherited in your derived class, you can still call them or if you don’t call a base class constructor from your derived class constructor, the compiler will try to do it for you
  • The super class constructor is called in a subclass using super ( );

Constructors

  • If the first statement in a derived class constructor is not a call to a base class constructor, the compiler will insert a call to the default class constructor i.e super ( ), for you
  • Default call for base class constructor is with no arguments. This sometimes result in a compiler error. Why?
  • When you define your own constructor in a class, no default constructor is created by the compiler. Thus you have to define the no argument constructor for the class yourself so that in a derived class you don’t get the compile error due to call to default constructor of base class.
Example
public class Person {
	protected String name;
	protected String address;
/* Default constructor */
public Person() {
  System.out.println(“Inside Person:Constructor”)
   name = ""; address = "";
 }. . . .
}
public class Student extends Person {
public Student()
{    System.out.println(“Inside Student:Constructor”);
}. . . .
}
public static void main( String[] args ){
   Student ann = new Student();
}

The “super” keyword

  • A subclass can also explicitly call a constructor of its immediate super class.
  • This is done by using the super constructor call.
  • A super constructor call in the constructor of a subclass will result in the execution of relevant constructor from the super class, based on the arguments passed.
  • Few things to remember when using the super constructor call:
  • The super() call must occur as the first statement in a constructor
  • The super() call can only be used in a constructor (not in ordinary methods)

 Super Keyword Example

public Student(){
  super( "SomeName", "SomeAddress" );
  System.out.println("Inside Student:Constructor");
}

Another use of super is to refer to members of the super class (just like the this reference).

public Student() {
  super.name = “somename”;
  super.address = “some address”;
}

Object Class

Object class is mother of all classes. In Java language, all classes are sub-classed (extended) from the Object super class. Object class is the only class that does not have a parent class. It defines and implements behavior common to all classes including the ones that you write

  • getClass()
  • equals()
  • toString()

Tagged with: ClassesJAVAObject Oriented
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.