Answer
OOP Inheritance in Java
Single Inheritance (Parent → Child)
Java
// Parent class
class Animal {
String name;
void eat() {
System.out.println(name + " eats food");
}
void breathe() {
System.out.println(name + " breathes air");
}
}
// Child class inherits from Animal
class Dog extends Animal {
void bark() {
System.out.println(name + " barks: Woof!");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.eat(); // inherited from Animal
dog.breathe(); // inherited from Animal
dog.bark(); // Dog's own method
}
}
Output
CODE
Buddy eats food
Buddy breathes air
Buddy barks: Woof!
Multilevel Inheritance (A → B → C)
Java
class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {
void drive() { System.out.println("Car is driving"); }
}
class ElectricCar extends Car {
void charge() { System.out.println("Electric car is charging"); }
}
public class MultilevelInheritance {
public static void main(String[] args) {
ElectricCar tesla = new ElectricCar();
tesla.start(); // from Vehicle (grandparent)
tesla.drive(); // from Car (parent)
tesla.charge(); // own method
}
}
Output
CODE
Vehicle started
Car is driving
Electric car is charging
Hierarchical Inheritance (One Parent → Multiple Children)
Java
class WebDriver {
void open(String url) { System.out.println("Opening: " + url); }
void close() { System.out.println("Browser closed"); }
}
class ChromeDriver extends WebDriver {
void launchChrome() { System.out.println("Chrome launched"); }
}
class FirefoxDriver extends WebDriver {
void launchFirefox() { System.out.println("Firefox launched"); }
}
public class HierarchicalInheritance {
public static void main(String[] args) {
ChromeDriver chrome = new ChromeDriver();
chrome.launchChrome(); // own
chrome.open("https://google.com"); // inherited
FirefoxDriver firefox = new FirefoxDriver();
firefox.launchFirefox(); // own
firefox.close(); // inherited
}
}
Key Concepts
| Concept | Description |
|---|---|
extends | Keyword to inherit from parent class |
super() | Call parent class constructor |
super.method() | Call parent class method |
@Override | Override parent method in child |
| Method Overriding | Child redefines parent method |
instanceof | Check if object is instance of a class |
Using super keyword
Java
class Parent {
String name = "Parent";
void display() { System.out.println("Parent class"); }
}
class Child extends Parent {
String name = "Child";
void display() {
super.display(); // call parent method
System.out.println("Child class");
System.out.println("Parent name: " + super.name); // parent field
System.out.println("Child name: " + this.name); // child field
}
}
Java Does NOT Support Multiple Inheritance (with classes)
Java
// This causes compile error:
// class C extends A, B {} // ERROR: multiple inheritance not allowed
// Use interfaces instead:
interface A { void methodA(); }
interface B { void methodB(); }
class C implements A, B {
public void methodA() { System.out.println("A"); }
public void methodB() { System.out.println("B"); }
}
