2 min read

Java Basics 3

Java Basics 3
Java Basics 3

Give an example of inheritance. What is the use of Super?

💡
It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.
💡
It is also used by class constructors to invoke constructors of its parent class.

Inheritance is a feature of object-oriented programming that allows a class to inherit properties and methods from another class. The class that inherits properties and methods is called the subclass or derived class, and the class from which properties and methods are inherited is called the superclass or base class.

Here is an example of inheritance in Java:

class Vehicle {
    int wheels;
    void startEngine() {
        // implementation
    }
}

class Car extends Vehicle {
    int seats;
    void honkHorn() {
        // implementation
    }
}

In this example, the Car class inherits from the Vehicle class. The Car class has access to the wheels and startEngine() method from the Vehicle class, as well as its own seats and honkHorn() method.

The super keyword is used to refer to the superclass or parent class of a class. It is used to access members (fields and methods) of the superclass that have been overridden by the subclass. It can also be used to call the constructor of the superclass.

For example:

class Car extends Vehicle {
    int seats;
    Car() {
        super();
    }
    void honkHorn() {
        super.startEngine();
        // implementation
    }
}

In this example, the Car constructor calls the constructor of the superclass using the "super()" statement. The honkHorn() method in the Car class calls the startEngine() method of the superclass using the "super.startEngine()" statement.

The use of super is to access the members of the parent class which are hidden by the members of the child class. It is mainly used to solve ambiguity caused by method overriding. Also, it is used to invoke the superclass's version of an overridden method.

How Static, private, and Final keywords can be used inheritance?

In Java, the use of the keywords "static", "private", and "final" with inheritance can have different effects depending on the context.

  1. Static: A static method or variable belongs to the class rather than an instance of the class, so it is not inherited by subclasses. However, a subclass can still access the static members of its superclass using the fully-qualified name of the superclass.
  2. Private: A private method or variable is not accessible from outside the class, so it is not inherited by subclasses. However, a private method or variable can still be used within the superclass and will affect the behavior of any overridden methods in the subclass.
  3. Final: A final method or variable cannot be overridden or re-assigned, so it is inherited by subclasses in the same way as a non-final method or variable. However, a subclass cannot override or re-assign a final method or variable.

In summary, Static and private members are not inherited by subclasses but a subclass can access them using the fully-qualified name of the superclass. Final members are inherited by subclasses but cannot be overridden.