The tutorial provides the detailing about the creation and usage of abstract class in java. The java abstract class is declared using the keyword’ abstract’ . The abstract class could have methods with or without implementation. The abstract classes cannot be initiated as they could have unimplemented methods but these abstract classes can be sub-classed.
abstract class usage in java
The below given is the abstract method which is declared but not implemented
abstract void model(String carModel);
abstract void engineCapacity(int cc);
The class using the abstract methods should also be declared as abstract class
public abstract class Car {
// declare class variables
//declare non-abstract methods (which includes implementation)
abstract void model(String carModel);
abstract void engineCapacity(int cc);
}
Points to Consider for creating java abstract class
- Java abstract class are required to be declared with “abstract” keyword
- Java abstract class cannot be initiated
- Java abstract class contains abstract method with or without body.
- Java abstract does not always require to have abstract methods even though it is being defined as an abstract class
- The sub class of the java abstract class must implement all abstract methods.
- Interfaces methods are implicitly abstract unless they are static or default. (https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html )
How java abstract class is different from interface class ?
The java abstract class is similar to Interface except the difference that the class which implements the interface must implement all of the interface methods whereas if we need the class which does not require to implement all the methods then the class with methods should be declared as ‘abstract’.
Java abstract class example
Lets consider the example of Vehicles where vehicles could be of different model and having different engine power. so the below abstract class Vehicle is created where the engine
package core.java.oracleappshelp.basics; //abstract class example public abstract class Vehicle { private String vehicleName; //private String vehicleModel; public Vehicle(String name){ this.vehicleName=name; //this.vehicleModel=model; } //create the abstract method public abstract void getEnginePower(int enginePower); //create the abstract method public abstract void getVehicleModel(String model); @Override public String toString(){ return "Vehicle Name="+this.vehicleName; } }
package core.java.oracleappshelp.basics; public class VehicleEngine extends Vehicle { private int engineCC; private String name; private String model; public VehicleEngine(String name) { super(name); this.name = name; } @Override public void getEnginePower(int enginePower) { if(enginePower == 0){ System.out.println("The Engine CC details are not provided"); }else{ this.engineCC =enginePower; System.out.println( "Vehicle Engine Power is="+this.engineCC + "\n"); } } @Override public void getVehicleModel(String model) { if(model == null){ System.out.println("The Engine model details are not provided"); }else{ this.model =model; System.out.println( "Vehicle Name="+ this.name); System.out.println( "Vehicle Model="+this.model ); } } public static void main(String args[]){ Vehicle vehicle = new VehicleEngine("Maruti"); vehicle.getVehicleModel("Maruti Suzuki ZX"); vehicle.getEnginePower(900); Vehicle vehicle2 = new VehicleEngine("Toyota"); vehicle2.getVehicleModel("Camry"); vehicle2.getEnginePower(1200); } }
Vehicle Name=Maruti Vehicle Model=Maruti Suzuki ZX Vehicle Engine Power is=900 Vehicle Name=Toyota Vehicle Model=Camry Vehicle Engine Power is=1200