- Create abstract method java
- Abstract Methods and Classes
- Abstract Classes Compared to Interfaces
- An Abstract Class Example
- When an Abstract Class Implements an Interface
- Class Members
- Abstract method in Java with examples
- Rules of Abstract Method
- Example 1: abstract method in an abstract class
- Example 2: abstract method in interface
- Top Related Articles:
- About the Author
- Comments
Create abstract method java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Abstract Methods and Classes
An abstract class is a class that is declared abstract it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract , as in:
public abstract class GraphicObject < // declare fields // declare nonabstract methods abstract void draw(); >
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .
Note: Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)
Abstract Classes Compared to Interfaces
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Which should you use, abstract classes or interfaces?
- Consider using abstract classes if any of these statements apply to your situation:
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
- You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
- You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
- You want to take advantage of multiple inheritance of type.
An example of an abstract class in the JDK is AbstractMap , which is part of the Collections Framework. Its subclasses (which include HashMap , TreeMap , and ConcurrentHashMap ) share many methods (including get , put , isEmpty , containsKey , and containsValue ) that AbstractMap defines.
An example of a class in the JDK that implements several interfaces is HashMap , which implements the interfaces Serializable , Cloneable , and Map . By reading this list of interfaces, you can infer that an instance of HashMap (regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream; see the section Serializable Objects), and has the functionality of a map. In addition, the Map interface has been enhanced with many default methods such as merge and forEach that older classes that have implemented this interface do not have to define.
Note that many software libraries use both abstract classes and interfaces; the HashMap class implements several interfaces and also extends the abstract class AbstractMap .
An Abstract Class Example
In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObject s must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject ) as shown in the following figure.
Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject
First, you declare an abstract class, GraphicObject , to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize , that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:
abstract class GraphicObject < int x, y; . void moveTo(int newX, int newY) < . >abstract void draw(); abstract void resize(); >
Each nonabstract subclass of GraphicObject , such as Circle and Rectangle , must provide implementations for the draw and resize methods:
class Circle extends GraphicObject < void draw() < . >void resize() < . >> class Rectangle extends GraphicObject < void draw() < . >void resize() < . >>
When an Abstract Class Implements an Interface
In the section on Interfaces , it was noted that a class that implements an interface must implement all of the interface’s methods. It is possible, however, to define a class that does not implement all of the interface’s methods, provided that the class is declared to be abstract . For example,
abstract class X implements Y < // implements all but one method of Y >class XX extends X < // implements the remaining method in Y >
In this case, class X must be abstract because it does not fully implement Y , but class XX does, in fact, implement Y .
Class Members
An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass.staticMethod() ) as you would with any other class.
Abstract method in Java with examples
A method without body (no implementation) is known as abstract method. A method must always be declared in an abstract class, or in other words you can say that if a class has an abstract method, it should be declared abstract as well. In the last tutorial we discussed Abstract class, if you have not yet checked it out read it here: Abstract class in Java, before reading this guide.
This is how an abstract method looks in java:public abstract int myMethod(int n1, int n2);
As you see this has no body.
Rules of Abstract Method
1. Abstract methods don’t have body, they just have method signature as shown above.
2. If a class has an abstract method it should be declared abstract, the vice versa is not true, which means an abstract class doesn’t need to have an abstract method compulsory.
3. If a regular class extends an abstract class, then the class must have to implement all the abstract methods of abstract parent class or it has to be declared abstract as well.Example 1: abstract method in an abstract class
//abstract class abstract class Sum < /* These two are abstract methods, the child class * must implement these methods */ public abstract int sumOfTwo(int n1, int n2); public abstract int sumOfThree(int n1, int n2, int n3); //Regular method public void disp()< System.out.println("Method of class Sum"); >> //Regular class extends abstract class class Demo extends Sum < /* If I don't provide the implementation of these two methods, the * program will throw compilation error. */ public int sumOfTwo(int num1, int num2)< return num1+num2; >public int sumOfThree(int num1, int num2, int num3) < return num1+num2+num3; >public static void main(String args[]) < Sum obj = new Demo(); System.out.println(obj.sumOfTwo(3, 7)); System.out.println(obj.sumOfThree(4, 3, 19)); obj.disp(); >>
10 26 Method of class Sum
Example 2: abstract method in interface
All the methods of an interface are public abstract by default. You cannot have concrete (regular methods with body) methods in an interface.
//Interface interface Multiply < //abstract methods public abstract int multiplyTwo(int n1, int n2); /* We need not to mention public and abstract in interface * as all the methods in interface are * public and abstract by default so the compiler will * treat this as * public abstract multiplyThree(int n1, int n2, int n3); */ int multiplyThree(int n1, int n2, int n3); /* Regular (or concrete) methods are not allowed in an interface * so if I uncomment this method, you will get compilation error * public void disp()< * System.out.println("I will give error if u uncomment me"); * >*/ > class Demo implements Multiply < public int multiplyTwo(int num1, int num2)< return num1*num2; >public int multiplyThree(int num1, int num2, int num3) < return num1*num2*num3; >public static void main(String args[]) < Multiply obj = new Demo(); System.out.println(obj.multiplyTwo(3, 7)); System.out.println(obj.multiplyThree(1, 9, 0)); >>
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Comments
This might be very old, but I’m trying to understand the necessity of the abstracts methods, there are more ways to get the same result, like deleting the interface, not implementing it, changing both methods to static and just printing the methods for example 1, like this: class Demo < public static int sumOfTwo(int num1, int num2)<
return num1+num2;
>
public static int sumOfThree(int num1, int num2, int num3) <
return num1+num2+num3;
>
public static void main(String args[]) <
System.out.println(sumOfTwo(3, 7));
System.out.println(sumOfThree(4, 3, 19));
>
> So, my question is why and when do you really need abstract methods? What is it that they can do that no other nonaccess modifier can do?They help you define a template for the sub classes. Abstract method is always in abstract class and when you extend the abstract class, you have to implement that method in your sub class else you will get compilation error. Which means you are defining a template for the sub classes. For example:
You want your sub classes to have a method disp() but at the same time, you want it to be different for each class. Then you declare this method abstract in parent class. This way all the sub classes will have to implement this method and give their own implementation.
I recommend you to read the abstract class tutorial (link provide in the above guide).