Alphalearningschool
        

<<< Prev

Next >>>

Up
Java Abstraction

Java Abstraction 

Abstraction is an object modeling principle. Through abstraction we will design/declare object operations those are to be implemented by different sub type programmers of this object.

Abstraction will come in to picture at project design phase.

In general abstraction means hiding an object operation implementation details from the user and provides only necessary details to access the object operation is called abstraction. 

Example: 

  1. Keyboard operations are abstracted- means when we type a key we do not know how characters are transferred and displayed on notepad file.

So we can say abstraction is about” Hiding unnecessary details from the user” and” Taking only what we need at that time by ignoring unnecessary things” is called abstraction. 

Important points of abstract method

  • We can develop abstraction in java using abstract method by declaring it either in abstract class or in interface.
  • Basically abstraction provides a contract document between object user and object implementer.
  • By implementing abstraction principle we will provide only method prototypes to user programmer by hide method implementation details in sub classes.
  • By declaring method as final we can restrict subclass not to override super class method.
  • By creating method as abstract method we can force subclass to override super class method. 

Abstract method        

 A method that has only prototype  and doesn’t have body is called abstract method. Abstract method should contain modifier abstract and should ends with semi colon. 

For example: abstract void m1();

public abstract class Student {

   private String name;

   private int  class;

   private int USN;

  

   public abstract double computeResult();

   // Remainder of class definition

}

Note − Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.

Suppose grade class inherits the student class, then it should implement the computeResult () method as shown below −

/* File name : grade.java */

public class Grade extends Student {

   private double grade;    

 

   public double computeResult() {

      System.out.println("Computing result for  " + getName());

      return grade/52;

   }

   // Remainder of class definition

}

 

Note: In a class we are allowed only to define methods with body. Since we are changing its default property- means removing its body- it must use modifier abstract keyword in its prototype

 

Abstraction:

       Basic:

  • Abstraction is in simple word hiding the implementation from user or client.
  • User or client able to see only functionality.
  • Like Computer you can only see the keyboard, mouse, display but you cannot able to see motherboard, Ram, SMPS etc.
  • We can achieve 100% abstraction by Interface and you can achieve abstraction by Abstract class.
  • Now we see what is Abstract class and Interface, how we can achieve 100% abstraction in java.

Abstract Class & Abstract method:

  • Abstract class is class with declared with abstract keyword.
  • If we achieve 100%, abstraction by abstract class then all method should be abstract only.
  • Abstract method means method without implementation.
  • In abstract class may or may not be all method abstract.
  • This abstract method must always override in subclass or child class otherwise, subclass should be abstract.
  • Abstract method must follow abstract key word.

For batter understanding we should go through to the example but in bellow example we will not get 100% abstraction because all method are not abstract.

Ex.
	
 abstract class Shape {

	 abstract void draw();
	 abstract void disp();
	 
	 public void volume(double d ,double e ){
		 System.out.println("Sum of Two number :- "+(d+e));
	 }
}
 

public class Circle extends Shape {

	@Override
	void draw() {
		System.out.println("Drawing something");

	}

	@Override
	void disp() {
		System.out.println("Yeah shape is Circle");

	}

}

public class Squre extends Shape {

	@Override
	void draw() {
		System.out.println("Drawing something");

	}

	@Override
	void disp() {
		System.out.println("Yeah shape is Squre");
	}

}

public class MainAbstrct {

	public static void main(String[] args) {
		Shape sc = new Circle();
		Shape ss = new Squre();
		sc.draw();
		sc.disp();
		System.out.println("------------------");
		ss.draw();
		ss.disp();
		System.out.println("------------------");
		sc.volume(1.1, 2.4);
		ss.volume(1.4, 2.6);

	}

}

Output:

Drawing something

Yeah shape is Circle

------------------

Drawing something

Yeah shape is Squre

------------------

Sum of Two number :- 3.5

Sum of Two number :- 4.0 

Now we want 100% by using abstract class for that example as bellow.

 

Ex.
abstract class Shape {

	 abstract void draw();
	 abstract void disp();
	 
	 abstract void volume(double d ,double e );
		 
}

public class Circle extends Shape {

	@Override
	void draw() {
		System.out.println("Drawing something");

	}

	@Override
	void disp() {
		System.out.println("Yeah shape is Circle");

	}

	@Override
	void volume(double d, double e) {
		System.out.println("Volume for Circle :- " + (d + e));
	}

}
public class Squre extends Shape {

	@Override
	void draw() {
		System.out.println("Drawing something");

	}

	@Override
	void disp() {
		System.out.println("Yeah shape is Squre");
	}

	@Override
	void volume(double d, double e) {
		System.out.println("Volume of Squre :- "+(d+e));
	}

}

public class MainAbstrct {

	public static void main(String[] args) {
		Shape sc = new Circle();
		Shape ss = new Squre();
		sc.draw();
		sc.disp();
		System.out.println("------------------");
		ss.draw();
		ss.disp();
		System.out.println("------------------");
		sc.volume(1.1, 2.4);
		ss.volume(1.4, 2.6);

	}

}

Output:

Drawing something

Yeah shape is Circle

------------------

Drawing something

Yeah shape is Squre

------------------

Volume for Circle :- 3.5

Volume of Squre :- 4.0

 

For using above example we can achieve 100% abstraction.

 

Interface in Java:

       Basic:

  • Interface is nothing but is a blue print of class.
  • In interface all method are abstract.
  • In interface we are using Interface <Class_name> instead of using class <Class_name>.
  • Interface also represent a Is A relationship.
  • We use interface for getting 100% abstraction. 

Interface:

  • Interface is same as a class.
  • In interface all method are unimplemented or abstract.
  • Java does not support multiple inheritances at that time we use Interface.
  • Interface also use for achieve loose coupling.
  • In interface variable by default static, final.
  • If any Interface does not have method then that interface called us Marker interface.
  • For batter understanding just follow the block diagram. 

  • Through interface, we can achieve 100 % abstraction.
  • Now question is arise if we already have abstract class then why we go for interface?
  • Because in abstract class may not contain final, static value but in interface by default all values are final, static.
  • We cannot create object of interface but we can make reference of it that refer to the object of implementing class.
  • Class can possible to implement more than one interface.
  • From JDK 1.9 interface can contain following also static method, private method and private static method.

For much understanding let, see one example of interface.

public interface Animal {

	public void vegiterian();

	public void nonVegiterian();

}


public class Elephant implements Animal {

	@Override
	public void vegiterian() {
		System.out.println("Elephant is Vegiterian");

	}

	@Override
	public void nonVegiterian() {
	}

}


public class Tiger implements Animal {

	@Override
	public void vegiterian() {

	}

	@Override
	public void nonVegiterian() {

		System.out.println("Tiger is Non-vegiterian");
	}

}



public class Zoo {

	public static void main(String[] args) {
		Animal el=new Elephant();
		el.vegiterian();
		Animal ti=new Tiger();
		ti.nonVegiterian();
	}
}

Output:

Elephant is Vegiterian

Tiger is Non-vegiterian