In Java, inheritance allows one class to inherit attributes and methods from another class. To implement inheritance in Java, you can use the extends
keyword to indicate that a class is a subclass of another class.
When creating a new class that wants to inherit from an existing class, you simply write public class NewClass extends ExistingClass { }
. This means that NewClass
is inheriting from ExistingClass
and will have access to all its public and protected attributes and methods.
You can also use the super
keyword to refer to the superclass from the subclass. This keyword is used to access variables and methods of the superclass when a method with the same name is also present in the subclass.
In Java, classes can only inherit from one superclass, which is known as single inheritance. However, you can implement multiple inheritance using interfaces, where a class can implement multiple interfaces but extend only one superclass.
Inheritance is a key concept in object-oriented programming as it allows for code reusability, promoting better organization and structure in your Java programs.
What is the super() constructor in Java inheritance?
The super() constructor in Java inheritance is used to call the constructor of the superclass. It must be the first statement in the child class constructor and can be used to pass arguments to the superclass constructor or to call the superclass constructor with no arguments. This is necessary when the superclass has parameters in its constructor that need to be initialized before the child class constructor can execute.
How to implement interface inheritance in Java?
To implement interface inheritance in Java, you can create a new interface that extends the parent interface. This new interface will inherit all the methods and constants from the parent interface and can also declare new methods or constants.
Here is an example of how to implement interface inheritance in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// Parent interface interface Animal { void eat(); void sleep(); } // Child interface that extends the Animal interface interface Pet extends Animal { void play(); } // Class Dog that implements the Pet interface class Dog implements Pet { @Override public void eat() { System.out.println("Dog is eating."); } @Override public void sleep() { System.out.println("Dog is sleeping."); } @Override public void play() { System.out.println("Dog is playing."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); dog.sleep(); dog.play(); } } |
In this example, the Pet
interface extends the Animal
interface, inheriting the eat()
and sleep()
methods. The Dog
class implements the Pet
interface, so it must provide implementations for all methods declared in both the Animal
and Pet
interfaces.
How to implement multiple inheritance in Java?
Java does not support multiple inheritance directly because it can lead to various issues like the Diamond Problem. However, you can achieve a similar effect by using interfaces or interface inheritance.
Here is an example of implementing multiple inheritance using interfaces in Java:
- Create multiple interfaces with the desired methods:
1 2 3 4 5 6 7 |
public interface Interface1 { void method1(); } public interface Interface2 { void method2(); } |
- Create a class that implements the interfaces:
1 2 3 4 5 6 7 8 9 10 11 |
public class MyClass implements Interface1, Interface2 { @Override public void method1() { System.out.println("Method 1"); } @Override public void method2() { System.out.println("Method 2"); } } |
- Use the MyClass class to call the methods:
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); obj.method1(); // Output: Method 1 obj.method2(); // Output: Method 2 } } |
By implementing multiple interfaces in a single class, you can achieve a similar effect to multiple inheritance in Java.
How to use the super() constructor in Java inheritance?
In Java, the super() constructor is used to call the constructor of the superclass. It is usually called as the first statement in the constructor of the subclass.
Here is an example to demonstrate how to use the super() constructor in Java inheritance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Animal { String species; Animal() { this.species = "Unknown"; } Animal(String species) { this.species = species; } void displayInfo() { System.out.println("Species: " + species); } } class Dog extends Animal { String breed; Dog(String species, String breed) { super(species); // calling the constructor of the superclass this.breed = breed; } void displayInfo() { super.displayInfo(); // calling a method of the superclass System.out.println("Breed: " + breed); } } public class Main { public static void main(String[] args) { Dog myDog = new Dog("Canine", "Golden Retriever"); myDog.displayInfo(); } } |
In this example, the Animal class has a parameterized constructor that takes a species as an argument. The Dog class extends Animal and has its own constructor that takes both species and breed as arguments. Inside the Dog constructor, super(species) is used to call the constructor of the superclass with the species argument. The displayInfo() method in the Dog class uses super.displayInfo() to call the displayInfo method of the superclass before displaying the breed information.
When you run the Main class, it will create a Dog object with the species "Canine" and the breed "Golden Retriever" and display the information using the displayInfo() method of the Dog class.