Smart Phone Example using Abstract Class in Java


This is an example of the use of abstract classes and methods in Java.

  • The abstract class Mobile defines two abstract methods camera() and touchDisplay(), and also has a non-abstract method VoiceCall(). Since the Mobile class is abstract, it cannot be instantiated directly.
  • The classes samsung and Nokia extend the Mobile class and implement its abstract methods. samsung only implements camera() and touchDisplay(), while Nokia implements the same methods as well as an additional method called fingerPrint().
  • The main method creates objects of both samsung and Nokia classes and calls their respective methods. samsung object has access to the VoiceCall(), camera(), and touchDisplay() methods of the Mobile class, while the Nokia object has access to these methods as well as the fingerPrint() method that is specific to the Nokia class.

Overall, this program demonstrates how abstract classes can be used to define a set of methods that must be implemented by any concrete class that extends it, and how these concrete classes can have different implementations of the abstract methods.

Source Code

//Example for Abstract Class in Java Programming
abstract class Mobile {
    void VoiceCall() {
        System.out.println("You can Make Voice Call");
    }
    abstract void camera();
    abstract void touchDisplay();
 
}
class samsung extends Mobile
{
 
    @Override
    void camera() {
        System.out.println("16 Mega Pixel Camera");
    }
 
    @Override
    void touchDisplay() {
        System.out.println("5.5 inch Display");
    }
}
 
class Nokia extends  Mobile
{
 
    @Override
    void camera() {
        System.out.println("8 Mega Pixel Camera");
    }
 
    @Override
    void touchDisplay() {
        System.out.println("5 inch Display");
    }
 
    void fingerPrint() {
        System.out.println("Fast Finger Sensor..");
    }
}
public class abstractDemo2 {
    public static void main(String args[]) {
 
        samsung M32 =new samsung();
        M32.VoiceCall();
        M32.touchDisplay();
        M32.camera();
        System.out.println("-------------------------");
        Nokia N1= new Nokia();
        N1.VoiceCall();
        N1.camera();
        N1.touchDisplay();
        N1.fingerPrint();
 
    }
}
 

Output

You can Make Voice Call
5.5 inch Display
16 Mega Pixel Camera
-------------------------
You can Make Voice Call
8 Mega Pixel Camera
5 inch Display
Fast Finger Sensor..
To download raw file Click Here

Basic Programs