In this article, we will understand the abstract keyword in Java with examples.
The abstract keyword is used to declare a class or a method as abstract.
An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
The below code example shows how to use an abstract keyword is used to declare a class as an abstract class or abstract method in Java:
Let's discuss what is an abstract class and abstract method with examples.
Abstract Class
An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.Use abstract keyword to declare an abstract class and follow the below rules to declare a class as an abstract class:
- Abstract classes cannot be instantiated directly.
- Abstract classes may not be marked as private or final.
- An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
- The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
Read more about abstraction at Abstraction in Java with Example
Read more about Abstract class at What is Abstract Class and Abstract Method in Java with Examples
Abstract Class Example 1
In this example, let's create an abstract Animals class with an abstract sound() method, the Dog and Cat classes extend Animals class and provide their own implementation to sound() method.abstract class Animals{
private String name;
// All kind of animals eat food to make this common to all animals
public void eat(){
System.out.println(" Eating ..........");
}
// The animals make different sounds. They will provide their own implementation
abstract void sound();
}
class Cat extends Animals{
@Override
void sound() {
System.out.println("Meoww Meoww ........");
}
}
class Dog extends Animals {
@Override
void sound() {
System.out.println("Woof Woof ........");
}
}
public class AbstractClassCompleteExample {
public static void main(String[] args) {
Animals animals = new Cat();
animals.sound();
animals = new Dog();
animals.sound();
}
}
Abstract Class Example 2
Let's create an abstract DrawObject class with an abstract draw() and resize() methods.Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw() and resize() methods:
package com.javaguides.corejava.keywords.abstractkeyword;
public class AbstractKeywordShapesExample {
public static void main(final String[] args) {
final DrawObject circle = new Circle(10, 20);
final DrawObject line = new Line(10, 20);
final DrawObject reactangle = new Reactangle(10, 20);
circle.draw();
line.draw();
reactangle.draw();
}
}
abstract class DrawObject {
int x, y;
public DrawObject(final int x, final int y) {
super();
this.x = x;
this.y = y;
}
void moveTo(final int newX, final int newY) {
System.out.println(" move GraphicObjects points :: " + newX + " " + newY);
}
abstract void draw();
abstract void resize();
}
class Rectangle extends DrawObject {
public Rectangle(final int x, final int y) {
super(x, y);
}
@Override
void draw() {
System.out.println("Draw Reactangle");
}
@Override
void resize() {
// TODO Auto-generated method stub
}
}
class Line extends DrawObject {
public Line(final int x, final int y) {
super(x, y);
}
@Override
void draw() {
System.out.println("Draw Line");
}
@Override
void resize() {
}
}
class Circle extends DrawObject {
public Circle(final int x, final int y) {
super(x, y);
}
@Override
void draw() {
System.out.println("Draw Circle");
}
@Override
void resize() {
}
}
Abstract Method
An abstract method is a method that is declared without an implementation.Abstract Method Definition Rules:
- Abstract methods may only be defined in abstract classes.
- Abstract methods may not be declared private or final.
- Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
- Implementing an abstract method in a subclass follows the same rules for overriding a method.
Example:
The below AbstractShape class contains two abstract methods - draw() and moveTo();
abstract class AbstractShape{
abstract void draw();
abstract void moveTo(double deltaX, double deltaY);
}
Conclusion
In this quick article, we have seen how to use an abstract keyword is used to declare a class as an abstract class or abstract method in Java with examples.
Comments
Post a Comment
Leave Comment