Pages

Friday, May 8, 2020

Abstraction in Java

Index:

1. What is Abstraction?
2. How to achieve Abstraction?
3. What is Interface?
4. What is Abstract class ?
5. Difference between Abstract class and Interface.
6. When to use interface, abstract class and concrete class?
7. Advantages of Abstraction.



1. What is Abstraction?

One of the pillar in object oriented programming. Exploring only required things to user. 

E.g.  ATM GUI Screen. We can see all options to check balance, withdraw money etc but can’t see code behind that. This shows that it will explore only required thing to user not internal implementations.


2. How to achieve Abstraction?

Abstraction in OOP can be achieved as below:

How to achieve Abstraction in java?



Abstraction mainly achieve by using Interface and Abstract class.
Let’s see one by one:

3. Interface :

1. Any Software requirement specification (SRS) called as interface. Blueprint of class.

E.g. Third party API i.e. Sun Micro System define JDBC-API and database vendors (Oracle/mysql etc) provides implementations for that.

Also Sun Micro System defines servlet-API to develop web application and web server vendor provides implementation to it.

2. It is used to achieve 100% abstraction.

3. It is same as class but does NOT have constructor.
           Eg.       interface Ai {
                       Ai(){       //Compile time error
     }
                 }

4. It can compile but can NOT run.

5. We Can Not create an object of interface but can create reference variable.
E.g.

Ai a1 = new Ai(); ----- Compile time error.
Ai a1 ; ---- correct.

6. All methods in interface must be public abstracts (if we not write then JVM will put it by default).
E.g.     public abstract m1(); ----- correct.

            interface Ai {
                 m1();       //even we haven’t assign public abstract, JVM puts                                               automatically public abstract to method.

}


7. Variables in interface must be public final static.

                Eg.       interface Ai {
                          int i;
                                }

 JVM puts public final static if we don’t put in code as below:
  
       interface Ai{
              public final static int i;
                   }


8. Can extends one or more interface but can NOT extends class.

9. Support Multiple inheritance but classes are not.

10. If we want to use method in interface then implement that method by implement interface by any normal class.

11. Interface without any method called as marker interface or tag interface or ability interface. RandomAccess (I), Cloneble (I), Serializable (I) are marker interface.

(Question: Clonable/Serializable Interface does not contains any method then what is use of that interface?
Answer: Clonable and Serializable Interface does not contain any method but it provide a Capability to Cloning of an object and Serialization of an object respectively.)

12. Interface act as contract between client and server:
E.g. ATM GUI screen is provided by bank at a same time the same GUI screen is served to customer. Since ATM GUI acts as contract between bank and customer.

13. We can define number of interfaces and class inside one interface.


Program of interface:

interface Bank {
     float rateOfInterest();
}

class SBI implements Bank {
     public float rateOfInterest() {
          return 6.25f;
     }
}

class ICICI implements Bank {
     public float rateOfInterest() {
          return 7.1f;
     }
}


class Test {
     public static void main(String[] args) {
          Bank sbi = new SBI();
          System.out.println("ROI of SBI: " + sbi.rateOfInterest());

          Bank icici = new ICICI();
          System.out.println("ROI of ICICI: " + icici.rateOfInterest());
     }
}

Output:
ROI of SBI: 6.25
ROI of ICICI: 7.1



4. Abstract Class:


1. Abstract keyword or modifier is used to achieve an abstraction partially. Java abstract data type (ADT) in a data structure (DS) is a type of data type.

2. It is same as that of ordinary/normal class but can NOT create an object of abstract class i.e. Abstract class can not be instantiated.

3. It mainly consist of abstract methods or concrete or both or none.

4. To implement abstract methods in abstract class, we have to extend that class by any Normal class.

5. If any method in any class is abstract then class must be abstract.
     (Method=abstract then Class=abstract)

6. Final- abstract combination is NOT allowed.
   
    Eg. public abstract final int a =0; ---> compile time error
      (final topic covered in different section)

7. Abstract class can compile and run.

8. Why we NOT create an object of abstract class in java?
Answer: Because as abstract class contains abstract methods also.
If JVM allows to create an object of abstract class then anyone can able to call that abstract method which does not have body/implementation. So there will not any sense to call abstract method. Hence JVM will not allow to create an object.
E.g. of abstract classes: Httpservlet, Generic servlet etc.



Program of abstract class:

abstract class Area {
     abstract void calculateArea();
}

//In real scenario, implementation is provided by others
//i.e. unknown by end user 

class Rectangle extends Area {
     void calculateArea() {
          System.out.println("Rectangle area: b*h");
     }
}

class Triangle extends Area {
     void calculateArea() {
          System.out.println("Triangle area: 0.5*b*h");
     }
}

//In real programming time, method is called by programmer or user 
class Test {
    
     public static void main(String args[]) {
    
     Area triangle = new Triangle();
     triangle.calculateArea(); //Calculate Area of triangle

     Area rectangle = new Rectangle();
     rectangle.calculateArea(); //Calculate Area of rectangle
     }
}

Output:
Triangle area: 0.5*b*h
Rectangle area: b*h

  

5. Difference between abstract class and interface:



Abstract class
Interface
1
We can achieve abstraction partially.
We can achieve fully abstraction.
2
Abstract class consist of abstract as well as concrete method.
Interface contain abstract methods only.
3
Abstract class can NOT achieve multiple inheritance.
Interface can achieve multiple inheritance.
4
Abstract class can extend but can NOT implement by class.
Interface can extend by interface and implement by class.
5
Abstract class has constructor but can NOT create object.
Not have constructor
6
Can compile and run.
Can compile only and not run.
7
e.g. Httpservlet class etc.
e.g. List, Set, Map etc.



6. When to use interface, abstract class and concrete class:

(OR in which case we should go for interface, abstract class and concrete class)

1. Interface: when we don’t know anything about implementation i.e. we just have requirement specification (100% abstraction) then we should go for interface.
e.g. Servlet interface

2. Abstract class: if we are talking about implementation but not completely (i.e. 0 to 100% abstraction/partial implementation) then we should use abstract class.
e.g. Httpservlet, Genericservlet

3. Concrete class: If we are talking about fully implementation and ready to provide service then we should go for concrete class.
E.g. String class, Class Student etc



7. Advantages of Abstraction:


Advantages of Abstraction


1. We can achieve security as outside people can't see out internal implementations.

2. Enhancement becomes very easy i.e. without effecting/impacting end user we can perform any type of changes in our internal system/existing code.

3. Improves maintenance of an application.

4. Provides more flexibility to end users to use system very easily.

5. Improves modularity and easiness of system.



Please Note : All interview questions and maximum programs on this topic will be covered in another section. 



No comments:

Post a Comment

**** Please let me know if you have any doubts. ****