Abstract class in java

An abstract class is the class which is declared abstract and can have abstract or non abstract methods. An abstract class can not be instantiated. It can be extended by subclass to implement abstract methods and either use or override concrete methods.

Abstract method in java

Abstract method is the method which do not have implementation i.e. it does not have anybody.

When do you need abstract class in java?

Let’s understand it with the help of the example. You have Shape class and it has some methods such as draw(), calcualteArea() etc. Drawing a shape is common for all the shapes but calculateArea() is different for each shape.

For example:

For Rectangle, it will be calculated as length * breadth and for circle, it will be pi* radius^2.

So with above behavior, we will implement draw() method in shape class and will create calcualteArea() as abstract method.So if you have some common behaviors and some specific behavior, make your class abstract.

Let’s create a abstract class as Shape and Rectangle, Circle class as concrete classes.

Example of Abstract class in java:

Create a main class named “AbstractClassMain”
============================
Draw method in shape class
Area of rectangle is 12.0
============================
Draw method in shape class
Area of circle is 28.259999999999998
============================

Let’s consider some cases to understand abstract class in java better:
Case 1: Is is possible to have zero abstract method in class but still you can declare class as abstract.
Answer:
Yes, It is possible to declare class as abstract even if you don’t have any abstract method in the class.
For example:

public void draw() { System.out.println(“Draw method in shape class”); } }

Case 2: Class contains abstract method but is it possible to not declare class as Abstract?
Answer:
No, It is mandatory to declare class as Abstract but it contain abstract method.

You will get compilation errors:

  • At line 1 : “The type Shape must be an abstract class to define abstract methods”
  • At line 7: “The abstract method calculateArea in type Shape can only be defined by an abstract class”

If you have abstract method in class, class must be declared as abstract class in java

Case 3: Can you declare class as final and abstract at the same time?

Answer:

No, you can not declare class as final and abstract at the same time. Final means No other class can extend that class and Abstract means Class must be extended by any other class.

Case 4: Can you instantiate abstract class in java?

Answer:

No, you can not instantiate abstract class.

You can not instantiate abstract class in java

Case 5: If you are extending an abstract class in java, do you need to implement all the abstract methods of that class?

Answer:

Yes, you need to implement all the abstract method of superclass unless and until subclass is also an abstract class in java.

That’s all about abstract class in java.

Was this post helpful?

Related Posts

  • 24 September

    Display Negative Number in Parentheses in Java

    In this post, we will see how to display negative number in Parentheses in Java. We can use pattern #,##0.00;(#,##0.00) with DecimalFormat‘s format() to display negative number in Parenthesis in Java. [crayon-632f041731de8747840343/] Output [crayon-632f041731def956035630/] If you don’t need decimal places, you can change to pattern to: [crayon-632f041731df0990809281/] Let’s understand meaning of pattern #,##0.00;(#,##0.00): First part […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 24 September

    Get List of Month Names in Java

    Table of ContentsUsing DateFormatSymbols’s getMonth() methodUsing DateFormatSymbols’s getShortMonths() method [ For short names]Using DateFormatSymbols’s getMonth() with Locale In this post, we will see how to get list of months name in Java. Using DateFormatSymbols’s getMonth() method We can use DateFormatSymbols's getMonth() to get list of months in Java. It will provide complete month names like […]

  • 24 September

    Escape Percent Sign in printf Method in C++

    In this post, we will see how to escape percent sign in printf Method in C++. Escape percent sign in Printf Method in C++ printf() method uses percent sign(%) as prefix of format specifier. For example: To use number in prinf() method, we use %d, but what if you actually want to use percent sign […]

  • 23 September

    Convert 0 to 1 and 1 to 0 in Python

    Table of ContentsWays to convert 0 to 1 and 1 to 0 in PythonUsing SubtractionUsing XORUsing not operatorUsing lambda expressionUsing addition and remainder operatorConvert 1 to 0 and 0 to 1 in listConvert 1 to 0 and 0 to 1 in numpy arrayUsing where with bitwise xorUsing bitwise operators In this post, we will see […]

  • 23 September

    TypeError: Object of Type Datetime Is Not Json Serializable in Python

    Table of ContentsFix the object of type datetime is not JSON serializable exception in PythonUsing the default parameter in the json.dumps() functionUsing the cls parameter in the json.dumps() functionUsing the str functionConclusion In Python, the datetime library allows us to create objects of the datetime class that can store and process date and time values […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.