Abstraction in Java

Okay Guys, Forgive me for the information I am about to talk, because I am not able to accept the descriptions present in the Internet about the Abstraction.

Internet Says (almost all websites) : Hiding internal details and showing functionality is known as abstraction., I donot Agree with this because this is what Encapsulation. isn't it ?

I could be wrong in my description, but I will continue with my perception, if you are not comfortable please skip this topic

Abstraction is nothing but the process of giving an set of guidelines to the user, who are going to implement these guidelines based on their needs.

For Example : I hope you are aware of headphone jack in the phones and radios, or the different chargers for different mobiles, which were available till 2008 (approximately, i donot know the exact year). Previously we had different phones with different chargers and different headphones jacks

After 2008 most of he companies came to an agreement that everyone should manufacture the Phones based on few specifications, which included headphone, charger ports(except Apple).

So they have concluded with 3.5mm jack and standard charger port, now you can interchangeable use the chargers and the headphones.

Like you can use samsung headphone with onePlus mobile

Similar to above scenarios when an architect design a framework, or a library or a software, he will give a specifications/guidelines.

If somebody want to take advantage of the framework/library they have to implement those specifications or guidelines.

Java provides Interface and Abstract class to achieve this abstraction.

Interface in Java :

Interface defines set of methods as a contract or as guidelines. The class that is implementing this interface must implement these functionalities in the concrete class.

An interface allows you to guarantee that certain methods exist and return the required types. When the compiler knows this, it can use that assumption to work with unknown classes as if they had certain known behavior.

The interfaces add value when you are building a larger system or a library.

Using references to interfaces instead of classes helps in minimizing the future changes as the user of an interface reference doesn't have to worry about the changes in the underlying concrete implementation.

Whenever there is interface there is Inheritance, as the class which change/ overrides the interface method must be subclass of Interface.

Webdriver interface provides few standard abstract methods to handle the webpages, So when some browser want to access Selenium method, then they have to implement the Webdriver interface and provide implementation to the methods present in the Webdriver interface

You must implement the all the abstract methods present in the interface but the behavior of the methods could be on your own way.


public interface WebDriver {
	// public- access modifier
	// void- return type
	// get - method name
	public void get(String url);
	public String getTitle();
}			

Below Class implements the WebDriver interface.


public class FireFoxDriver implements WebDriver{
	@Override
	public void get(String url) {
		System.out.println("Open webpage");
		// actual code differs, in get method
	}
	@Override
	public String getTitle() {
		System.out.println("Webpage title");
		// actual code differs, in getTite method
		return "title";
	}
}			

If you donot implement the methods present in the WebDriver class then Compiler shows error.

Types of Methods in Interface from Java9

YES, we can have private methods in interface in java 9, private methods could be static or non-static. In both cases, the private method is not inherited by sub-interfaces or implementations.

They are mainly there to improve code re-usability within same interface only – thus improving encapsulation.

Using private methods in interfaces have four rules :

  • Private interface methods cannot be abstract.
  • Private method can be used only inside interface.
  • Private static method can be used inside other static and non-static interface methods.
  • Private non-static methods cannot be used inside private static methods.

Interfaces can below item in interface in Java9 (aka JDK 1.9) :

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

										package frames;
										interface ISam {
											public static final String name = "Java9";
											public void testAbstract();
											default void testDefault(){
												// call non-static method from another non-static method.
												testPrivateNonStatic();
												// call static method from non-static method
												testPrivateStatic();
												
												System.out.println("Default method");
											}
											
											public static void testStatic(){
												System.out.println("static method");
											}
											private void testPrivateNonStatic(){
												System.out.println("private non-static method");
											}
											private static void testPrivateStatic(){
												System.out.println("private static method");
											}
										}
										public class Sample implements ISam{
											@Override
											public void testAbstract() {
												System.out.println("Overridden 'testAbstract' method");
											}
											public static void main(String[] args) {
												System.out.println("constant variable value : "+ name);
												// access static method
												ISam.testStatic();
												// create object for Sample class
												Sample sam = new Sample();
												// we can access default method using object
												sam.testDefault();
												// call abstract method (implementation in Sample class)
												sam.testAbstract();
											}
										}
										

Output


											constant variable value : Java9
											static method
											private non-static method
											private static method
											Default method
											Overridden 'testAbstract' method
										

Abstract Class :

We use abstract classes when we know the methods, but we don’t know how they would be implemented.

There are very few instances where implementing an empty method in Java is a good idea. The reason is, if someone were using our class, they wouldn’t know they needed to provide an implementation.

When we mark the method as abstract, anyone using our class has no choice but to provide an implemented child class. They can’t instantiate the class otherwise.

This is called compile time safety. Compile-time safety ensures that any classes that extend our abstract class must provide the bare minimum functionality to work.

Then why cannot I use Interface instead of Abstract class, you cannot because if you want to provide implementation for few methods (concrete methods) then you must use Abstraction

Note : With latest updates of Interface, Abstract class may go obsolete in 3-4 years as Interface now started to accept the method with implementations.

Abstract method : method with no implementation

Concrete Method : method with implementation

We can override both abstract and concrete methods in subclasses, but we cannot create object to a abstract class, but we can create object to the subclass which overrides the abstract class completely.

Abstract class must have abstract keyword in method declaration so abstract methods.


public abstract class WebDriver {
	// public- access modifier
	// void- return type
	// get - method name
	public abstract void get(String url);
	public String getTitle() {
		System.out.println("Webpage title");
		// actual code differs, in getTite method
		return "title";
	}
}			

Below Class implements the WebDriver interface.


public class ABSFireFoxDriver extends WebDriver{
	@Override
	public void get(String url) {
		System.out.println("Open webpage");
		// actual code differs, in get method
	}
}			

Handling Custom Dropdowns in selenium

Multi-level Abstraction in Java :

It is not mandatory to override all the abstract methods present in the abstract class in single sub-class, if we donot provide all the implementation in subclass then we have to mark it as abstract class only.

We can create an object to a subclass which donot have any abstract methods in it. So we can implement the abstraction methods in multi-level

Multilevel Abstraction is nothing but the implementing a interface or Abstract class methods in different sub classes

For Example an Interface 'A' has 4 abstract methods called testa(), testb(), testc(), testd().

Now we can implements multilevel abstraction like class SubB implements interface A and provides implementation only to testa(), another class SubC extends SubB class and provides implementation to testb(), another class SubD extends SubC class and provides implementation to testc() and testd().

Now SubB, SubC classes are providing implementation to only few methods but not for all so they must be marked as abstarct classes. But class SubD provides implementation to all the methods so we can mark it as normal class and we can create object to it.

If you have query like SubD provides implementation to only testc(), testd(), then you might not notices the relation between the SubB, SubC, SubD.

When a parent class provides implementation to some abstract class then it is not mandatory for a subclass to provide implementation to the method, which has implementation in parent class

If you ask cannot I override it in subclass ?, yes you can override but it is optional only

Below images shows how the multi-level abstraction works: multi-level-abstraction-java

Program for multi-level abstraction


package test;
interface A{
	public void testa();
	public void testb();
	public void testc();
	public void testd();
}
abstract class SubB implements A {
	public void testa() {
		System.out.println("test-a");
	}
}
abstract class SubC extends SubB{
	public void testb() {
		System.out.println("test-b");
	}
}
public class SubD extends SubC{
	public void testc() {
		System.out.println("test-c");
	}
	public void testd() {
		System.out.println("test-d");
	}
	public static void main(String[] args) {
		// create object to class SubD
		SubD subdObj = new SubD();
		subdObj.testa();
		subdObj.testb();
		subdObj.testc();
		subdObj.testd();
	}
}
			
output-multi-level-abstraction

Methods in Interface

YES, we can have private methods in interface in java 9, private methods could be static or non-static. In both cases, the private method is not inherited by sub-interfaces or implementations.

They are mainly there to improve code re-usability within same interface only – thus improving encapsulation.

Using private methods in interfaces have four rules : 1. Private interface methods cannot be abstract.
2. Private method can be used only inside interface.
3. Private static method can be used inside other static and non-static interface methods.
4. Private non-static methods cannot be used inside private static methods.
Interfaces can below item in interface in Java9 (aka JDK 1.9) : 1. Constant variables
2. Abstract methods
3. Default methods
4. Static methods
5. Private methods
6. Private Static methods


										package frames;

										interface ISam {
											public static final String name = "Java9";
											public void testAbstract();
											default void testDefault(){
												// call non-static method from another non-static method.
												testPrivateNonStatic();
												// call static method from non-static method
												testPrivateStatic();
												
												System.out.println("Default method");
											}
											
											public static void testStatic(){
												System.out.println("static method");
											}
											private void testPrivateNonStatic(){
												System.out.println("private non-static method");
											}
											private static void testPrivateStatic(){
												System.out.println("private static method");
											}
										}

										public class Sample implements ISam{
											@Override
											public void testAbstract() {
												System.out.println("Overridden 'testAbstract' method");
											}
											public static void main(String[] args) {
												System.out.println("constant variable value : "+ name);
												// access static method
												ISam.testStatic();
												// create object for Sample class
												Sample sam = new Sample();
												// we can access default method using object
												sam.testDefault();
												// call abstract method (implementation in Sample class)
												sam.testAbstract();
											}
										}

										

Output


											constant variable value : Java9
											static method
											private non-static method
											private static method
											Default method
											Overridden 'testAbstract' method
										

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Krishn
    Wt is the abstract that we used in selenium
    Reply
    • Krishna
      What is the abstract class that we used in selenium
      Reply
  • Recent Addition

    new tutorial Protractor Online Training : We have closed registration for training

    Please do email to chercher.tech@gmail.com for any queries

    If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
     
    Join My Facebook Group
    Join Group