articles

Home / DeveloperSection / Articles / Abstraction in Java

Abstraction in Java

Manoj Pandey3260 30-Mar-2015

Abstraction is a process of hiding the implementation details and showing only functionality to the user.Abstraction in java is achieved by using interface and abstract class. Interface give 100% abstraction and abstract class give 0-100% abstraction.

For example you are using mobile phone for calling. You don’t know how it’s work and what its process but you know how to call, here process is abstraction. Simply process is hide and external details is visible.

What is Abstract class in Java?

 A class that is declared as abstract is known as abstract class. An abstract class may have abstract methods and not-abstract method. It cannot be instantiated.

Syntax
abstract class className{           
  -----
}

 What is Abstract method in Java?

A method that is declare as abstract and does not have implementation is known as abstract method. If you define abstract method than class must be abstract.

If you are extending abstract class then you have compulsory to override its methods.

Syntax

abstract return_type method_name();

Here I am creating a sample of abstraction

1.      My Abstraction class 

public abstract class Data {   

 

     // abstraction method

     protected abstract int StudentId();

 

     // abstraction method

     public abstract String getStudnetCollegeInfo();

 

     // non abstraction method

     public String getStudentPersonalInfo() {

           return "\n Studnet Address-:     10B mg road Mumbai India \n Student Contact No-:  00000000\n Student Father Name-: Zack Anderson";

     }

 

}

}

2.      Main class

public class Student extends Data {

 

     //Override Abstraction methods

     @Override

     public String getStudnetCollegeInfo() {

 

           return "\n Student Name-:        Carry Anderson";

     }

 

     // Override Abstraction methods

     @Override

     protected int StudentId() {

           return 101;

     }

 

     public static void main(String args[]) {

           Student student = new Student();

      System.out.print(" Student Id-:\t       " + student.StudentId());

           System.out.print(student.getStudnetCollegeInfo());

           System.out.print(student.getStudentPersonalInfo());

 

     }

 

}

 

   After run your application output

      Abstraction in Java

 

 

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By