Javatpoint Logo
Javatpoint Logo

How to call a concrete method of abstract class in Java

The ability to call concrete methods of abstract classes is an important concept to understand when programming in Java. Abstract classes provide a way to define a standard set of behaviours and features that multiple classes can share without implementing them from scratch. These classes can create a foundation upon others to build, allowing developers to quickly create new classes with minimal effort.

In Java, abstract classes are created using the abstract keyword, followed by the class definition. Abstract classes can contain abstract and concrete methods, defined in the same way as any other Java methods. Abstract methods define the expected behaviour of classes that inherit from the abstract class, while concrete methods define the actual implementation of that behaviour.

Abstract classes are classes in Java that cannot be instantiated. They provide a base for subclasses that can be extended and used to create concrete objects. An abstract class may have abstract methods defined but not used. When a subclass extends an abstract class, the abstract class's abstract methods must be implemented. Abstract classes are essential to object-oriented programming, allowing developers to create abstract data types. These abstract data types can be used to define a class of objects, with each object having the same data structure and behaviour. Abstract classes also allow developers to define a set of operations that can be performed on any subclass object.

In this article, we will discuss how to call a concrete method of an abstract class in Java. We will also look at some examples of concrete methods and how they can be used in Java programs.

What is an Abstract Class?

An abstract class is a class that cannot be instantiated. It provides a base for subclasses that can be extended and used to create concrete objects. An abstract class may have abstract methods that must be implemented that abstract class will have abstract methods defined but not used.

A data type that is defined by its behaviour rather than by its struc known as an abstract data type. An abstract data type is defined using abstract classes. This means that if a class has a set of operations that can be applied to any class object, it may be said to be an abstract data type. A set of actions that may be carried out on any object belonging to the subclass can likewise be defined using an abstract class. This enables programmers to design a consistent interface between the abstract class and the subclass objects.

How to Call a Concrete Method of an Abstract Class

When a subclass extends an abstract class, the abstract class's abstract methods must be implemented. This contains the abstract class's concrete methods.

The subclass must first generate an object of the abstract class to invoke a concrete method of the abstract class. The subclass may then invoke the concrete methods of the abstract class on the object after it has been generated.

For example, we have an abstract class called Animal with a concrete method called move(). To call the move() method of the Animal class, the subclass must first create an object of type Animal. Once the object has been created, the subclass can call the move() method.

The syntax for Calling Concrete Methods

Calling concrete methods of abstract classes in Java is very similar to calling any other method. The syntax is as follows:

The ClassName is the abstract class name containing the concrete method you wish to call. The concreteMethodName is the name of the concrete method that you wish to call. Finally, the Arguments are the parameters that will be passed to the concrete method.

For example, if we have an abstract class called AbstractClass with a concrete method called doSomething, we can call it like this:

The something method in the abstract class will then be called with the argument, "Hello World!".

Importance of Using Concrete Methods

Using concrete methods in abstract classes is essential for several reasons. Firstly, it allows developers to quickly create classes with the same behavbehaviourfeatures as other classes without implementing them from scratch. This allows for efficient and clean code that is easy to maintain.

Secondly, concrete methods in abstract classes make the code easier to read and understand. Defining the expected behaviour of a class in an abstract class makes it much easier to determine what methods are available and what they do without having to read through all of the code.

Finally, using concrete methods in abstract classes can help improve the performance of programs. By defining the behaviour of a class in an abstract class, the code for that class does not need to be rewritten each time it is used. This can help reduce the amount of time it takes for a program to run, as well as reduce the program's memory usage.

Examples of Concrete Methods

Concrete methods are methods that are declared in an abstract class and are implemented in the subclass. They define a set of operations that can be performed on any object of the subclass.

Here are some examples of concrete methods that could be declared in an abstract class:

  • move(): This method would allow objects of the subclass to move in a particular direction.
  • eat(): This method would allow objects of the subclass to consume food.
  • sleep(): This method would allow objects of the subclass to rest.

Abstract classes in Java cannot be instantiated and contain abstract methods that subclasses must override. An abstract method is a method that is declared but not implemented in the code.

To call a concrete method of an abstract class, you must first create a subclass that extends the abstract class and then override the abstract methods. You can then create an instance of the subclass and call it the concrete method.

Program

AbstractClass.java

Output:

Abstract method implemented
A concrete method of abstract class

Conclusion

In this article, we discussed how to call a concrete method of an abstract class in Java. We looked at an abstract class and how it can be used to define an abstract data type. We also looked at some examples of concrete methods and how they can be used in Java programs. Finally, we discussed how to call a concrete method of an abstract class by creating an object of the abstract class and then calling the concrete method on the object.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA