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.

Related Posts

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • 11 September

    RSA Encryption and Decryption in Java

    Table of ContentsIntroductionGenerate RSA key pairEncrypt a random textDecrypt the random textConclusion Introduction RSA is a short form for Rivest, Shamir, and Adleman, are the people who first publicly described it in 1977. It is an algorithm for asymmetric cryptography which involves the use of two keys. A public key, which can be known to […]

  • 10 September

    How to remove element from Arraylist in java while iterating

    Table of ContentsIntroductionUsing Collection’s removeIf() methodUsing ListIterator classUsing removeAll() methodUsing Java 8 Stream to filter List itemsConclusion Introduction In this tutorial, you will learn how to remove element from Arraylist in java while iterating using different implementations provided by Java. It is necessary to understand the right way to remove items from a List because […]

  • Watch New Release Movies Online Free Without Signing Up
    10 September

    Watch New Release Movies Online Free Without Signing Up: Top 22 Websites

    Table of ContentsWatch New Release Movies Online Free Without Signing UpWatch New Release Movies Online Free Without Signing Up: Best Websites123MoviesFMoviesWatchFreeYes MoviesYifi MoviesWatch Movie StreamSolar MoviesPut LockerSony CrackleMovies JoyYo MoviesPopcorn FlixCracklePluto TVSome other Platforms to Watch New Release Movies Online Free Without Signing Up It is Saturday night again, and are you tired of signing […]

  • 10 September

    Change the font size of title in Matplotlib

    In Python, there are a lot of libraries that are used for data visualization. One of the libraries is the Matplotlib library. The Matplotlib library is a very good library when it comes to data visualisation in python. With the help of this library, many types of graphs and visual representations of the data can […]

  • Free movie apps for android
    08 September

    22 Best Free Movie Apps For Android

    Table of ContentsThe Most Promising Free Movie Apps for AndroidPopcornflixSony CrackleFreeFlix HQVuduTubi TVFilm RiseUKMovNowCrunchyrollCyberflix TVYidioPluto TVTea TVPlexMovies AnywhereOld MoviesCinema HDVideoMixKodiSnagfilmsIMDB OriginalsFree Movie Tube App for AndroidMegabox HDFrequently Asked Questions About Free Movie Apps for AndroidDo there exist free movie apps for Android?Which are the best free movie apps for Android?Is it safe to use the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.