Inheritance

Java Inheritance defines an is-a relationship between a superclass and its subclasses. This means that an Object of a subclass can be used wherever an object of the superclass can be used. Class Inheritance in Java is used to build new Classes from existing Classes. The inheritance relationship is transitive: if Class x extends Class y, then a Class z, which extends Class x, will also inherit from Class y.

For example a car Class can inherit some properties from a general vehicle Class. Here we find that the base Class is the vehicle Class and the subclass is more specific car Class. A subclass must use the extends clause to derive from a super Class which must be written in the header of the subclass definition. The subclass inherits members of the superclass and hence promotes code reuse. The subclass itself can add its own new behaviour and properties. The java.lang.Object Class is always at the top of any Class inheritance hierarchy.

What is not possible using java Class Inheritance?

  1. Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.
  2. Since constructors and initializer blocks are not members of a Class, they are not inherited by a subclass.
  3. A subclass can extend only one superclass
  4. Members that have default accessibility in the superclass 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 superclass.

this and super keywords:

The two keywords, this and super to help you explicitly name the field or Method that you want. Using this and super keyword you have full control on whether to call a Method or field in the same Class or to call from the immediate superclass. The this keyword is used as a reference to the current Object which is an instance of the current Class. The super keyword also references the current Object, but as an instance of the current class’s super class.

The this keyword references the current Object and is useful in situations where a local variable hides, or shadows, a field with the same name. If a Method needs to pass the current Object to another Method, it can do so using the this reference. Note that the this reference cannot be used in a static context, as static code is not executed in the context of any Object.

Java Inheritance Cheatsheet

 

  • All public variables of Super class will be inherited by subclass.
  • All default variables will be inherited by all subclasses in the same package only. Subclasses outside the package will not inherit any default memeber.
  • Private members can not be inherited by subclass because they will not be visible to the subclass and hence the subclass can create the Method or property with the same name without any problem.
  • Protected variables will be inherited by all subclasses in the same package or outside package(Different from default).
  • Methods which are not inherited can not be overridden. And hence overridden rules can not be applied to those Methods. But Methods can still be defined in subclass though those Methods will not be the overridden Method. Instead it defines a new Method.
  • Static Methods or variables do not take part in inheritance.
  • Even though static methods or variables do not take part in inheritance and can not be overridden they can be redefined in subclass. The redefinition is not called overridden but hidden.

 

Series Navigation<< Inner ClassString Builder >>
By | 2017-07-09T12:14:42+00:00 April 21st, 2012|Core Java|3 Comments

About the Author:

3 Comments

  1. Durga Prasad March 26, 2015 at 1:27 pm - Reply

    Hi,

    Is there any pdf copies of the core java material for offline. If it is available please give me the link for downloading or else can you send me mail.

    Regards,
    Prasad.

  2. Meena January 12, 2016 at 3:35 pm - Reply

    Can you please explain using a program about “how a Subclass can’t inherit constructor from its Superclass”?

    • J Singh January 16, 2016 at 7:08 am - Reply

      Inheriting means you are talking about property or Method of any object. While Constructor is not an object it’s sole purpose is to initialize the object. So there is no point in inheriting constructor of a class.

Leave A Comment