Inheritance in Java allows classes to inherit attributes and methods from another class. To implement inheritance, you can use the "extends" keyword to specify the parent class that the child class will inherit from. The child class can then access all the public and protected members of the parent class.
When implementing inheritance in Java, it is important to note that a class can only extend one parent class, as Java does not support multiple inheritance. You can create a hierarchy of classes by having multiple levels of inheritance, where a subclass becomes the parent class for another subclass.
In addition to inheriting attributes and methods, subclasses can also override methods from the parent class by providing their own implementation. This allows for polymorphism, where different classes can be treated as instances of the same parent class.
Inheritance in Java helps to promote code reusability and maintainability by allowing you to define common functionality in a parent class and have it automatically available in all its subclasses.
How to override a method in a subclass in Java?
To override a method in a subclass in Java, follow these steps:
- Create a subclass that extends the superclass which contains the method you want to override.
- Declare a method in the subclass with the same signature (name, return type, and parameters) as the method you want to override in the superclass.
- Use the @Override annotation before the method declaration in the subclass to indicate that you are overriding a method from the superclass. This is optional but good practice.
- Write the new implementation of the method in the subclass.
Here is an example demonstrating how to override a printMessage()
method in a subclass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Superclass { public void printMessage() { System.out.println("Hello from superclass"); } } class Subclass extends Superclass { @Override public void printMessage() { System.out.println("Hello from subclass"); } } public class Main { public static void main(String[] args) { Subclass obj = new Subclass(); obj.printMessage(); // Output: Hello from subclass } } |
In this example, the printMessage()
method in the Subclass
overrides the method with the same name in the superclass Superclass
. When the printMessage()
method is called on an object of the Subclass
, it will print "Hello from subclass" instead of "Hello from superclass".
What is the difference between inheritance and interface in Java?
Inheritance and interface are two mechanisms in Java that help achieve code reusability, but they differ in their implementation and usage.
- Inheritance: Inheritance is a mechanism in which a class inherits the properties and behaviors of another class. The class that is inherited from is called the superclass, and the class that inherits is called the subclass. Subclasses can access and reuse the fields and methods of the superclass. Inheritance allows for code reusability and the establishment of an "is-a" relationship between classes.
- Interface: An interface in Java is a reference type that contains only abstract method signatures, default methods, and static constants. Classes can implement interfaces, and they must provide implementations for all the abstract methods declared in the interface. Interfaces allow for achieving multiple inheritance in Java, as a class can implement multiple interfaces. Interfaces are used to enforce a contract on implementing classes, defining a common set of behaviors that implementing classes must adhere to. Interfaces are used to achieve abstraction and loose coupling in Java.
In summary, inheritance allows a class to inherit properties and behaviors from another class, while an interface defines a contract of behaviors that implementing classes must adhere to. Interfaces promote loose coupling and abstraction, while inheritance establishes an "is-a" relationship between classes.
How to call a superclass constructor in a subclass in Java?
In Java, you can call a superclass constructor in a subclass using the super()
keyword.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Animal { public Animal(String name) { System.out.println("Animal constructor called with name: " + name); } } public class Dog extends Animal { public Dog(String name) { super(name); // Call superclass constructor System.out.println("Dog constructor called with name: " + name); } public static void main(String[] args) { Dog myDog = new Dog("Buddy"); } } |
In this example, the Dog
class extends the Animal
class. When creating an instance of Dog
, the superclass constructor Animal(String name)
is called using super(name)
. This will output:
1 2 |
Animal constructor called with name: Buddy Dog constructor called with name: Buddy |
This demonstrates how to call a superclass constructor in a subclass in Java.