Abstract class in Java

 In real world, there are multiple scenarios where we do not have the complete definition of an object, still we want to continue working on what we have. Similarly in programming, while creating classes and objects there may be some methods or functionality which definition is not available currently. Hence to solve this issue, JAVA introduces abstract classes.

    A class is said to be abstract class if it is incomplete. We use abstract keyword to declare an abstract class. 
    Note: We cannot instantiate an abstract class.

    Syntax:
        abstract class SmartPhone{
            //statements
        }

    Similarly, we define abstract methods (which do not have any body/definition):
        Example:
            abstract class SmartPhone{
                    public abstract void newFunctionality();
            

        Note: An abstract class can have abstract and non-abstract class together.
                  Abstract class enforces inheritance and overriding.

        
Consider the following example:
        abstract class SmartPhone{
                public abstract void newFunctionality();
                public abstract String newFeatures();
                public void touch(){
                    System.out.println("This smartphone is touchable");
                }
        }

        We have to extend the abstract class to implement its functionality as:
         class Realme extends SmartPhone{
                //Since we have extended an abstract method, we must implement all abstract method declared within abstract class
                @Override
                public void newFunctionality(){
                    System.out.println("New Functionality Implemented");
                }

                @Override
                public String newFeatures(){
                    return "Fetures";
                }

        }

    

Comments

Popular Posts