Home Java Beginners Abstraction In Java

 
 

Share on Google+Share on Google+

Abstraction In Java

Advertisement
This section describes you how to implement abstraction in Java.

Abstraction In Java

In this section we will read about the abstraction in Java.

Java is an Object Oriented Programming language and Abstraction is one of the feature of OOPs i.e. Java has the feature of abstraction. In Java abstraction is implemented through the abstract keyword. Abstraction means hiding of unwanted data from the user who doesn't have mean how it is functioning. In an abstraction information content of a concept is reduced and retain only the information applicable for a particular purpose. Abstraction provides the feature of separating categories and concepts from implementation.

In Java abstraction can be achieved by using the abstract class or implementing the interface. An interface provides the full abstraction in Java. In Java abstract keyword can be used with the class and methods but can't use with the variables.

There are two types of Abstraction

  1. Data Abstraction : In the data abstraction only the meaningful operation is exposed and all the implementations detail are being prevent from seen outside works.
  2. Control Abstraction : In the control abstraction, similar statements that are repeated over multiple times are identified and grouped together to perform a task. This type of abstraction is seen when a function is created for performing a task.

Abstraction Using abstract class

Abstraction in Java can be provided using abstract class. An abstract class is a special class which contains the abstract keyword before the class keyword. An abstract class may or not contain the abstract method. Abstract class can't be instantiated directly as the normal classes are instantiated. It can be inherited to its subclass and then these sub classes can use the methods of the abstract. An abstract class contains the common functionality to all its subclasses.

Example

Here we will show you a simple example which will demonstrate you about how to use abstraction in Java using abstract class. In this example we will first create an abstract class which will contain some abstract and non-abstract method.

ParentClass.java

public abstract class ParentClass {
	
	public abstract void display();
	
	public void add()
	{
		int a = 5;
		int b = 10;
		int c = a+b;
		System.out.println("Parent Class Addition = "+c);
	}	
}

ChildClass.java

public class ChildClass extends ParentClass{

	int a = 10;
	int b = 5;
	int c = a+b;
	@Override
	public void display() {
		
		System.out.println("Child Class Addition = "+c);
	}
	
	public void show()
	{
		System.out.println("Child Class");
	}
	
	public static void main(String args[])
	{
		ChildClass cc = new ChildClass();
		cc.show();
		cc.display();
		cc.add();
	}
}

Output :

Abstraction Using Interface

Interface in Java is also an another option to apply abstraction. Abstraction using interface is provided in Java using the interface keyword. Interface is written like a class in Java. It is a collection of public static final constant and/or abstract methods. To use the methods or constants of interface a Java class must have to implement this interface and override all of its methods, if declared.

Example

Here we will show you a simple example which will demonstrate you about how to use abstraction in Java using interface. In this example we will create an interface where we will declare some abstract methods then we will create a Java class which will implement this interface. If the class implements the interface then this class must have to override all of its methods.

Parent.java


public interface Parent {

	public static final int i = 10;
	
	public abstract void display();
	
	public void show();
}

Child.java

public class Child implements Parent{

	@Override
	public void display() {
		System.out.println("i of display() : "+Parent.i);		
	}

	@Override
	public void show() {
				
		System.out.println("i of show() : "+Parent.i);
	}
	
	public void add()
	{
		int b = 5;
		int c = Parent.i+b;
		System.out.println("add() : "+c);
	}

	public static void main(String args[])
	{
		Child ch = new Child();
		ch.add();
		ch.display();
		ch.show();
	}
}

Output :

When you will execute the Child.java class then the output will be as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: July 10, 2013

Related Tutorials

Discuss: Abstraction In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Abhinav Reddy
August 31, 2013
java interface and abstract

In java interface, even if they don't specify any abstract methods, do we still need to override those methods?
DMCA.com