Java Interview Questions
Java
Web DevelopmentBackendQuestion 14
Explain the concept of inheritance in Java.
Answer:
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behaviors (fields and methods) of another class. It promotes code reuse and establishes a natural hierarchical relationship between classes.
Key Concepts of Inheritance
- Superclass (Parent Class): The class whose properties and methods are inherited by another class.
- Subclass (Child Class): The class that inherits the properties and methods of another class.
Syntax
To create a subclass, use the extends
keyword followed by the name of the superclass.
class Superclass {
// Fields and methods
}
class Subclass extends Superclass {
// Additional fields and methods
}
Example
Superclass:
public class Animal {
String name;
public void eat() {
System.out.println(name + " is eating.");
}
}
Subclass:
public class Dog extends Animal {
public void bark() {
System.out.println(name + " is barking.");
}
}
Using the Subclass:
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.eat(); // Inherited method
dog.bark(); // Subclass-specific method
}
}
Output:
Buddy is eating.
Buddy is barking.
Types of Inheritance
- Single Inheritance: A subclass inherits from one superclass.
- Multilevel Inheritance: A subclass inherits from another subclass, forming a chain.
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
Single Inheritance Example:
class Animal {
// Fields and methods
}
class Dog extends Animal {
// Fields and methods
}
Multilevel Inheritance Example:
class Animal {
// Fields and methods
}
class Dog extends Animal {
// Fields and methods
}
class Puppy extends Dog {
// Fields and methods
}
Hierarchical Inheritance Example:
class Animal {
// Fields and methods
}
class Dog extends Animal {
// Fields and methods
}
class Cat extends Animal {
// Fields and methods
}
Benefits of Inheritance
- Code Reusability: Allows the reuse of fields and methods of the existing class.
- Method Overriding: Subclasses can provide specific implementations for methods defined in the superclass.
- Polymorphism: Facilitates the use of a superclass reference to refer to a subclass object.
Method Overriding
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
Example:
public class Animal {
public void makeSound() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.makeSound(); // Output: Bark
}
}
super
Keyword
The super
keyword is used in a subclass to refer to its immediate superclass. It can be used to access superclass methods and constructors.
Accessing Superclass Methods:
public class Animal {
public void makeSound() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
super.makeSound(); // Call superclass method
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Animal sound\nBark
}
}
Calling Superclass Constructor:
public class Animal {
String name;
public Animal(String name) {
this.name = name;
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name); // Call superclass constructor
}
public void display() {
System.out.println("Dog's name is " + name);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.display(); // Output: Dog's name is Buddy
}
}
Important Points
- Inheritance vs. Composition: Inheritance represents an "is-a" relationship (e.g., a Dog is an Animal). Composition represents a "has-a" relationship (e.g., a Car has an Engine).
- Access Modifiers: Private members of the superclass are not accessible directly in the subclass. Protected members are accessible in subclasses.
- Multiple Inheritance: Java does not support multiple inheritance with classes to avoid complexity and ambiguity. However, a class can implement multiple interfaces.
Conclusion
Inheritance is a powerful feature in Java that allows for hierarchical classification and code reuse. By using inheritance, you can create a well-structured and maintainable codebase, promoting efficient and logical organization of related classes. Understanding and leveraging inheritance effectively is crucial for mastering object-oriented programming in Java.