What is Abstract Class? What are Abstract Methods in Java? Abstraction Code Example Abstract Class in Java Final Keyword in Java

Consider the following class hierarchy consisting of a Shape class which is inherited by three classes Rectangle, Circle, and Triangle. The Shape class is created to save on common attributes and methods shared by the three classes Rectangle, Circle, and Triangle. calculateArea() is one such method shared by all three child classes and present in Shape class.

Now, assume you write code to create objects for the classes depicted above. Let’s observe how these objects will look in a practical world. An object of the class rectangle will give a rectangle, a shape we so commonly observed in everyday life.

An object of the class triangle will give a triangle, again a common everyday shape.

But what would an object of Class Shape look like in a practical world ??

If you observe the Shape class serves in our goal of achieving inheritance and polymorphism. But it was not built to be instantiated. Such classes can be labelled Abstract. An abstract java class cannot be instantiated. Syntax: It is possible that you DO NOT label Shape class as Abstract and then instantiate it. But such object will have no use in your code and will open a room for potential errors. Hence this is not desirable. As we all know, the formula for calculating area for rectangle, circle, & triangle is different. The calculateArea() method will have to be overridden by the inheriting classes. It makes no sense defining it in the Shape class, but we need to make sure that all the inheriting classes do have the method. Such methods can be labeled abstract. Syntax: For an abstract method, no implementation is required. Only the signature of the method is defined.

Abstraction Code Example

Abstract Class in Java: Important Points

An abstract class may also have concrete (complete) methods. For design purpose, a class can be declared abstract even if it does not contain any abstract methods Reference of an abstract class can point to objects of its sub-classes thereby achieving run-time polymorphism Ex: Shape obj = new Rectangle(); A class must be compulsorily labelled abstract, if it has one or more abstract methods.

Final Keyword in Java

The final modifier applies to classes, methods, and variables. The meaning of final varies from context to context, but the essential idea is the same.

A final class cannot be inherited A final variable becomes a constant and its value cannot be changed. A final method cannot be overridden. This is done for security reasons, and these methods are used for optimization.

Example :- To learn abstract & final keywords Step 1) Copy the following code into an Editor. Step 2) Save , Compile & Run the code. Step 3) Error =? The abstract method is not implemented int the class Rectangle. To fix the issue uncomment line #15. Step 4) Uncomment line # 13 . Save & Compile the code. Step 5) Error = ? variable b is final Rules of Abstract Method

Abstract methods do not have an implementation; it only has method signature If a class is using an abstract method, they must be declared abstract. The opposite cannot be true. This means that an abstract class does not necessarily have an abstract method. If a regular class extends an abstract class, then that class must implement all the abstract methods of the abstract parent