Inheritance Java Program
Published: 2021-11-23
2 min read
Here are java program to implement the inheritance, the most amazing OOP concept. There are different types of inheritance like single, multilevel, and multiple inheritance.
Single Inheritance
Source Code:
class Animal{
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal{
void bark() {
System.out.println("barking...");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.bark();
d.eat();
}
}
Output:
barking...
eating...
Multi-Level Inheritance
Source Code:
class Animals{
void eat() {
System.out.println("eating...");
}
}
class Dogs extends Animals{
void bark() {
System.out.println("barking...");
}
}
class BabyDogs extends Dogs{
void weep() {
System.out.println("weeping...");
}
}
public class MultiLevelInheritance {
public static void main(String[] args) {
BabyDogs bd = new BabyDogs();
bd.weep();
bd.bark();
bd.eat();
}
}
Output:
weeping...
barking...
eating...
Multiple Inheritance
Source Code:
interface Vehicle{
void wheelerType();
}
interface Color{
void colorType();
}
class Car implements Vehicle, Color{
public void wheelerType() {
System.out.println("It's two wheeler.");
}
public void colorType() {
System.out.println("It's Red color.");
}
}
public class MultipleInheritance {
public static void main(String s[]) {
Car c1 = new Car();
c1.wheelerType();
c1.colorType();
}
}
Output:
It's two wheeler.
It's Red color.
Recommended Post
Free Engineering ToolsNEW
8 free, 100% client-side tools for developers — no signup, no data uploads.
Explore all tools