Java Abstract Class and Method (With Example)
Java Abstract Class and Method (With Example)
Thank
60% you for printing our content
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please check back soon for new
Access
contents.
OFF All
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND42JUCABIP5QNFT7LYKQNCAS
segment=placement:wwwprogramizcom;) ADS VIA CARBON
(HTTP://CARBONADS.NET/?UTM_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
An abstract Offer
class can have both the regular methods and abstract methods. For
example, Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please check back soon for new
abstract class
AccessLanguage {
contents.
OFF All
Courses
// abstract method
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
abstract void method1();
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
// regular method
www.domain-name.com
void method2() {
System.out.println("This is regular method");
}
}
If a class contains an abstract method, then the class should be declared abstract.
Otherwise, it will generate an error. For example,
// error
// class should be abstract
class Language {
// abstract method
abstract void method1();
}
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example:
Thank
60% Java Abstract
you for printing Class
our content and Method
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please check back soon for new
Access
contents.
OFF All
Though abstract classes cannot be instantiated, we can create subclasses from it.
Courses
For Life
We (https://programiz.pro/learn/master-java?utm_source=right-floating-
can then
Try PRO access members of the abstract class using the object of the subclass.
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
For example,
www.domain-name.com
Output
In the above example, we have created an abstract class named Language . The
class contains a regular method display() .
We have created the Main class that inherits the abstract class. Notice the
statement,
obj.display();
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Here,
Thank
60% youobj is the object
for printing of theatchild
our content class Main . We arePlease
calling
sale__top-banner-programiz-web__feb__50)
www.domain-name.com. the back
check method
soonof the
for new
Access
contents.
OFF
abstract class Allusing the object obj .
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Implementing Abstract Methods
If the abstract class includes any abstract method, then all the child classes
inherited from the abstract superclass must provide the implementation of the
abstract method. For example,
class Main {
public static void main(String[] args) {
d1.makeSound();
d1.eat();
}
}
Output
We then used the object d1 of the Dog class to call methods makeSound() and
eat() .
Note: If the Dog class doesn't provide the implementation of the abstract
method makeSound() , Dog should also be declared as abstract. This is
because the subclass Dog inherits makeSound() from Animal .
An abstract class can have constructors like the regular class. And, we can access
the constructor of an abstract class from the subclass using the super keyword. For
example,
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please check back soon for new
abstract classAccessAnimal {
contents.
OFF Animal()All{
…. Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO }
for} FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
class Dog extends Animal {
Dog() {
super();
...
}
}
Here, we have used the super() inside the constructor of Dog to access the
constructor of the Animal .
Note that the super should always be the first statement of the subclass
constructor. Visit Java super keyword (/java-programming/super-keyword) to learn
more.
Java Abstraction
The major use of abstract classes and methods is to achieve abstraction in Java.
The major advantage of hiding the working of the brake is that now the
manufacturer can implement brake differently for different motorbikes, however,
what brake does will be the same.
class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
s1.brake();
}
}
Output:
MountainBike Brake
SportsBike Brake
In the above example, we have created an abstract super class MotorBike . The
superclass MotorBike has an abstract method brake() .
We can access the static attributes and methods of an abstract class using the
reference of the abstract class. For example,
Animal.staticMethod();
Next Tutorial:
(/java-programming/interfaces)
Java Interfaces
Previous Tutorial:
(/java-programming/super-keyword)
Java super Keyword
Related Tutorials
Java Tutorial
(/java-programming/method-overriding)
Java Tutorial
Java Interface
Offer
(/java-programming/interfaces)
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content
Java Tutorial Access
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please check back soon for new
contents.
OFF All
Java OutputStream
Courses Class
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(/java-programming/outputstream)
Java Tutorial
(/java-programming/inputstream)