0% found this document useful (0 votes)
2 views1 page

Java Single Inheritance Example

Uploaded by

optimus prime
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 views1 page

Java Single Inheritance Example

Uploaded by

optimus prime
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

2.

Create a Java program demonstrating single inheritance where a subclass extends a superclass and
calls its methods

// Superclass
class Animal {
void eat() {
[Link]("The animal eats food.");
}

void sleep() {
[Link]("The animal sleeps.");
}
}

// Subclass
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}

void showActivities() {
// Calling superclass methods
eat();
sleep();
bark();
}
}

// Main class
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog(); // Creating object of subclass
[Link](); // Calling method that uses both superclass and subclass methods
}
}

You might also like