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 new behavior 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. 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 superclass.
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 this reference. Note that 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 Superclass will be inherited by a subclass.
- All subclasses will inherit all default variables in the same package only. Subclasses outside the package will not inherit any default members.
- Private members cannot 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.
- All subclasses will inherit protected variables in the same package or outside package(Different from default).
- Methods which are not inherited cannot be overridden. And hence overridden rules cannot be applied to those Methods. But Methods can still be defined in subclass though those Methods will not be the overridden Method. Instead, it represents 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 cannot be overridden, they can be redefined in a subclass. The redefinition is not called overridden but hidden.