0% found this document useful (0 votes)
101 views

Abstraction in Java

Abstraction in Java allows hiding unnecessary details and showing only necessary information, improving code readability, maintainability, and reusability. Abstract classes define common behaviors without implementing them, while concrete subclasses implement the behaviors. This allows code to be independent of specific implementations. Examples of abstraction in Java include collections, I/O, and networking classes.

Uploaded by

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

Abstraction in Java

Abstraction in Java allows hiding unnecessary details and showing only necessary information, improving code readability, maintainability, and reusability. Abstract classes define common behaviors without implementing them, while concrete subclasses implement the behaviors. This allows code to be independent of specific implementations. Examples of abstraction in Java include collections, I/O, and networking classes.

Uploaded by

uday vaidya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Abstraction in Java

Abstraction is a powerful tool that allows us to hide unnecessary details and only
show the needed information. This makes our code more readable, maintainable,
and reusable.

Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The properties
and behaviors of an object differentiate it from other objects of similar type and
also help in classifying/grouping the objects.

Java Abstract classes and Java Abstract methods

● An abstract class is a class that is declared with an abstract keyword.


● An abstract method is a method that is declared without implementation.
● An abstract class may or may not have all abstract methods. Some of
them can be concrete methods
● A method-defined abstract must always be redefined in the subclass, thus
making overriding compulsory or making the subclass itself abstract.
● Any class that contains one or more abstract methods must also be
declared with an abstract keyword.
● There can be no object of an abstract class. That is, an abstract class can
not be directly instantiated with the new operator.
● An abstract class can have parameterized constructors and the default
constructor is always present in an abstract class.

Algorithm to implement abstraction in Java

● Determine the classes or interfaces that will be part of the abstraction.


● Create an abstract class or interface that defines the common behaviors
and properties of these classes.
● Define abstract methods within the abstract class or interface that do not
have any implementation details.
● Implement concrete classes that extend the abstract class or implement
the interface.
● Override the abstract methods in the concrete classes to provide their
specific implementations.
● Use the concrete classes to implement the program logic.
When to use abstract classes and abstract methods

There are situations in which we will want to define a superclass that declares
the structure of a given abstraction without providing a complete implementation
of every method. Sometimes we will want to create a superclass that only defines
a generalization form that will be shared by all of its subclasses, leaving it to each
subclass to fill in the details.

Abstract classes

Abstract classes are classes that cannot be instantiated directly. They must be
extended by other classes, which are called concrete classes. Concrete classes
implement the abstract methods of the abstract class.

Interfaces

Interfaces are similar to abstract classes, but they have one key difference:
interfaces cannot have any concrete methods. They can only have abstract
methods. Concrete classes must implement all of the abstract methods of the
interfaces they implement.

Example:

Let's consider a simple example of abstraction in Java:

Java
abstract class Animal {
abstract void makeSound();
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow!");
}
}

In this example, the Animal class is an abstract class. It has one abstract
method, makeSound(). The Dog and Cat classes are concrete classes that extend
the Animal class and implement the makeSound() method.

To use the Animal class, we can create a list of animals and add Dog and Cat
objects to it. We can then iterate over the list and call the makeSound() method
on each animal.

Java
List<Animal> animals = new ArrayList<>();
animals.add(new Dog());
animals.add(new Cat());

for (Animal animal : animals) {


animal.makeSound();
}

Output:

Woof!
Meow!

Benefits of abstraction

Abstraction has a number of benefits, including:

● Readability: Abstraction makes code more readable by hiding


unnecessary details. This makes it easier for other programmers to
understand and maintain the code.
● Maintainability: Abstraction makes code more maintainable by making it
easier to change without affecting other parts of the program. This is
because the abstract classes and interfaces define the public interface of
the code, while the concrete classes implement the details.
● Reusability: Abstraction makes code more reusable by allowing us to
create abstract classes and interfaces that can be implemented by
different concrete classes. This allows us to share code between different
parts of the program.

Examples of abstraction in Java

Abstraction is used in many parts of the Java standard library. For example:

● The java.io package contains abstract classes and interfaces for reading
and writing data. This allows us to write code that is independent of the
specific underlying I/O device.
● The java.util package contains abstract classes and interfaces for
collections, such as lists, sets, and maps. This allows us to write code that
is independent of the specific implementation of the collection.
● The java.net package contains abstract classes and interfaces for
networking. This allows us to write code that is independent of the specific
underlying network protocol.

Conclusion

Abstraction is a powerful tool that can be used to improve the readability,


maintainability, and reusability of Java code. By using abstract classes and
interfaces, we can hide unnecessary details and only show the needed
information. This makes our code easier to understand, maintain, and reuse.

Additional examples of abstraction in Java:

● The java.lang.Object class is the root class of all Java classes. It


defines a number of abstract methods, such as equals() and
hashCode(). These methods are implemented by concrete classes in a
way that is specific to the class.
● The java.lang.String class represents a string of characters. It
implements a number of abstract methods, such as charAt() and
substring(). These methods allow us to manipulate strings without
having to know the underlying implementation.
● The java.util.List interface represents a list of objects. It defines a
number of abstract methods, such as add() and get(). These methods
allow us to add and remove objects from a list without having to know the
underlying implementation.

Real-world examples of abstraction:

● A car is an example of abstraction in the real world. We can drive a car


without knowing how it works internally. We don't need to know how the
engine works or how the wheels turn. We just need to know how to start
the car, steer it, and brake.
● A computer is another example of abstraction in the real world. We can
use a computer without knowing how it works internally. We don't need to
know how the CPU works or how the

You might also like