Abstract in Java
In Java, an abstract class is a class that is declared with the abstract keyword. It can contain both abstract methods (methods without a body) and non-abstract methods (methods with a body). An abstract class cannot be instantiated directly; its implementation must be provided by a subclass. In Java, abstract classes are used to achieve abstraction, which is the process of hiding the internal details of an object and exposing only the necessary functionality.
Abstraction in Java
Abstraction in Java can be achieved in two ways:
1. Abstract class
2. Interface
Both abstract classes and interfaces can have abstract methods that enforce abstraction. However, abstract classes can also have non-abstract methods, whereas interfaces (prior to Java 8) cannot have method implementations.
A method that is declared as abstract and does not have a body is called an abstract method. Its implementation must be provided by derived classes.
Abstract Class Structure:
abstract class MyBaseClass {
// Abstract method declaration
public abstract void myAbstractMethod();
// Non-abstract method
public void myConcreteMethod() {
System.out.println(“This is a concrete method in the abstract class.”);
}
}
Scenario: Simulating Different Pets
You can create a program to simulate different pets where each pet makes its own sound, and all pets can sleep. You can use an abstract class for common behaviors and specific classes for each pet.
Example:
abstract class Animal {
public abstract void makeSound(); // Abstract method
public void sleep() { // Concrete method
System.out.println(“The animal is sleeping.”);
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println(“The dog says: Woof Woof”);
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println(“The cat says: Meow Meow”);
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound(); // Outputs: The dog says: Woof Woof
myCat.makeSound(); // Outputs: The cat says: Meow Meow
myDog.sleep(); // Outputs: The animal is sleeping.
myCat.sleep(); // Outputs: The animal is sleeping.
}
}
Explanation:
abstract class Animal {
// Define an abstract class named Animal
public abstract void makeSound();
// Abstract method that must be implemented by derived classes
public void sleep() {
System.out.println(“The animal is sleeping.”);
// Concrete method that is common to all animals
class Dog extends Animal {
// Dog class inherits from Animal
public void makeSound() {
System.out.println(“The dog says: Woof Woof”);
// Override the makeSound method to specify dog’s sound @Override
class Cat extends Animal {
// Cat class inherits from Animal
public void makeSound() {
System.out.println(“The cat says: Meow Meow”);
}
// Override the makeSound method to specify cat’s sound @Override
public class Main {
public static void main(String[] args) {
// Main program class
Animal myDog = new Dog();
Animal myCat = new Cat();
// Create instances of Dog and Cat
myDog.makeSound(); // Outputs: The dog says: Woof Woof
myCat.makeSound(); // Outputs: The cat says: Meow Meow
// Call the makeSound method for dog and cat
myDog.sleep(); // Outputs: The animal is sleeping.
myCat.sleep(); // Outputs: The animal is sleeping.
// Call the sleep method for dog and cat
Tasks:
1. Basic Abstract Class and Method Implementation:
Create an abstract class Vehicle with an abstract method start(). Derive two classes Car and Bike from Vehicle. Implement the start() method in both derived classes to print “Car starting” and “Bike starting” respectively. In the main method, create instances of Car and Bike and call the start() method on each.
2. Abstract Class with Properties:
Create an abstract class Person with an abstract property name and an abstract method getDetails(). Derive two classes Student and Teacher from Person. Implement the name property and getDetails() method in both derived classes. In the main method, create instances of Student and Teacher, set their names, and call the getDetails() method to print their details.
3. Abstract Class with Constructor:
Create an abstract class Appliance with a protected constructor that takes a String parameter brand and an abstract method showBrand(). Derive two classes WashingMachine and Refrigerator from Appliance. Implement the showBrand() method in both derived classes to print the brand. In the main method, create instances of WashingMachine and Refrigerator and call the showBrand() method.
4. Abstract Class with Multiple Methods:
Create an abstract class Shape with two abstract methods: calculateArea() and calculatePerimeter(). Derive two classes Circle and Rectangle from Shape. Implement the calculateArea() and calculatePerimeter() methods in both derived classes. In the main method, create instances of Circle and Rectangle, set their dimensions, and call the methods to print the area and perimeter of each shape.
5. Abstract Class and Inheritance Hierarchy:
Create an abstract class Animal with an abstract method speak(). Derive a class Mammal from Animal and override the speak() method. Further, derive two classes Dog and Cat from Mammal and provide their specific implementations of the speak() method. In the main method, create instances of Dog and Cat and call their speak() methods.
6. Abstract Class with Virtual and Abstract Methods:
Create an abstract class Employee with a concrete method work() that prints “Working” and an abstract method getSalary(). Derive two classes FullTimeEmployee and PartTimeEmployee from Employee. Override the work() method in both derived classes to provide specific implementations and implement the getSalary() method. In the main method, create instances of FullTimeEmployee and PartTimeEmployee and call their methods.
Course Video
YouTube Reference :
The training covers Java abstract classes, including their concepts, syntax, and practical applications.
Yes, it includes examples demonstrating how to define abstract classes and implement abstract methods in subclasses.
Yes, exercises involve creating abstract classes and concrete subclasses to understand their usage in various scenarios.
Yes, it’s designed for beginners with clear explanations and step-by-step examples.
A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler will suffice.
Yes, it covers how abstract classes provide a base for other classes to build upon, promoting code reuse and organization.
Yes, the training is self-paced and accessible online anytime.
Yes, it explains the distinctions and appropriate use cases for abstract classes and interfaces
It typically takes a few hours to complete, depending on your learning pace.
Yes, this Java Abstract Classes training is completely free on Iqra Technology Academy’s website.