Abstract class:
In Java, abstraction is a fundamental concept of object-oriented programming (OOP) that focuses on hiding the internal details and complexities of a class while providing a simplified interface for interacting with objects. It is one of the key principles of OOP, along with encapsulation, inheritance, and polymorphism.
Hide internal details and show only functionality to the user.
To create an abstract class in Java, you need to follow these steps:
Use the abstract keyword before the class declaration to specify that it is an abstract class.
Define one or more abstract methods inside the abstract class. An abstract method is a method that is declared without an implementation. You specify the method signature (name, return type, and parameters), but don't provide the method body.
Optionally, you can also have concrete (non-abstract) methods in the abstract class that provide a default implementation or common functionality for derived classes.
You cannot directly create an instance of an abstract class using the new keyword. Instead, you need to create concrete classes that extend the abstract class and provide implementations for the abstract methods.
syntax:
public abstract class <name of the class>{
//abstract method
//non-abstract method
}
Abstraction allows you to create abstract classes and interfaces that define common behaviors and characteristics of a group of related classes, without providing a specific implementation. An abstract class cannot be instantiated directly; it serves as a blueprint for creating derived classes. On the other hand, an interface is a contract that defines a set of methods that a class must implement.
By using abstraction, you can define a common interface or set of methods that multiple classes can implement in their own specific way. This promotes code reusability and provides a way to interact with objects at a higher level of abstraction without needing to know the internal details of how they are implemented.
Here's an example to illustrate abstraction in Java:
java// Abstract class
abstract class Animal {
public abstract void sound();
}
// Concrete classes implementing the abstract class
class Dog extends Animal {
public void sound() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Meow!");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.sound(); // Output: Woof!
cat.sound(); // Output: Meow!
}
}
In this example, the Animal
class is an abstract class that defines the sound()
method as an abstract method, meaning it doesn't provide an implementation. The Dog
and Cat
classes extend the Animal
class and provide their own implementations of the sound()
method. In the Main
class, we can create instances of Dog
and Cat
and call the sound()
method without knowing the specific details of how each animal produces its sound. This demonstrates abstraction by providing a simplified interface to interact with different types of animals.
0 Comments