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?
- Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.
- Since constructors and initializer blocks are not members of a Class, they are not inherited by a subclass.
- A subclass can extend only one superclass
- 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.