Java – class inheritance | superclass and subclass

In Java a class can inherit another class. The inherited class is called Superclass and the class that inherits another class it is called subclass.

syntax:

puplic class subclassName extends superclassName{
   additional fields and methods or method overriding
}
class inheritance, <span>Java – class inheritance | superclass and subclass</span>, KandZ
superclass
class inheritance, <span>Java – class inheritance | superclass and subclass</span>, KandZ
subclass

The subclass should only declare new fields, implement new methods or override superclass methods. All the variables and methods of the superclass are inherited automatically. As you see on the example above, the subclass’s constructor called the superclass constructor using the super keyword. This is important. The subclass must initialize the superclass with super and the parameters. If you want to call a method from the superclass then use super.MethodName(parametersIfAny);

class inheritance, <span>Java – class inheritance | superclass and subclass</span>, KandZ

All public methods from the superclass can be called from the subclasss. If you use the private keyword then this is impossible. The protected keyword allows the subclass to call the method, but not a subclass of the subclass.

If you want to override a method then read this post.

THE END!!!

88790cookie-checkJava – class inheritance | superclass and subclassJava – class inheritance | superclass and subclass

Related posts