Oops Abstraction in Java
What is abstraction?
Abstraction is a programming concept used in Java that enables you to build sophisticated systems by keeping implementation details hidden from the user and just exposing the functionality that is required. By developing abstract classes or interfaces that specify the overall behaviour of a group of related classes, it is a method for controlling complexity.
To specify common behaviours and traits that can be inherited or implemented by other classes, abstract classes and interfaces are created in Java. It enables you to design a shared interface without mentioning the specifics of implementation for a collection of classes.
One of the guiding principles of object-oriented programming (OOP) is abstraction, which is employed to accomplish modularity, encapsulation, and code reuse. You can concentrate on the key characteristics and actions of a single object or a collection of related objects by abstracting away the implementation specifics.
How we can achieve abstraction in oops?
We can achieve abstraction using abstract class and interfaces:
Abstract class
In java, an abstract class which cannot be instantiated, which means it cannot make objects out of it. It act as a foundation or model for classes that augment in it.
A class that is abstract may have both abstract and non-abstract methods. Abstract methods lack a body because they are stated without an implementation. Any subclass that extends the abstract class must implement these methods. On the other hand, non-abstract methods contain a body and can be called straight from the subclass without requiring any additional implementation.
For example:
//Abstract class abstract class Animal { // Abstract method public abstract void makeSound(); } //Subclass of the abstract class class Dog extends Animal { public void makeSound() { System.out.println("Woof!"); } } //Another subclass of the abstract class class Cat extends Animal { public void makeSound() { System.out.println("Meow!"); } } //Main class to demonstrate the usage of the abstract class public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.makeSound(); // Output: Woof! Animal cat = new Cat(); cat.makeSound(); // Output: Meow! } }
Explanation:
Basically, in this program Animal is a abstract class that defines an abstract method makeSound (), Dog and Cat are two subclasses of Animal that implement makeSound () method.
And in the main class we create instances of Dog and Cat and call their makeSound()
Methods. Since Dog and Cat are subclasses of Animal we can use an Animal reference to point to either a Dog or a Cat object.
Output:
Woof! Meow!
Interface
In java, an interface is a group of abstract methods that is use to form a contract or a set of method so that every class that implements the interface must implement. It offers a means of defining a collection of associated methods that may be accessed by several unconnected classes.
The Java "interface" keyword is used to declare an interface. Interface does not coati any method implementation it is use to only contain method signature and constants. Instead, it is up to the classes that implement the interface to implement the methods.
Lets take an example:
public interface Shape { public double getArea(); public double getPerimeter(); }
As you can all see in the above example we have getArea and getPerimeter that is used to refer to two methods on the shape interface. Both of these methods must be implemented by any class that conforms to this interface.
A class must use the "implements" keyword and give an implementation for each of the interface's methods in order to implement an interface.
For example:
public class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2 * Math.PI * radius; } }
Limitation of interface
Java interfaces contain various restrictions that programmers should be aware of. The following are some interfaces' drawbacks:
- No implementation: Java interfaces can only provide method signatures; they cannot specify how they should be implemented. This implies that any class that implements the interface must offer a unique implementation of each of the interface's methods.
- Cannot contain fields: Except for public static final constants, interfaces are unable to contain fields. This can restrict the functionality of interfaces by preventing them from having instance variables.
- Cannot be instantiated: Interfaces cannot be directly instantiated, which prevents them from being utilised as stand-alone objects. Instead, a class must implement them, which can complicate the coding.
- Cannot change after compilation: After an interface has been compiled, it cannot be updated to modify its methods or signatures. As a result, it may be challenging to alter an interface over time and may cause compatibility problems.