Abstract Class with Multiple Methods
This program demonstrates abstract class with multiple abstract methods.
Problem Statement
Write a Java program to define abstract class Animal with abstract methods eat() and sound().
Source Code
| 1 | abstract class Animal { |
| 2 | abstract void eat(); |
| 3 | abstract void sound(); |
| 4 | } |
| 5 | class Dog extends Animal { |
| 6 | void eat(){ System.out.println("Dog eats bones"); } |
| 7 | void sound(){ System.out.println("Dog barks"); } |
| 8 | } |
| 9 | public class AbstractMultipleMethods { |
| 10 | public static void main(String[] args) { |
| 11 | System.out.println("Eduinq Abstract Multiple Methods"); |
| 12 | Animal a=new Dog(); |
| 13 | a.eat(); a.sound(); |
| 14 | } |
| 15 | } |
Program Output
Eduinq Abstract Multiple Methods Dog eats bones Dog barks
Explanation
Abstract class can declare multiple abstract methods. - Subclass must implement all of them. - This enforces consistent behavior across subclasses while allowing specific implementations. Conclusion: Shows how to use abstract class with multiple methods.