0% found this document useful (0 votes)
5 views

Java_Practical _9

The document presents practical examples of different types of inheritance in Java: single inheritance with a Dog class extending an Animal class, multilevel inheritance with a Puppy class extending Dog, and multiple inheritance using interfaces with a Duck class implementing Walkable and Swimmable. Each section includes code snippets demonstrating the creation of instances and method calls. The document is authored by Sahil Badhe, with enrollment number 2206005.

Uploaded by

Sahil Badhe
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)
5 views

Java_Practical _9

The document presents practical examples of different types of inheritance in Java: single inheritance with a Dog class extending an Animal class, multilevel inheritance with a Puppy class extending Dog, and multiple inheritance using interfaces with a Duck class implementing Walkable and Swimmable. Each section includes code snippets demonstrating the creation of instances and method calls. The document is authored by Sahil Badhe, with enrollment number 2206005.

Uploaded by

Sahil Badhe
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/ 3

Practical No : 09

• Single Inheritance
/*
Practical No:- 09
Enroll : 2206005
Name : Sahil Badhe
*/

// Superclass
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

// Subclass
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

// Main class
public class SingleInheritance {
public static void main(String[] args) {
Dog myDog = new Dog(); // Creating an instance of Dog

myDog.eat(); // Calling method from the superclass


myDog.bark(); // Calling method from the subclass
}
}

• Out put
• Multilevel Inheritance
/*
Practical No:- 09
Enroll : 2206005
Name : Sahil Badhe
*/
// Base class
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Intermediate class
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

// Derived class
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping");
}
}
// Main class
public class MultilevelInheritance {
public static void main(String[] args) {
Puppy myPuppy = new Puppy(); // Creating an instance of Puppy

myPuppy.eat(); // Calling method from Animal class


myPuppy.bark(); // Calling method from Dog class
myPuppy.weep(); // Calling method from Puppy class
}
}
• Out Put
• Multiple Inheritance
/*
Practical No:- 09
Enroll : 2206005
Name : Sahil Badhe
*/
interface Walkable
{
void walk(); //method decaration

}
interface Swimmable
{
void swim();

}
class Duck implements Walkable,Swimmable
{
public void walk() //implementing the method from method declaration
{
System.out.println("Duck is walking");
}
public void swim()
{
System.out.println("Duck is swimming");
}
}
class MultipleInheritance
{
public static void main(String[] args)
{
Duck d=new Duck();
d.walk();
d.swim();
}
}
• Out Put

You might also like