menu

Inheritance in Java - Important Points

Inheritance in Java - MCQ


Inheritance is one of the core concepts of object-oriented programming (OOP), which allows us to create a new class by inheriting the properties and methods of an existing class. In Java, the process of inheritance involves creating a new class (the subclass) from an existing class (the superclass). The subclass inherits all the properties and methods of the superclass and can also add its own properties and methods.

Inheritance in Java is implemented using the 'extends' keyword. When a subclass extends a superclass, it inherits all the non-private properties and methods of the superclass. The subclass can then override the methods of the superclass or add its own methods to the subclass.

Let's consider an example to understand inheritance in Java:

class Animal {

    void eat() {

        System.out.println("I am eating.");

    }

}

class Dog extends Animal {

    void bark() {

        System.out.println("Woof!");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.eat();

        dog.bark();

    }

}

In the above example, we have a superclass 'Animal' with a method 'eat'. We then create a subclass 'Dog' that extends 'Animal' and has a new method 'bark'. In the 'Main' class, we create an object of the 'Dog' class and call the 'eat' and 'bark' methods.

The output of the above code will be:

I am eating.

Woof!

As we can see, the 'Dog' class has inherited the 'eat' method from the 'Animal' class and has added its own method 'bark'.

In Java, we can also use the 'super' keyword to access the properties and methods of the superclass in the subclass. The 'super' keyword can be used to call the constructor of the superclass or to call a method of the superclass.

Let's consider another example to understand the use of the 'super' keyword in Java:

class Animal {

    Animal() {

        System.out.println("I am an animal.");

    }

}

class Dog extends Animal {

    Dog() {

        super();

        System.out.println("I am a dog.");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

    }

}

In the above example, we have a superclass 'Animal' with a constructor that prints "I am an animal." We then create a subclass 'Dog' that extends 'Animal' and has a constructor that calls the constructor of the superclass using the 'super' keyword and then prints "I am a dog." In the 'Main' class, we create an object of the 'Dog' class.

The output of the above code will be:

I am an animal.

I am a dog.

As we can see, the 'Dog' class calls the constructor of the superclass using the 'super' keyword and then adds its own constructor to print "I am a dog."

In summary, inheritance in Java allows us to create a new class by inheriting the properties and methods of an existing class. We can use the 'extends' keyword to create a subclass that inherits from a superclass. We can also use the 'super' keyword to access the properties and methods of the superclass in the subclass. Understanding inheritance is an important concept for beginners to learn in Java.

Here are some additional points to help beginners better understand inheritance in Java:

  1. Inheritance forms an 'is-a' relationship between the subclass and superclass. For example, in the above example, 'Dog' is a subclass of 'Animal', and hence 'Dog' is an 'Animal'.
  2. Java supports single inheritance, which means a class can only extend one superclass. However, a class can implement multiple interfaces.
  3. In Java, the Object class is the ultimate superclass of all classes. Every class in Java implicitly extends the Object class.
  4. Inheritance can also help in code reusability, as we can reuse the properties and methods of the superclass in the subclass.
  5. When a subclass overrides a method of the superclass, the method of the subclass will be called when the object is of the subclass type. This is known as method overriding.
  6. Java also provides the 'final' keyword that can be used to prevent a class or method from being inherited or overridden by a subclass.
  7. In Java, access modifiers such as public, private, and protected can be used to control the accessibility of the properties and methods of the superclass in the subclass.
  8. Inheritance can also help in achieving polymorphism, where a single method can have different behaviors depending on the type of the object that is calling it.

Overall, inheritance is a fundamental concept in Java and is widely used in developing complex software applications. It provides a way to create new classes by reusing the properties and methods of existing classes, resulting in code reusability, easier maintenance, and better organization of code.

Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now