🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment