Inheritance is an object-oriented programming concept where a new class, known as a subclass or derived class, inherits the properties and behaviors (attributes and methods) from another class, known as the superclass or base class. Inheritance allows for code reusability and the creation of a hierarchical relationship between classes.
The "IS-A" relationship in inheritance represents a relationship between two classes where one class is a specialized form of another. Example
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
}
}
// Base class (Super class)
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Single Inheritance: Dog extends Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
// Multi-level Inheritance: Dog extends Animal and Puppy extends Dog
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping");
}
}
// Hierarchical Inheritance: Dog and Cat both extends Animal
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
// Multiple Inheritance via Interface
interface Pet {
void play();
}
class DomesticDog extends Dog implements Pet {
public void play() {
System.out.println("Dog is playing");
}
}
public class Main {
public static void main(String[] args) {
// Single Inheritance
Dog dog = new Dog();
dog.eat();
dog.bark();
// Multi-level Inheritance
Puppy puppy = new Puppy();
puppy.eat();
puppy.bark();
puppy.weep();
// Hierarchical Inheritance
Cat cat = new Cat();
cat.eat();
cat.meow();
// Multiple Inheritance via Interface
DomesticDog domesticDog = new DomesticDog();
domesticDog.play();
domesticDog.bark();
domesticDog.eat();
}
}
Answer: No, private members of a superclass are not accessible directly by the subclass. However, they can be accessed through public or protected methods of the superclass.
Answer: Overriding refers to redefining a method in the subclass that already exists in the superclass with the same signature. Overloading refers to having multiple methods in the same class with the same name but different parameter lists.
Answer: Java doesn't support multiple inheritance using classes to avoid ambiguity caused by the "diamond problem." Instead, multiple inheritance is achieved using interfaces.
Answer: No, constructors are not inherited. However, the subclass can call the constructor of the superclass using the super() keyword.
Answer: To prevent inheritance, the class can be declared as final. A final class cannot be subclassed.
Answer: The method from the superclass will be called, as the subclass has not provided its own overriding implementation.
A is extended by class B, and class B is extended by class C. If class C does not override a method from class A, which version of the method is called when an object of class C invokes it?
Answer: The method from class A will be called, as class B and class C do not override it.
Answer: Yes, protected members are accessible to subclasses and classes in the same package.
Answer: Objects of the subclass hold a reference to the parent class, meaning all fields from the superclass are also stored in memory when an object of the subclass is created. This hierarchical allocation can impact memory usage.