Access Modifiers in java | Abstract Modifier in java
Abstract Modifier in java
Abstract Modifier:
Abstract is the modifier which can be used for methods and classes only but not for variables.
Abstract Methods:
For abstract methods we don't have implementation still we can declare a method with abstract modifier.
So from these,we come to know abstract methods have only declaration but not implementation.
Also abstract method declaration should compulsory ends with semicolon.
Example:
public abstract void methodOne(); ----->valid
public abstract void methodOne(){} ----->invalid
Child classes provides implementation for parent class abstract methods.
Example:
Program:vehicle.java
abstract class vehicle
{
public abstract int getNoOfWheels();
}
Program:Bus.java
class Bus extends vehicle
{
public int getNoOfWheels()
{
return 7;
}
}
Program:Auto.java
class Auto extends vehicle
{
public int getNoOfWheels()
{
return 3;
}
}
Important Note:
1. The main advantage of abstract methods is, by declaring abstract method in parent Class we can provide guide lines to the child class such that which methods they should compulsory implement.
2. Abstract method never talks about implementation whereas if any modifier talks
about implementation then the modifier will be against to abstract and that is always
illegal combination for methods.
Following are the various illegal combinations for methods:
1.abstract final
2.abstract static
3.abstract synchronised
4.abstract native
5.abstract strictfp
6.abstract private
All the 6 combinations are illegal.
Abstract class:
Any java class for which we are not allow to create an object such type of class we have to declare
with abstract modifier and for abstract class instantiation is not possible.
Example:
abstract class Test
{
public static void main(String args[])
{
Test t=new Test():
}
}
Output:
javac Test.java ------->(run on cmd)
Compile time error ------>(output on cmd)
What is the difference between abstract class and abstract method ?
1. If a class contain at least one abstract method then compulsory the corresponding class
should be declare with abstract modifier. Because implementation is not complete and
hence we can't create object of that class.
2. Even though class doesn't contain any abstract methods still we can declare the class
as abstract that is an abstract class can contain zero number of abstract methods also.
Example l: HttpServlet class is abstract but it doesn't contain any abstract method.
Example 2: Every adapter class is abstract but it does not have any abstract method.
Example l:
class Parent
{
public void methodOne();
}
Output:
javac Parent.java ----->(run on cmd)
Compile time error ----->(output on cmd)
Example 2:
class Parent
{
public abstract void methodOne()
{
}
}
Output:
javac Parent.java ------>(run on cmd)
Compile time error ----->(output on cmd)
Example 3:
class Parent
{
public abstract void methodOne()
{
}
}
Output:
javac Parent.java ------>(run on cmd)
Compile time error ------>(output on cmd)
If a class extends any abstract class then it is mandatory to provide implementation for
every abstract method of the parent class otherwise we need to declare child class as abstract.
Example:
abstract class Parent
{
public abstract void methodOne();
public abstract void methodTwo();
}
class child extends Parent
{
public void methodOne()
{
}
}
Output:
javac Parent.java -------->(run on cmd)
Compile time error -------->(output on cmd)
If we declare class child as abstract then the code compiles fine but child of child has responsibility to provide implementation for methodTwo().
Question: What is the difference between final and abstract ?
1. For abstract methods compulsory we should override in the child class to provide
implementation. But for final methods we can't override hence abstract final
combination is illegal for methods.
2. For abstract classes we should compulsory create child class to provide implementation whereas for final class we cannot create child class. Hence final abstract combination is illegal for classes.
3. Final class cannot contain abstract methods whereas abstract class can contain final
method.
Example:
final class A
{
public abstract void
methodOne();
}
Note: Above class A is invalid
abstract class A
{
public final void methodOne()
{
}
}
Note: Above class A is valid
Important Note:
Usage of abstract methods, abstract classes and interfaces is always good Programming practice.
An Inspirational quote:
"Very often, a change of self is needed more than a change of scene."
Happy Reading.Happy Learning!!! See you in next post :)
Post a Comment