Quick Contact

    Hibernate

    Abstract class in Java

    In java if a class is declared with abstract keyword than it is called an abstract class. The abstract class consist of abstract method and non-abstract method. We cannot create an object in the abstract class.

    Abstract has a concrete method, so the abstract class is used to achieve abstraction, but it does not provide 100% abstraction.

    Here are the following points to abstract class-

    • An abstract class must be declared with an abstract keyword.
    • It can have abstract and non-abstract methods.
    • It cannot be instantiated.
    • It is used for abstraction.
    • It can have a constructor and static method.

    Syntax

    abstract class class_name { }

    Abstract method

    A method that is declared without anybody within the abstract class is known as an abstract method. An abstract method can never be final and static. If a class extends an abstract class, then must implement all the abstract methods.

    Syntax

    abstract return_type function_name (); //No definition

    Here are the following points to remember about the Abstract class and Method

    • Abstract classes are not interfaces.
    • Abstract classes have Constructor, Method variables and Normal methods.
    • Abstract classes are not instantiated.
    • When we extend an abstract class with the abstract method, we must define the abstract method in child class.

    Example of Abstract class

    		abstract class Car{  
    		  abstract void run();  
    		}  
    		class Maruti extends Car{  
    		void run(){System.out.println("Running smoothly");}  
    		public static void main(String args[]){  
    		 Car obj = new Maruti();  
    		 obj.run();  
    		}  
    		}  
    

    Output

    Running smoothly

    In the above example, Car is an abstract class that contains only one abstract method run. The Maruti class provides its implementation.

    Another example of abstract class

    		abstract class Company{    
    		abstract int Salary();    
    		}    
    		class Developer extends Company{    
    		int Salary(){return 50000;}    
    		}    
    		class Designer extends Company{    
    		int Salary(){return 40000;}    
    		}    
    
    		class Employee{    
    		public static void main(String args[]){    
    		Company c;  
    		c=new Developer();  
    		System.out.println("Monthly salary of Developer is: "+c.Salary()+" per month");    
    		c=new Designer();  
    		System.out.println("Monthly salary of Designer is: "+c.Salary()+" per month");    
    		}}    
    

    Output

    Monthly salary of Developer is: 50000 per month

    Monthly salary of Designer is: 40000 per month

    An Abstract class with a non-abstract method

    Abstract classes can also have non-abstract methods along with abstract methods. But while extending the class, provide a definition for the abstract method.

    Example

    		abstract class Car
    		{
    		  abstract void run();
    		  public void show()
    		  {
    		    System.out.println("this is non-abstract method");
    		  }
    		}
    
    		class Maruti extends Car
    		{
    		  void run()
    		  {
    		    System.out.println("Running Smoothly...");
    		  }
    
    
    		  public static void main(String[] args)
    		  {
    		    Maruti m = new Maruti();
    		    m.run();
    		    m.show();
    		  }
    		  }
    

    Output

    Running Smoothly…

    This is non-abstract method

    Abstraction in Java

    In abstraction, it shows only functionality and hiding the complexity. Abstraction is an important feature of OOPs. An abstract class is used to provide abstraction. Simultaneously, the abstract class does not provide 100% abstraction because it can also have a concrete method.

    In java, Abstraction is achieved by interfaces and abstract classes.

    Example of Abstraction in Java

    		abstract class Vehicle
    		{
    		   public abstract void engine();
    		}
    		 class Car extends Vehicle {
    
    		    public void engine()
    		    {
    		System.out.println("Car engine");	//car engine implementation
    		    }
    
    
    		    public static void main(String[] args)
    		    {
    		        Vehicle v = new Car();
    		        v.engine();
    		    }
    		}
    

    Output

    Car engine

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.