0% found this document useful (0 votes)
8 views9 pages

Java Abstract Classes and Methods Explained

The document discusses Java abstract classes and methods. It explains that abstract classes cannot be instantiated and defines abstract methods as methods without a body. It provides examples of creating abstract classes and methods and inheriting from abstract classes. The document also discusses how abstraction in Java allows hiding unnecessary details and showing only needed information.

Uploaded by

Ven Dicator
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Java Abstract Classes and Methods Explained

The document discusses Java abstract classes and methods. It explains that abstract classes cannot be instantiated and defines abstract methods as methods without a body. It provides examples of creating abstract classes and methods and inheriting from abstract classes. The document also discusses how abstraction in Java allows hiding unnecessary details and showing only needed information.

Uploaded by

Ven Dicator
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 class can have both the regular methods and abstract methods.
For example,

abstract class Language {

// abstract method
abstract void method1();

// regular method
void method2() {
[Link]("This is regular method");
}
}

To know about the non-abstract methods, visit Java 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();
}

Example: Java Abstract Class and Method


Though abstract classes cannot be instantiated, we can create subclasses
from it. We can then access members of the abstract class using the object of
the subclass. For example,

abstract class Language {

// method of abstract class


public void display() {
[Link]("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
[Link]();
}
}
Run Code

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,

[Link]();
Here, obj is the object of the child class Main . We are calling the method of the
abstract class using the object obj .

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


[Link]("I can eat.");
}
}

class Dog extends Animal {

// provide implementation of abstract method


public void makeSound() {
[Link]("Bark bark");
}
}

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

// create an object of Dog class


Dog d1 = new Dog();

[Link]();
[Link]();
}
}
Run Code

Output

Bark bark
I can eat.

In the above example, we have created an abstract class Animal . The class
contains an abstract method makeSound() and a non-abstract method eat() .

We have inherited a subclass Dog from the superclass Animal . Here, the
subclass Dog provides the implementation for the abstract method makeSound() .

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,

abstract class Animal {


Animal() {
….
}
}

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

Example 3: Java Abstraction


abstract class MotorBike {
abstract void brake();
}

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
[Link]("SportsBike Brake");
}
}

class MountainBike extends MotorBike {

// implementation of abstract method


public void brake() {
[Link]("MountainBike Brake");
}
}

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

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

So, the implementation of brake() in MotorBike is kept hidden.


Here, MountainBike makes its own implementation
of brake() and SportsBike makes its own implementation of brake() .

Note: We can also use interfaces to achieve abstraction in Java. To learn


more, visit Java Interface.

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,
[Link]();

REFERENCES: [Link]

Common questions

Powered by AI

Abstract classes in Java that include both regular and abstract methods simplify code maintenance by enabling a centralized blueprint with default implementations. Regular methods provide a default behavior that can be shared across subclasses without redefining, while abstract methods define placeholders for methods that must be customized in each subclass. This structure prevents redundancy, ensures consistency in method signatures, and simplifies updates since changes to the base methods only occur in one place, the abstract class .

A subclass must provide an implementation for all abstract methods defined in an abstract superclass to provide specific functionality defined in the abstract method signature. If it doesn't provide the implementation, the subclass itself must be declared abstract. This is because the abstract method indicates an incomplete method in the design, which must be completed in a subclass to be instantiated .

Java's abstract classes and methods manage complexity by abstracting common design patterns, allowing developers to define generic operations in abstract classes without worrying about specific implementations until derived in subclasses. This separates the conceptual framework (operations) from concrete details, enabling developers to focus on overarching architecture and system design complexity, instead of minutiae. As abstract methods have no body, they act as enforced templates ensuring cohesiveness and consistency across subclass implementations, thus reducing code duplication and improving maintainability .

Abstract classes offer the advantage of enforced method implementation in subclasses through abstract methods. By requiring subclasses to provide specific implementations for abstract methods, abstract classes ensure that any concrete subclass adheres to a defined contract, maintaining consistent behavior across different subclasses while allowing customized implementations. This mechanism supports design integrity and extensibility, providing developers with the means to enforce necessary functionality while allowing flexibility in implementation details .

Attempting to instantiate an abstract class directly would result in a compilation error because abstract classes are incomplete by design due to their abstract methods lacking implementations. This rule exists to enforce the principle of abstraction, ensuring that specific subclasses provide concrete implementations of all abstract methods before an instance is created. Thus, it aligns with the incomplete nature of abstract classes and their purpose to serve as a blueprint for subclasses .

It is necessary for a subclass to override all abstract methods from an abstract superclass because these methods define operations that are expected to be implemented for the subclass to be instantiated. Without implementations, the subclass would lack concrete behavior for those operations. However, if the subclass itself is declared abstract, it is not required to override the abstract methods, as it is still considered an incomplete design, which can be further extended by other subclasses .

In Java, the "super" keyword is used within a subclass constructor to call the constructor of its superclass, including abstract classes. This is essential for the initialization of the inherited attributes and methods. The call using "super" must be the first statement in the subclass constructor. This mechanism allows the subclass to utilize the setup defined in the abstract class’s constructor while providing additional functionality or initializations specific to the subclass .

Accessing static attributes and methods of abstract classes using a class reference contributes to java's abstraction by allowing shared functionality or state that does not pertain to a specific instance. This aligns with the abstraction principle by focusing on the class-level behavior distinct from individual objects, enabling both a means to define utility methods and standardize constants across associated subclasses. This approach enhances modularity and encapsulates class-level logic separate from object-specific data, aligning with abstraction principles .

Interfaces in Java can be used to achieve abstraction by defining a contract that specifies methods without implementations, similar to abstract methods in abstract classes. However, unlike abstract classes, interfaces can be implemented by any class regardless of where they are in the inheritance hierarchy, providing greater flexibility. Additionally, interfaces support multiple inheritance, unlike abstract classes which only allow single inheritance. This allows interfaces to complement or replace abstract classes, depending on the need for flexibility and complexity management in object-oriented design .

Abstraction in Java allows hiding the complex implementation details while only exposing the functionality to the user. For instance, in a real-world scenario like a motorbike brake, abstraction is applied by letting the user know that pressing the brake will stop the motorbike. However, the underlying mechanism of how the brake works is hidden. This allows manufacturers the flexibility to create different implementations of brakes for various motorbike models while ensuring the same functionality for the user .

You might also like