Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
62 views

Java Abstract Class and Method (With Example)

1. The document discusses Java abstract classes and methods. It explains that abstract classes cannot be instantiated and must contain at least one abstract method. 2. Subclasses of an abstract class must implement all abstract methods declared in the abstract class. 3. The document provides an example of an Animal abstract class with a makeSound abstract method. A Dog subclass implements makeSound and can then be instantiated.

Uploaded by

Avanish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Java Abstract Class and Method (With Example)

1. The document discusses Java abstract classes and methods. It explains that abstract classes cannot be instantiated and must contain at least one abstract method. 2. Subclasses of an abstract class must implement all abstract methods declared in the abstract class. 3. The document provides an example of an Animal abstract class with a makeSound abstract method. A Dog subclass implements makeSound and can then be instantiated.

Uploaded by

Avanish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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
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)

Java Abstract Class and Abstract


Methods
In this tutorial, we will learn about Java abstract classes and methods with the help
of examples. We will also learn about abstraction in Java.

Java Abstract Class


The abstract class in Java cannot be instantiated (we cannot create objects of
abstract classes). We use the abstract keyword to declare an abstract class. For
example,

// create an abstract class


abstract class Language {
// fields and methods
}
...

// try to create an object Language


// throws an error
Language obj = new Language();

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");
}
}

To know about the non-abstract methods, visit Java methods (/java-


programming/methods). Here, we will learn about abstract methods.

Java Abstract Method


A method that doesn't have its body is known as an abstract method. We use the
same abstract keyword to create abstract methods. For example,

abstract void display();

Here, display() is an abstract method. The body of display() is replaced by ; .

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

abstract class Language {

// method of abstract class


public void display() {
System.out.println("This is Java Programming");
}
}

class Main extends Language {

public static void main(String[] args) {

// create an object of Main


Main obj = new Main();

// access method of abstract class


// using object of Main class
obj.display();
}
}

Run Code (/java-programming/online-compiler)

Output

This is Java programming

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,

abstract class Animal {


abstract void makeSound();

public void eat() {


System.out.println("I can eat.");
}
}

class Dog extends Animal {

// provide implementation of abstract method


public void makeSound() {
System.out.println("Bark bark");
}
}

class Main {
public static void main(String[] args) {

// create an object of Dog class


Dog d1 = new Dog();

d1.makeSound();
d1.eat();
}
}

Run Code (/java-programming/online-compiler)

Output

Bark bark Offer


I can eat. 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
In the
Thank
60% youabove example,
for printing we have
our content created an abstract class check. back
Animal
sale__top-banner-programiz-web__feb__50)
at www.domain-name.com. Please The class
soon for new
Access
contents.
OFF
contains an All abstract method makeSound() and a non-abstract method eat() .
Courses
For Life
Try PRO
We (https://programiz.pro/learn/master-java?utm_source=right-floating-
have inherited a subclass Dog from the superclass Animal . Here, the subclass
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
Dog provides the implementation for the abstract method makeSound() .
www.domain-name.com

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 .

Accesses Constructor of Abstract Classes

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.

Abstraction is an important concept of object-oriented programming that allows us


to hide unnecessary details and only show the needed information.

This allows us to manage complexity by omitting or hiding details with a simpler,


higher-level idea.

A practical example of abstraction can be motorbike brakes. We know what brake


does. When we apply the brake, the motorbike will stop. However, the working of the
brake is kept hidden from us.

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.

Let's take an example that helps us to better understand Java abstraction.


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% you for3: Javaour
printing Abstraction
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
abstract
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
class MotorBike {
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
abstract void brake();
}
www.domain-name.com

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("SportsBike Brake");
}
}

class MountainBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("MountainBike Brake");
}
}

class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
s1.brake();
}
}

Run Code (/java-programming/online-compiler)

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() .

The brake() method cannot be implemented inside MotorBike . It is because every


bike has different implementation of brakes. So, all the subclasses of MotorBike

would have different implementation of brake() .


Offer
So, the implementation
Ends of brake() in MotorBike is kept hidden.
(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% youMountainBike makes
for printing our itsatown
content implementation of Please
brake()
sale__top-banner-programiz-web__feb__50)
www.domain-name.com. andback
check SportsBike
soon for new
Access
contents.
OFF
makes its own Allimplementation of brake() .
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
Note:
(/) block&utm_campaign=programiz&utm_medium=referral)
We can also use interfaces to achieve abstraction in Java. To learn
www.domain-name.com
more, visit Java Interface (/java-programming/interfaces).

Key Points to Remember


We use the abstract keyword to create abstract classes and methods.

An abstract method doesn't have any implementation (method body).

A class containing abstract methods should also be abstract.

We cannot create objects of an abstract class.

To implement features of an abstract class, we inherit subclasses from it and


create objects of the subclass.

A subclass must override all abstract methods of an abstract class. However, if


the subclass is declared abstract, it's not mandatory to override abstract
methods.

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

Share on: 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 (https://www.facebook.com/sharer/sharer.php?
at www.domain-name.com. (https://twitter.com/intent/tweet?
60% you for printing our content
sale__top-banner-programiz-web__feb__50)
Please check back soon for new
Access
u=https://www.programiz.com/java- text=Check%20this%20amazing%20
contents.
OFF All
programming/abstract-classes-methods) programming/abstract-classes-meth
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)
Did you find this article helpful?
www.domain-name.com

Related Tutorials

Java Tutorial

Java Method Overriding

(/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 InputStream Class

(/java-programming/inputstream)

You might also like