Java Abstract class
Posted by JanWan
Last Updated: July 05, 2012

Java Abstract class 

 

What is a Java Abstract class?

Abstract class represents common pattern or skeleton of similar classes which will extend an abstract class and implement all abstract methods of the abstract class.

An abstract method is a method that is declared but does not have an implementation (without braces, and followed by a semicolon).

You should use abstract methods to specify what methods (behavior) subclasses MUST have but it is NOT SAME for all subclasses (If you have one same method implementation for all subclasses, then you should create a normal, non-abstract method in the parent class WITH implementation, and all subclasses will inherit this method as is).

Class that extends an abstract class must implement all abstract methods, or be an abstract class.

It may contain variables, implemented methods and  methods declarations  abstract methods.

Abstract class CANNOT be instantiated (you can create an object of the class that does not have ANY abstract method).

Abstract class CAN extend a class and CAN implement a Java interface.

 

Java Abstract class example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

/**
 * Java abstract class example
 * Abstract class represents skeleton of the classes
 * which will extend this abstract class
 */


    
/**
     * Vehicle abstract class is a general class of all vehicles
     * It contains methods and attributes common to all vehicles
     * but you can't create an instance of the Vehicle class
     * Vehicle class is used as a basic structure for all specific vehicles (car,bus...)    
     */

    
abstract class Vehicle {
        
//subclasses will inherit an attribute
        
int maxSpeed;
        
//subclasses must implement this method 
        
//(otherwise they have to be declared as abstract classes)
        
abstract void showMaxSpeed(); 
        
//subclass will have this method (through inheritance) as is (default implementation)
        
//or they may implement their own implementation (override a method)
        
int getWheelsNumber() {
           
return 4;
        
}
  
}
   
   
/**Car IS-A Vehicle*/
   
class Car extends Vehicle {
     
public Car() {
         maxSpeed = 
180; 
     
}
     
     
public void showMaxSpeed() {
        System.
out.println("Car max speed: " + maxSpeed + " km/h");
     
}
     
//Car class will inherit getWheelsNumber() method from the parent class
     
//there is no need to override this method because default implementation
     
//is appropriate for Car class - 4 wheels
   
}
   
   
/**Bus IS-A Vehicle*/
   
class Bus extends Vehicle {
     
public Bus() {
         maxSpeed = 
100; 
     
}
     
     
public void showMaxSpeed() {
        System.
out.println("Bus max speed: " + maxSpeed + " km/h");
     
}
     
//Bus class will override this method because the default implementation
     
//is not appropriate for Bus class
     
public int getWheelsNumber() {
         
return 6;
     
}
   
}
   
   
/**Truck IS-A Vehicle*/
   
class Truck extends Vehicle {
     
public Truck() {
         maxSpeed = 
80;
     
}
     
     
public void showMaxSpeed() {
        System.
out.println("Truck max speed: " + maxSpeed + " km/h");
     
}
     
//Truck class will override this method because the default implementation
     
//is not appropriate for Bus class
     
public int getWheelsNumber() {
         
return 10;
     
}
   
}

public class AbstractClassExample {
   
   
public static void main(String[] args) {
     Vehicle car = 
new Car();
     Vehicle bus = 
new Bus();
     Vehicle truck = 
new Truck();
     car.
showMaxSpeed();
     bus.
showMaxSpeed();
     truck.
showMaxSpeed();
     System.
out.println("Wheels number-car:" + car.getWheelsNumber() + 
             
", bus:" + bus.getWheelsNumber() + ", truck:" + truck.getWheelsNumber());
   
}
}

 Java abstract class example

J.W. PRODUCTION

 

REFERENCE: http://www.javacodeexamples.com/java-abstract-class-example/

 

Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024
Square and Cube Class in C++
Square and Cube Class in C++
Samath | Jan 01, 2015