Abstraction in Java and used of abstract class & method in Java
Skip to main content

Abstraction in Java and used of abstract class & method in Java

abstraction in java, data abstraction in java, abstract class and method in java, abstract method in java, abstract class in java, what is an abstraction in java, what is data abstraction in java, java abstract methods, abstract class in java example, abstract class in javascript, abstract class in java program




Introduction:-

                 Abstraction in Java and Abstract Class & Method in this tutorial we explain about what is abstraction and also explain about abstract method and abstract class in Java with example


What is abstraction?
Abstraction is a process to hiding the certain details from the users and that focus on a  service provide to a users is called Abstraction. For example  a developer create a calculator in Java and that's providing a service addition, subtraction, multiplication and division. When you use a calculator for addition a two number then user get only output of addition he did not know what is working behind the calculation in a program.

Advantage:-

1.      Avoid code duplication and increase reusability.

2.      The size of code is reduce and reduce complexity.

3.      Provide a security of program only require detail is provide to users.

4.      We can able to  perform any type of changes in back side without effecting the end users.  

 

Abstract Class:-

                        Which is used abstract keyword before the class at the declaration time in a Java is called abstract class.  it can have abstract and non abstract methods. It cannot be instantiated.

 

Note:-

1.      We cannot create a object of abstract class

2.      We use both abstract and non abstract methods in a abstract class

3.      We can also use constructor and final method in a constructor in a abstract class.

4.      To use a abstract class, we have inherit in sub class.  

5.      If class has contain a any abstract method in a class then its compulsory to create that class as abstract class.

 

Most Read:-What is Looping statement in Java and explain with example?

 

Syntax:-

                        abstract class_name{

                                --------------------------

                                ----------------------------

                           }


Abstract Methods:-

                                 If two things  different but some of features is same then we use abstract method.

Syntax:- abstract method_name(){}

 

Example:- two car Maruti and Hyundai and  if  both cars is red colour then we create a abstract function colour for both of car extends by super class car.

abstract class Car{

           public abstract void colour();

     }

class Maruti extends Car{

    colour(){

             System.out.println(“The Maruti Car Colour is Red”);

              }

 }

class Hyundai extends Car{

    colour(){

             System.out.println(“The Hyundai Car Colour is Red”);

              }

 }

class Main{

        public static void main(){

             Maruti maruti =new Maruti();  //  creating a reference of the abstract class for using the function of the abstract class.

            Hyundai hyundai = new Hyundai();

              maruti.colour();

             hyundai.colour();

      }

}

 

Output:- The Maruti Car Colour is Red

The Hyundai Car Colour is Red


Most Read:-What is a switch case and How to use in Java?




         

Post a Comment

0 Comments