Abstract Classes
Abstract class is a class which is declared using abstract keyword and can have one or more abstract method.
Syntax of an Abstract Class:
package com.meritmix.blog.java;
public abstract class AbstractClassWithAbstractMethods {
public abstract void abstractMethod();
}
Following are the properties of abstract class---
1- You can not create instance of an abstract class.
package com.meritmix.blog.java;
public abstract class AbstractClassWithAbstractMethods {
public abstract void abstractMethod();
public static void main(String[] args) {
AbstractClassWithAbstractMethods obj = new AbstractClassWithAbstractMethods();
// it will give compilation error 'Cannot instantiate the type AbstractClassWithAbstractMethods'
}
}
2- If a class has any abstract method then that class must be declared as abstract class but if a class is declared as an abstract class then its not necessary to have one or more abstract methods.
Abstract Class without abstract methods:
package com.meritmix.blog.java;
public abstract class AbstractClassWithoutAbstractMethod {
public void concreteMethod1() {
System.out.println("in concrete method 1");
}
public void concreteMethod2() {
System.out.println("in concrete method 2");
}
}
3- Abstract class can have combination of abstract and concrete methods, it doesn't provide 100% abstraction. We use interfaces where we need 100% abstraction.
Abstract Class with abstract methods:
package com.meritmix.blog.java;
public abstract class AbstractClassWithAbstractMethods {
public abstract void abstractMethod(); // abstract Method
public void concreteMethod() { // concrete Method
System.out.println("in concrete method");
}
}
4- A Class can be either abstract or final, it cannot be both.
package com.meritmix.blog.java;
public final abstract class AbstractClassWithAbstractMethods {
//Compilation error "The class AbstractClassWithAbstractMethods can be either abstract or final, not both"
public abstract void abstractMethod();
public void concreteMethod() {
System.out.println("in concrete method");
}
}
Reason:
When we make a Class final that mean this class cannot be extended and when we make a Class abstract then it is assumed to be extended so that its abstract methods could be implemented. So when we make a class both final and abstract then both these concepts contradict each other.
What is Abstract Method?
A method which has only declaration but doesn't have body is called abstract method. And in abstract classes we need to use abstract keyword in method declaration.
Example:
abstract public void myMethod();
What is concrete method?
A method which has method body along with its declaration is called concrete method.
Example
public void myMethod(){
system.out.println("its concrete method");
}
Real life examples of abstract Classes:
Abstract means something not concrete or complete i.e. it cannot come into actual existence, so is our abstract class, as we have learnt that we can not create Object of abstract Class.
So there must be an concrete Class which implements all the abstract methods on the abstract Class.
In addition to implementing abstract methods on abstract Parent Class, Concrete Class can have its own method.
So lets take example of Class 'Shape'. Shape Is an abstract class because even in real world when you say Shape, you don't know what kind of shape it is.
Shape.java
package com.meritmix.blog.java.abstraction;
public abstract class Shape {
private String name;
public Shape(String name) {
this.name = name;
}
public abstract int getArea();
public abstract int getPerimeter();
public String getName(){
return name;
}
}
Now comes the concrete Classes like Rectangle or Circle. When we say Rectangle or Circle then we know how they look like and what are their properties.
Rectangle.java
package com.meritmix.blog.java.abstraction;
public abstract class Shape {
private String name;
public Shape(String name) {
this.name = name;
}
public abstract int getArea();
public abstract int getPerimeter();
public String getName(){
return name;
}
}
And
Circle.java
package com.meritmix.blog.java.abstraction;
public class Circle extends Shape {
private int radius;
public Circle(String name, int radius) {
super(name);
this.radius = radius;
}
@Override
public int getArea() {
return (22/7)*radius*radius;
}
@Override
public int getPerimeter() {
return 2*(22/7)*radius;
}
}
Now create a Test Class to test the classes.
TestClass.java
package com.meritmix.blog.java.abstraction;
import java.util.ArrayList;
import java.util.List;
public class TestClass {
public static void main(String[] args) {
Shape circle = new Circle("Circle", 5);
Shape rectable = new Rectangle("Rectangle", 5, 3);
List shapes = new ArrayList<>();
shapes.add(circle);
shapes.add(rectable);
for (Shape shape : shapes) {
System.out.println("Shape's name: "+ shape.getName()+", Area: "+shape.getArea()+" perimeter: "+ shape.getPerimeter());
}
}
}
Output:
Shape's name: Circle, Area: 75 perimeter: 30
Shape's name: Rectangle, Area: 15 perimeter: 16

No comments:
Post a Comment