Non Abstract methods in Java
What is non-abstract method?
In java, a non-abstract method is a method that has a defined implementation or we can say that it is a block code that is executed when we call the method.
As we all know that a non-abstract method is also known as concrete method because it provide a concrete implementation of a method. An abstract method, in contrast just give a signature of that the implementing class must be specify. Non-abstract class does not have a stated implementation.
A non-abstract method can be declared within a class, interface or abstract class. However, if we define a method as non-abstract in an interface it must provide a default implementation using the default keyword. Non abstract methods in an interface are implicitly public and abstract.
For example:
Let’s take an example of a non-abstract method in java:
public class MyClass { public void myMethod() { System.out.println("This is a non-abstract method"); } }
Explanation of above program:
myMethod () is a non-abstract method that write a message to the console. The message this is a non-abstract method will be print when object of myclass will created and myMethod is invoked on the object.
Non-abstract method can also have parameter and return value just like abstract method. Non abstract method can also have implementation detail that specific to the class that defines them unlike abstract method.
Why we use non abstract method?
We use non-abstract method because it help us to provide the concrete implementation of a method, that helps us to define the behaviour of a method and we can also execute that method when it is called.
In other words, non-abstract method is used to define the behaviour of the class and it also provide the specific implementation of its methods.
Let’s us take an example so we can understand non-abstract methods in detail, now suppose we have a class that represent a bank account and may have non-abstract methods to deposit and withdraw the money, get the account balance and so on. So we can say that these method will prove a concrete way to interact with the account object and perform specific actions on it.
It is also useful in inheritance where a subclass can inherit the properties from the non-abstraction methods of its parent class and override them to provide a specialized implementation.
It helps us to reuse the code and it also maintain a consistent behaviour across related classes.
Basically, we can say that non-abstract method provide us a way to interact with method that have a concrete implementation of a method and it also support inheritance and code reusability.
Features of non-abstract methods in java
There are some features that are present in non-abstract method. Some are as follow:
- Defined implementation: as we already discussed about that non-abstract method are already defined implementation that can be executed when we call the method.
- Encapsulation: in non-abstract method we encapsulate the internal state of a class and it provide us a way to interact with the methods.
- Access modifier: in non-abstract method we also have access modifier like public private and protected that is used to control their visibility and accessibility.
- Return values: in the non-abstract method we can return a value of specified type, and it allow us to provide data to the code that is calling value.
- Parameters: it also take one or more parameters, which allow them to receive data from the calling code.
- Method overloading: as a name suggest it can be overload means that if we have multiple method with same name but different parameter can be exist in the same class.
- Inheritance: non-abstract methods can be inherited by subclasses, and it can then provide their own implementation of the method.
- Polymorphism: it can also participate in polymorphism, means that a reference to a subclass object can be used to call a non-abstract method defined in a superclass.
Limitation of non-abstract method in java
There are some limitation also present in the non-abstract method in java. Some of limitation are:
- Lack of flexibility: as we all know that non-abstract methods have a defined implementation so it can be less flexible compare to the abstract method.
- Inability to participate in dynamic method dispatch: it is basically bound at a compile time, means that the method that will be executed determined at compile time based on the declared type by object reference.
- Tightly coupled code: non-abstract method can be created tightly coupled code means it have ripple effect if one class made a change it will have effect on the other class.