Friday, 8 May 2020

What is static final, abstract, synchronized and native keyword in java?



Static:-The Static keyword is used with method, variables, and inner classes. The static keyword is used to define class variable and method that belong to a class and not a any particular instance of the class.

Final:-The final keyword is used with methods, variable, and classes. The final keyword indicates that the data member cannot be modified.
The Final keyword dose not allow the class to be inherited or modified.

The final keyword has the following characteristic:
  •          A final method can not be modified in the subclass.
  •          A final class cannot be inherited.
  •          All the method and data members in a final class are implicitly final.


Abstract:-The abstract keyword is used to declare classes that only define common properties and behavior of the other classes. A classes declared as abstract cannot be instantiated.
Where don’t user abstract:-
  • An abstract keyword cannot be used with variables and constructors.
  • If a class is abstract, it cannot be instantiated.
  • If a method is abstract, it doesn't contain the body.
  • We cannot use the abstract keyword with the final.
  • We cannot declare abstract methods as private.
  • We cannot declare abstract methods as static.
  • An abstract method can't be synchronized.
Where do use abstract
  • An abstract keyword can only used with class and method.
  • An abstract class can contain constructors and static methods.
  • If a class extends the abstract class, it must also implement at least one of the abstract method.
  • An abstract class can contain the main method and the final method.
  • An abstract class can contain overloaded abstract methods.
  • We can declare the local inner class as abstract.
  • We can declare the abstract method with a throw clause.



Example:-
abstract class Car
{  
    abstract void bike();  
      
}  
class Honda extends car  
{  
  
    @Override  
    void bike() {  
        System.out.println("Bike is running");  
      
    }  
      
}  
  
public class AbstractExample1 {  
  
    public static void main(String[] args) {  
  
   Honda obj=new Honda();  
    obj.bike();  
    }         
}  



0 comments:

Post a Comment