Easy Tutorial
For Competitive Exams

Java Programming Abstraction

What is Absraction?

Abstraction is process of hiding the implementation details and showing only the functionality.

Hiding of data is known as data abstraction.

We use abstraction every day when interacting with technological objects.

Example :Sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.

What is Abstract class?

A class that is declared as abstract is known as abstract class.

If a class contain any abstract method then the class is declared as abstract class.

An abstract class is never instantiated. It is used to provide abstraction.

Although it does not provide 100% abstraction because it can also have concrete methods.

Syntax

abstract class class_name { }

What is Abstract method?

A method that is declare as abstract and does not have implementation is known as abstract method.

Any class that extends an abstract class must implement all the abstract methods declared by the super class.

Syntax

abstract  return_type  function_name();    // No definition

Example

abstract class A{
abstract void callme(); 
}
class B extends A{
void callme(){
System.out.println("this is callme."); 
 }
public static void main(String[] args){
  B b = new B();
  b.callme();
   }
}
Try it Yourself

Can Abstract class have concrete(normal) method?

Yes,Abstract class can also have normal methods with definitions, along with abstract methods.

Example

abstract class A{
abstract void callme();
public void normal(){
System.out.println("this is concrete method");
     }  
}
class B extends A{
void callme(){
System.out.println("this is callme."); 
 }
public static void main(String[] args){
  B b = new B();
  b.callme();
  b.normal();
  }
}
Try it Yourself

Solve this!!

11560.Which of these is not a correct statement?
Every class containing abstract method must be declared abstract.
Abstract class can be initiated by new operator.
Abstract class can be inherited.
All of the above.
Explanation:
Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.
11561.Which of the following declares an abstract method in an abstract Java class?
public abstract method();
public abstract void method();
public void method() {}
public abstract void method() {}
11562.What is the output of this program?
class A {
        int i;
        void display() {
            System.out.println(i);
     }
}    
class B extends A {
        int j;
        void display() {
            System.out.println(j);
     }
}    
class method_overriding {
       public static void main(String args[]){
            B obj = new B();
            obj.i=1;
            obj.j=2;   
            obj.display();     
    }
}
1
2
3
4
Share with Friends