Home Java Master-java Abstraction in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Abstraction in Java

Advertisement
Abstraction is one of the four pillar of OOPS Concept. Abstraction in java is a process by which hiding the irrelevant data and exposing only the essential feature of object. In java Abstraction is achieved by using abstract class and interface.

Abstraction in Java

Abstraction is one of the four pillar of OOPS Concept. Abstraction in java is a process by which hiding the irrelevant data  and exposing only the essential feature of object. In java Abstraction is achieved by using abstract class and interface. Abstract class and interface is something which is not concrete or incomplete. for example car is made up of several parts like engine, tire, gearbox ,steering etc but while creating a class of car one does not need to know how it works, they just need to know how to communicate with them i.e. send a message, receive a message etc.

Advantage of Abstraction in Java.

  • Reusability.
  • Readability.

What is abstract class in java?

A class which cannot be instantiated , means we cannot create object of abstract class. To implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be abstract in Java.

Syntax of Abstract class :

abstract class Classname 
 { 
    abstract type methodname(); //only declaration 

    type methodname()
    {
      //Body
    }
  }

Abstract Method:

An Abstract Keyword is used to declare a method Abstract  method ,which have  method signature  but not  method body. Abstract method  would not have body and signature followed by semicolon. no curly braces as follows.

 public abstract class Student 
 {
  private String name;
  private String address;
  public abstract info();//abstract method 
  }

To declare a method abstract following two ground rule have to be followed :

  • If a method declared as abstract that class should also declared as abstract.
  • Method which is declared as abstract ,should be override in Subclass which extend the abstract class.

Abstract class Example in Java.- In the example ,we define a class  Car as abstract class and under that defining two methods one is abstract method as acceleration and  another one is non-abstract method as speed . We need to override the methods of abstract class. So declaring a class Bike extending abstract class Car and overriding the accereleration method  in Subclass Bike. In class Abstractexp there is main method where we creating a object of Bike Class through that we call the acceleration method and speed method.

abstract class Car
{
	public void speed()//Non abstract method 
	{
		System.out.println("Non abstract method");
	}
	abstract void acceleration();//abstract method not to defined 
}
 class  Bike extends Car // extending the abstract class 
{
	public void acceleration()//abstract method should be override in subclass
	{
		System.out.println("Abstract method");
	}
}
public class Abstractexp{
	public static void main(String args[])
	{
		Bike c1=new Bike();
		c1.acceleration();//calling the abstract class methods 
		c1.speed();//calling non abstract method		
	}
}

Output : Through object of Bike class we are invoking the acceleration method of abstract class and printing the message "abstract method and also calling non abstract method speed, getting message as non abstract method.

Download Source Code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 3, 2013

Ask Questions?    Discuss: Abstraction in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com