Abstract Class Examples in Java –
Example 1:
abstract class Shape{ abstract void draw(); } class Rectangle extends Shape{ void draw(){ System.out.println("Drawing Rectangle"); } } class Traingle extends Shape{ void draw(){ System.out.println("Drawing Traingle"); } } class AbstractDemo{ public static void main(String args[]){ Shape s1 = new Rectangle(); s1.draw(); s1 = new Traingle(); s1.draw(); } }
Example 2:
A Car has Engine, wheels and many other parts. When we write all the properties of the Car, Engine, and wheel in a single class, it would look this way:
public class Car { int price; String name; String color; int engineCapacity; int engineHorsePower; String wheelName; int wheelPrice; void move(){ //move forward } void rotate(){ //Wheels method } void internalCombustion(){ //Engine Method } }
In the above example, the attributes of wheel and engine are added to the Car type. As per the programming, this will not create any kind of issues. But when it comes to maintenance of the application, this becomes more complex.
Abstraction has three advantages:
Applying the abstraction with composition,
the above example can be modified as given below:
public class Car { Engine engine = new Engine(); Wheel wheel = new Wheel(); int price; String name; String color; void move(){ //move forward } }
public class Engine { int engineCapacity; int engineHorsePower; void internalCombustion(){ //Engine Method } }
public class Wheel { String wheelName; int wheelPrice; void rotate(){ //Wheels method } }
You can see that the attributes and methods related to the Engine and Wheel are moved to the respective classes.
Engine and Wheel are referred from the Car type. When ever an instance of Car is created, both Engine and Wheel will be available for the Car and when there are changes to these Types(Engine and Wheel), changes will only be confined to these classes and will not affect the Car class.
Abstract Method :
Abstract methods are those which need to be implemented in subclass / child class. Abstract methods are only defined in superclass / parent class but with no body.
Syntax of Abstract Method:
abstract class clsName { // Variable declaration // Constructor // Method abstract rtype mthName(params); }
clsName is a valid identifier in java. It is a class name.
abstract is a keyword to define that method an abstract method.
rtype is return type of a method.
mthName is a method name and valid java identifier.
Example of an Abstract Method:
abstract class Shape { public static float pi = 3.142; protected float height; protected float width; abstract float area(); } class Square extends Shape { Square(float h, float w) { height = h; width = w; } float area() { return height * width; } } class Rectangle extends Shape { Rectangle(float h, float w) { height = h; width = w; } float area() { return height * width; } } class Circle extends Shape { float radius; Circle(float r) { radius = r; } float area() { return Shape.pi * radius *radius; } } class AbstractMethodDemo { public static void main(String args[]) { Square sObj = new Square(5,5); Rectangle rObj = new Rectangle(5,7); Circle cObj = new Circle(2); System.out.println("Area of square : " + sObj.area()); System.out.println("Area of rectangle : " + rObj.area()); System.out.println("Area of circle : " + cObj.area()); } }
Declaring a method as abstract has two results:
A child class that inherits an abstract method must override it. If they do not, they must be abstract,and any of their children must override it.
Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…