Abstract Methods and classes in java

Today we are going to talk about Java abstract classes and methods. These are the two basic and important concepts in Java programming. A Java training institute covers these important concepts as a part of its course syllabus. You can learn it from there.


Abstract class:

To make a class of this type, it is declared as abstract. It may or may not have abstract methods in it. Such classes can be sub-classed but not instantiated.

Abstract method:

An abstract method is declared as follows:

abstract void shiftTo(double deltaX, double deltaY);

If a class has abstract methods, then the class itself should be declared as abstract, as in:

public abstract class GraphicObject {

// declare fields

// declare nonabstract methods

abstract void draw();


At the point when an abstract class is sub-classed, the subclass typically gives executions to all the abstract methods in its parent class. Be that as it may, on the off chance that it doesn’t, then the subclass should likewise be declared abstract.

Interface and Abstract class: A Comparison

Abstract classes are like interfaces. You can’t instantiate them, and they may contain a blend of methods declared with or without an implementation. In any case, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private methods. With interfaces, all fields are naturally public, static, and final, and all methods that you declare or define (as default methods) are public. What’s more, you can extend just a single class, regardless of whether it is unique, while you can execute any number of interfaces.

Which would it be advisable for you to utilize, interfaces or abstract classes?

-Consider utilizing abstract classes if any of these apply to your circumstance:

  • You need to share code among a few firmly related classes.

  • You expect that classes that develop your abstract class have numerous regular strategies or fields, or require get to modifiers other than public, (for example, protected and private).

  • You need to declare non-static or non-final fields. This empowers you to define methods that can get to and modify the state of the object which they have a place.

– Consider utilizing interfaces if any of these statements apply to your circumstance:

  • You expect that irrelevant classes would implement your interface. For instance, the interfaces Comparable and Cloneable are executed by numerous unrelated classes.

  • You need to determine the conduct of a specific data type, yet not worried about who executes its behavior.

  • You need to exploit multiple inheritance of type.

A case of a conceptual class in the JDK is AbstractMap, which is a piece of the Collections Framework. Its subclasses (which incorporate HashMap, TreeMap, and ConcurrentHashMap) share numerous methods (counting get, put, isEmpty, containsKey, and containsValue) that AbstractMap characterizes.

A case of a class in the JDK which actualizes the interfaces is HashMap which implements the interfaces Serializable, Cloneable, and Map<K, V>. By perusing this rundown of interfaces, you can induce that an occurrence of HashMap (paying little mind to the developer or organization who implemented the class) can be cloned, is serializable (which implies that it can be changed over into a byte stream; see the section Serializable Objects), and has the usefulness of a map. Moreover, the Map<K, V> interface has been upgraded with many default methods, for example, merge and forEach that older classes that have executed this interface don’t need to be defined.

Take note of that numerous software libraries utilize both abstract classes and interfaces; the HashMap class actualizes a few interfaces furthermore extends the abstract class AbstractMap.

This was about abstract methods and classes. Hope that you enjoyed reading it. For more Java oriented training, search for a best Java institute in Pune or best Java courses in Pune.