Abstraction in Java 7

This section elaborate about abstraction. It is one of OOPs concept.

Abstraction in Java 7

This section elaborate about abstraction. It is one of OOPs concept.

Abstraction in Java 7

Abstraction in Java 7

This section elaborate about abstraction. It is one of OOPs concept.

Abstraction :

 Abstraction means hiding the unnecessary information to the user and show only essential features of a particular concept or object.
 In java, you can see concept of abstraction whenever you use

  • Interface or abstract class.
  • Object of any class
  • Using access modifiers to hide information

Abstract Class : Abstract class is a class which is partially implemented. So you can say abstract class is an incomplete class which can't be instantiated

  • A abstract keyword is used before the class.
  • You can't insatiate the abstract class.
  • You must implement all methods where you inherit the abstract class.

For Example -

  abstract class Class1{
                ......
                 }

Abstract Method :  Abstract method is a method which is only declared not implemented. So if you want to implement any method in its subclass(child class) just use abstract keyword before the declared method but don't implement there.

If you declare any method abstract it is necessary to declare class abstract too.

For Example :

abstract void method1();

Example : Here we are giving a simple example which shows the abstraction concept.

AbstractClass.java - This is abstract class having two abstract methods.

package abstraction;

//abstract class
abstract class AbstractClass {
	int a;
	int b;

	// abstract method
	abstract void methodA();

	void methodB(int b) {
		b = this.b;
		System.out.println("Value of b= " + b);
	}

	// abstract method
	abstract void methodC();
}

ExtendAbstract.java - In this class we are extending the abstract class and implementing its abstract methods.

package abstraction;

class ExtendAbstract extends AbstractClass {

	void methodA() {
		System.out.println("Value of a= " + a);
	}

	@Override
	void methodC() {
		System.out.println("This is another abstract method.");

	}

	void display() {
		System.out.println("This class implementing all abstract methods of abstract class.");
	}

}

MainClass.java This is your main class.

package abstraction;

class MainClass{
	public static void main(String[] args){
		 ExtendAbstract abstract1=new ExtendAbstract();
		 abstract1.a=10;
		 abstract1.b=20;
		 abstract1.display();
		 abstract1.methodA();
		 abstract1.methodB(abstract1.b);
	}
}

Output :

This class implementing all abstract methods of abstract class.
Value of a= 10
Value of b= 20