
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why Abstract Class is used in Java?
A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.
A program that demonstrates an abstract class in Java is given as follows:
Example
abstract class Animal { abstract void sound(); } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal { void sound() { System.out.println("Cow Moos"); } } public class Demo { public static void main(String[] args) { Animal a; a = new Cat(); a.sound(); a = new Dog(); a.sound(); a = new Cow(); a.sound(); } }
Output
Cat Meows Dog Barks Cow Moos
- Related Questions & Answers
- Why Final Class used in Java?
- Abstract class in Java
- Why can't a Java class be both abstract and final?
- Differences between abstract class and concrete class in Java
- Can we define an abstract class without abstract method in java?
- What is the difference between abstract class and a concrete class in Java?
- Can we define an abstract class with no abstract methods in Java?
- Difference between Abstract Class and Interface in Java
- Differences between abstract class and interface in Java
- Explain abstract class in PHP.
- Why GeckoDriver is used in selenium?
- Why can't static method be abstract in Java?
- Why String class is immutable or final in Java?
- Why Object class is the super class for all classes in Java?
- Can a class in Java be both final and abstract?
Advertisements