abstract class example program in java
abstract class example program in java
An abstract class in Java serves as a blueprint for other classes and cannot be instantiated on its own. It can contain abstract methods (without a body) that must be implemented by subclasses, as well as concrete methods with defined behavior. For example, consider an abstract class named “Animal” with an abstract method “makeSound()”. Subclasses like “Dog” and “Cat” would extend the “Animal” class and provide their specific implementation of the “makeSound()” method. This setup allows the creation of a hierarchy where common features are defined in the abstract class, while each subclass implements its unique characteristics, promoting code reusability and a clear structure in object-oriented programming.
To Download Our Brochure: https://www.justacademy.co/download-brochure-for-free
Message us for more information: +91 9987184296
1 — Definition of Abstract Class: An abstract class in Java is a class that cannot be instantiated on its own and is meant to be subclassed. It can contain abstract methods (methods without a body) as well as concrete methods (methods with a body).
2) Purpose of Abstract Classes: They are used to provide a base for subclasses to build upon, promoting code reuse and establishing a clear contract for derived classes.
3) Abstract Methods: An abstract class can declare abstract methods, which must be implemented by any subclass that is not also abstract.
4) Example Program Structure: An example program might involve creating an abstract class called “Animal” with abstract methods like “makeSound()” and concrete methods like “eat()”.
5) Class Implementation: Subclasses such as “Dog” and “Cat” would extend the “Animal” class and provide their own implementations of the “makeSound()” method.
6) Code Reusability: By using an abstract class, the common functionality (like “eat()”) can be implemented in the base class (Animal) and reused in the subclasses without duplication.
7) Preventing Instantiation: By declaring a class as abstract, you prevent instantiation directly. For example, Animal myAnimal = new Animal(); would raise an error.
8) Example Program Code:
java
abstract class Animal {
abstract void makeSound(); // abstract method
void eat() { // concrete method
System.out.println(“This animal eats food.”);
}
}
class Dog extends Animal {
void makeSound() {
System.out.println(“Bark”);
}
}
class Cat extends Animal {
Get Surbhi K’s stories in your inbox
Join Medium for free to get updates from this writer.
void makeSound() {
System.out.println(“Meow”);
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound(); // Output: Bark
myCat.makeSound(); // Output: Meow
myDog.eat(); // Output: This animal eats food.
}
}
9) Polymorphism: This approach allows you to handle objects of different subclasses (Dog and Cat) using a common superclass reference (Animal), enabling polymorphism.
10) Real world Application: Abstract classes are often used in frameworks or APIs to define common behaviors across different components, thus standardizing functionality.
11) Flexibility: They provide flexibility in designing systems where future extensions are anticipated, allowing new subclasses to be created with their specific behaviors.
12) Summary: Understanding abstract classes is crucial for understanding inheritance and polymorphism in Java, forming a foundational concept in object oriented programming.
13) Importance of Practice: Encouraging students to practice creating and extending abstract classes will help solidify their understanding and improve their coding skills in Java.
Browse our course links : https://www.justacademy.co/all-courses
To Join our FREE DEMO Session: Click Here
This information is sourced from JustAcademy
Contact Info:
Roshan Chaturvedi
Message us on Whatsapp: +91 9987184296
Email id: info@justacademy.co






