The Best Technical Blog Ever

twitterrss

Abstraction in Java

Data abstraction gives only important details without including the background details or explanations. It is a process of hiding the implementation details and showing only the functionalities to the user. For example, when we see a mobile phone, we get to see the whole image as a one single device. We do not see the underlying layers and how it works. It lets you focus on what the object does instead of how it does it.


abstraction


Abstraction creates an interface to denote common behavior without specifying any details about how that behavior works. In java, abstraction is achieved in two ways (1). by using interface (2) by using abstract class. An interface or abstract class is something which is incomplete. If you want to use it, you need to extend/implement it to make concrete. Classes use the concepts of data abstraction, called as Abstract Data Type (ADT).


Abstract Class:

If a class is declared with abstract keyword, then that class is called as an abstract class. Abstract classes may or may not contain abstract methods. It is not mandatory that abstract class should have at least one abstract method. But, if a class have at least one abstract method, then the class must be declared as abstract class.


An abstract class is something which is incomplete and you can not create an instance of the abstract class. If you want to use it, you need to make it complete by extending it from another class. If you inherit an abstract class you need to provide implementations for all of the parent class's abstract methods in it. However, if you failed to provide implementations, then the subclass must be declared as abstract.


Abstract class declaration:

To declare abstract class, add the abstract keyword to the class declaration. If a class is declared abstract it cannot be instantiated but it can be subclassed. However, If you try to create object for abstract class, the compiler will show error message.


public abstract class AbstractClassSample{ 
    // declare fields and non abstract methods 
    public void method1(){ 
	  //Statements 
    }
    public abstract void method2(); // Abstract method: without
   			 implementation 
}
				

Abstract methods:

To declare abstract method, add abstract keyword in front of the method declaration. It just has a method signature but no implementation. If a class has an abstract method, then that class must be declared as abstract class. Not all methods in an abstract class have to be abstract methods.


Abstract method declaration:
public abstract void method1();  // Abstract method in abstract 
			parent class: without implementation 
				

Child class of an abstract class must implement all abstract methods of its abstract parent class.


public class Sample extends AbstractClassSample {
   public void method1() {
      System.out.println("Abstract method
		  	implementation in child class");
   }
}

If a child class does not have implementations for the abstract methods of its parent class, then the child class also considered as abstract class.


Example:
abstraction


Note: An abstract class cannot be declared as "final". Since if we declare a class as abstract, we can't create object and need to extend that class. But if we declare a class as final, then we can't extend that class.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status