Java- abstract modifier / Chapter -3/ Access Modifier



Java- abstract modifier


the abstract is a modifier applicable for classes and methods but not for variables.

1 . abstract method:-
If we don't know about implementation even though we have to declare the method then we should go for abstract method.
In the abstract method, the only declaration is available but not the implementation that's why abstract method declaration should end with "; " semicolon.


example:-
public abstract void m1( ) ;           correct
public abstract void m1( ){ }          incorrect

Child class is responsible to provide implementation for parent class abstract method.

example:-

abstract class Parent
{
    public  abstract int salary() ;
}


class Child1 extends Parent
{
    public int salary( )
    {
           return 70000;   
    }
}
class Child2 extends Parent
{
    public int salary( )
    {
           return 60000;   
    }
}

By declaring abstract method in the parent class we can provide guidelines to the child class such that which methods compulsory child class has to implement.

 

abstract method never talks about implementation.

If any modifier talks about implementation then it will be an illegal combination with the abstract modifier.

The following are illegal combinations of modifiers for the method with respect to "abstract".

java abstract modifier

 

2. abstract class:-
For any java class if we are not allowed to create an object then we have to declare that class as "abstract class".
If we know only partial implementation then we should go for abstract class.
Hence for abstract classes, instantiation is not possible.

example:-

abstract class Test
{
     public static void main(String [] args)
     {
         Test  t =new Test( );
      }
}
Compile-Time Error
Test is abstract, cannot be instantiated.




java abstract modifier

1.If a class contain at least one abstract method then compulsory we should declare that class as "abstract" otherwise we will get compile time error.

Reason:-

 If a class contain at least one abstract method then the implementation is not complete, hence it is not recommended to create objects.

To restrict object instantiation compulsory we should declare that class as "abstract".

 

 

2.Even though the class does not contain any abstract method still we can declare that class as "abstract".

if we don't want object instantiation.

So that abstract class can contain 0 number of the abstract method also.

example:-

 

HttpServlet class is abstract but it does not contain any abstract method. 

 

 

Important Syntax Errors 


example 1

 

class Test

{

       public void m1( );

}

 

Compile Time Error: missing method body, or declare as abstract.

 

 

example 2

 

class Test

{

       public abstract void m1( ){}

}

 

Compile Time Error: abstract methods cannot have a body.

 

 

example 3

 

class Test

{

       public abstract void m1( );

}

Compile Time Error: Test is not abstract and does not override abstract method m1( ) in Test.
Next:Java- strictfp modifier / Chapter -4/ Access Modifier
Back:Java- final modifier / Chapter -2/ Access Modifier


No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com