How do you achieve abstraction in java




















Let's understand the abstraction with the help of a real-world example. The best example of abstraction is a car. When we derive a car, we do not know how is the car moving or how internal components are working? But we know how to derive a car.

It means it is not necessary to know how the car is working, but it is important how to derive a car. The same is an abstraction. The same principle as we have explained in the above example also applied in Java programming and any OOPs.

In the language of programming, the code implementation is hidden from the user and only the necessary functionality is shown or provided to the user. We can achieve abstraction in two ways:. Abstract classes are the same as normal Java classes the difference is only that an abstract class uses abstract keyword while the normal Java class does not use.

We use the abstract keyword before the class name to declare the class as abstract. Remember that, we cannot instantiate create an object an abstract class. An abstract class contains abstract methods as well as concrete methods. If we want to use an abstract class, we have to inherit it from the base class.

If the class does not have the implementation of all the methods of the interface, we should declare the class as abstract. It provides complete abstraction. It means that fields are public static and final by default and methods are empty. It is used to define generic types of behavior at the top of an OOPs class hierarchy and use its subclasses to provide implementation details of the abstract class.

Let's see an example of an abstract class. In Java, an interface is similar to Java classes. The difference is only that an interface contains empty methods methods that do not have method implementation and variables. In other words, it is a collection of abstract methods the method that does not have a method body and static constants. The important point about an interface is that each method is public and abstract and does not contain any constructor.

Along with the abstraction, it also helps to achieve multiple inheritance. The implementation of these methods provided by the clients when they implement the interface. Separating interface from implementation is one way to achieve abstraction. The Collection framework is an excellent example of it. To use an interface in a class, Java provides a keyword called implements.

String color;. Override double area. Override public String toString. Output Shape constructor called Circle constructor called Shape constructor called Rectangle constructor called Circle color is Redand area is : Next Abstract Classes in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide.

Load Comments. What's New. Most popular in Java. More related articles in Java. We use cookies to ensure you have the best browsing experience on our website. We will cover the abstract classes later. They provide total abstraction, which means that all the methods are empty and field variables are public static and final by default. Interfaces serve several features:. Abstract classes are just like a normal Java class, except they use the keyword abstract in front of class declaration and method declaration.

Abstract classes are created using the abstract keyword and they may or may not have method implementation. If a method is declared abstract, then its implementation has to be provided by the class extending the abstract class.

We can have an abstract class without the abstract method. They can also contain final methods. A class that extends the abstract class is a child class for that abstract class and has to provide an implementation for the abstract method declared in the abstract class.

Now, one question must have come up: Why do we have interfaces and abstract classes? There are a few key reasons worth noting:. Interfaces are implicitly abstract and cannot have implementations. Abstract classes can have method implementations. Variables of interfaces are final by default.

Abstract classes may or may not have a final variable. Interface methods are public whereas abstract classes can provide all types of access modifiers for its members, i. An interface can extend only while classes can implement multiple interfaces and extend one class only.

Thus, both abstract classes and interfaces are used to achieve abstraction and both have their own importance while designing a Java solution, but the most preferable choice for most developers is to use interfaces as they provide complete abstraction.

I hope this article helps to clear up any questions or doubts regarding abstraction.



0コメント

  • 1000 / 1000