Core Java - SPLessons

Abstraction in Java

Chapter 21

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Abstraction in Java

Abstraction in Java

shape Description

Abstraction in Java, Hiding the class implementation is known as abstraction. Abstraction can be achieved by defining class behavior in an Interface and implementing it in a class. Instead of creating a class reference, referring to the class object through an interface reference is known as abstraction. The advantage of abstraction is if the implementation of the class is changed, that will not affect the usage of the class, hence one can provide enhancement modification easily.

shape Description

Abstraction in Java, All abstract methods must be declared using “abstract” keyword. If a class containing at least one abstract method, then the class must be declared as abstract. If a class declared as abstract as it is not mandatory to develop abstract methods.


shape Conceptual
figure

shape Example

The following is an example for abstract that has abstract method.

package com;

abstract class Tutorial{  
    abstract void run();  
  }  
  class Java extends Tutorial{  
  void run(){System.out.println("It's a java tutorial");}  
  public static void main(String args[]){  
   Tutorial obj = new Java ();  
   obj.run();  
  }  
  }  

In the above example, Tutorial is the abstract class that has abstract method run() and implementation for this method will be provided by the class Java .
Output:
When compile code output will be as follows.

It's a java tutorial

shape More Info


shape Example

package com.spl.abstract1;

public class Abstract1 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		B dRef1=new B();
		dRef1.test1();
		dRef1.test2();
		System.out.println("Program ends");

	}
}
abstract class Sample1
{
	abstract int test1();
	abstract void test2();
}
abstract class A extends Sample1
{
	int test1()
	{
		System.out.println("test1() is defined in class A");
		return 12;
	}
}
class B extends A
{
	void test2()
	{
		System.out.println("test2() is defined in class B");
	}
}
  • Above example class A preaches or violets the contract of sample 1 abstract class because class A is not defining test 2() method which is still abstract in the class A.
  • Class B is a concrete class even though it is not overriding all the abstract methods because class B contains only test 2()method as an abstract that method is defined in class B).
  • Where as test 1() is already made concrete in the super class A.

Output:
When compile the code result will be as follows.

Program starts
test1() is defined in class A
test2() is defined in class B
Program ends

shape Example

package com.spl.abstract2;

public class Abstract2 {
public static void main(String[] args) {
	System.out.println("Program starts");
	A dRef1=new A();
	dRef1.test1();

	System.out.println("Program ends");

}
}
abstract class Sample1
{
	int test1()
	{
		System.out.println("test1() is defined in class Sample1");
		return 12;
	}
}
class A extends Sample1
{

}

Output:
When compile the code following is the result will be generated.

Program starts
test1() is defined in class Sample1
Program ends
  • In the above example the class B is becoming concrete before fulfilling the contract of abstract class because the abstract class doesn’t have any abstract method that has to be overridden by subclass. Hence, abstract class is not 100% abstract.
  • The following is an example for abstract class which will have data member, constructor and method.

    package com;
    
    abstract class Tutorial1{  
    	   Tutorial1(){System.out.println("My tutorial");}  
    	   abstract void run();  
    	   void myblog(){System.out.println("This is my blog");}  
    	 }  
    	  
    	 class Java1 extends Tutorial1{  
    	 void run(){System.out.println("It's java tutorial");}  
    	 }  
    	 class TestAbstraction{  
    	 public static void main(String args[]){  
    	  Tutorial1 obj = new Java1();  
    	  obj.run();  
    	  obj.myblog();  
    	 }  
    	}  

    Output:
    The result will be as follows.

    My tutorial
    It's java tutorial
    This is my blog
    

    Summary

    shape Key Points

    • To achieve 100% abstract, develop all the methods in abstract class as abstract or we can use interfaces.
    • Abstraction in Java, Abstraction will be achieved by abstract class.
    • One can declare abstract keyword before the class name and method name.