0% found this document useful (0 votes)
2 views9 pages

Inheritance & Polymorphisam in Java

Uploaded by

bhavyadarji2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Inheritance & Polymorphisam in Java

Uploaded by

bhavyadarji2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Inheritance

PROGRAM 1:SIMPLE INHERITANCE


// Parent class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

// Child class
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}

// Child class
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}

// Child class
class Cow extends Animal {
void sound() {
System.out.println("Cow moos");
}
}

// Main class
public class Geeks {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();

a = new Cat();
a.sound();

a = new Cow();
a.sound();
}
}

PROGRAM 2:
Types of Inheritance in Java

1) Single Inheritance
2) Multilevel Inheritance

3) Hierarchical Inheritance
4) Multiple Inheritance (Through Interfaces)
5) Hybrid Inheritance
Polymorphism
Method overriding in Java Inheritance

You might also like