Abstraction In Java With Example, abstract Class and Abstract Method

What is Abstraction in java?

Abstraction is one of the important oops concept ,

Abstraction is defined as the process of showing the set of services and hiding the implementation.
here a real time example of abstraction is Bank ATM In ATM machine the screen showing the set of services like Cash withdraw , transfer , change Pin but in not showing the internal implementation ,this is all about abstraction.
We are achieving the abstraction by using abstract class and interface.
Using abstract class (0% to 100%)
Using interface (100%)

Before learning abstraction first we have to understand about the abstract class and abstract method .

Abstract Method

Abstract method in java ,is a method which is declare with abstract keyword and will have semicolon "" at the end.
A abstract method is a just a signature without any implementation block inside. abstract method must be overridden in the sub class to make use of the object to invoke .
An abstract method is just a prototype for the method with the following attribute .
1) A return type
2) A name
3) A list of Parameters
4) A throws clause which is optional

Example:   abstract void salary();     //This is the example of abstract method
  • Abstract method contains only method declaration but no implementation .
  • Every abstract method must ends with semicolon(;)
  • We use abstract modifier to represent a method is abstract.
Example of abstract method 
Following is an Example of abstract method

  public abstract class School//Abstract Class
   {
String name;
int rollnumber;

abstract void Student ();//Abstract Method

   }

Abstract Class
In java A abstract class is defined as the "Class may contain abstract method or may not contain abstract method but for the abstract classes object creation not possible ".
A class which is declare with abstract keyword is known as the abstract classes.

Important Point to Remember 

  • For the abstract classes object creation not possible.
  • Abstract Class may contain abstract method or may not contain abstract method.
  • Abstract class must declare with abstract modifier.
  • if the class contain the abstract method then write the implementation in child classes.
  • Inside the abstract class it is possible to declare the main method.
  • Inside the abstract Class it is possible to declare the constructor.
  • it is possible to override the abstract method to normal method.
  • it is possible to override normal method to abstract method.
  • Inside the abstract class it is possible to declare the variable.
  • Inside the abstract class it it possible to declare the instance block and static block.
  • In Child class it is possible to override the abstract method and declare user defined normal method.
Example of Abstract class

Case:1 Class contain at least one abstract method 

 public abstract class School //Abstract Class
 {
String name;
int rollnumber;
void student()
{
}
void student()
{
}
abstract void student(); //Abstract Method

}

Case:2 abstract Class does not contain abstract method  

public abstract class School//Abstract Class
 {
 {
String name;
int rollnumber;
void student()
{
}
void student()
{
}
}


Example:1 Abstract Class Example with object Creation

//abstract parent class
abstract class Animal
{
 //abstract method
public abstract void sound();
}

//Dog class extends Animal class
public class Dog extends Animal
{

 public void sound()
{
System.out.println("huhuhu");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
  }

  }

Example -2  Inside the abstract Class declareation of main method is possible 

 public abstract class School //Abstract Class

 {
public static void main(String args[])
{
System.out.println("Abstract main method ");
}
}





Hope !! The above Tutorial " Abstraction in java With Example " Helpful For you ...
Team,
QA acharya

This article is contributed by  " Sandeep Yadav." 
If you want to share your knowledge with us and  would like to contribute, you can mail your article to   qa.article@gmail.com.



Tags: Abstraction in java , abstraction with example in java , abstract Class and abstract method , Java abstraction , abstraction in oops , abstract class in java with example , java abstraction example
Related Post:



Post a Comment

0 Comments