abstraction


Abstraction
Def:-- Hiding or removing unnecessary implementation of the code from the user is known as abstraction.
 For ex:--
In a book shop many books are available, but he will show whatever book we ask and remaining  Books he is hiding.
 For ex:--
  Println () ---  implementing that is Abstraction .
 This is achieved with the pre-defined keyword known as “abstract”. If we are using abstract keyword in a method declaration or prototype we call it as abstract method.
 Ex:--public abstract void display ();
     These abstract method doesn’t have any implementation in the current class.(usually if we take a class, it should have method with body.)
 If a method is abstract then that class also declared as abstract.
For ex:--higher authorities just declare the things & staff follows the implementation.
abstract  Class  A{
abstract void m1 ();
}
Note:- Abstract class cannot be instantiated
·         Abstract method definition is available in the sub-classes.
·         An abstract class may consist of concrete methods also.
·         If any method provided with definition or body we can call it as concrete method.
Ex1:--
abstract class Abs {
       abstract int get();
       void show(){
                   int x=7,y=8;
                   System.out.println("sum is:"+(x+y));
       }
 } 
 class AbsDemo extends Abs {
       int get() {
            return 3;
 }
      public static void main(String[] args) {
                  AbsDemo aa = new AbsDemo();
                  aa.show();
                  int   i = aa.get();
                  System.out.println(i);
      }
}
Differences between normal class and Abstract class
  Abstract class
   Normal class
1.It consists of abstract keyword with class keyword.
1.only with class keyword.
2.It consists of abstract methods as well as concrete methods.
2.consists of only concrete methods.
3.can’t create object directly for these classes.
3.Can create any no of normal objects.
4.Can create reference variables that can refer other classes.
4.can create references also.
5.can have constructor(with or without parameterized)
5.same.
Note :-If the sub-class is unable to provide method definition for the abstract class then our sub-class must be declared as abstract.
 Abstract class provides specification for the sub class as well as provides reusabily.
for abstract classes can't create no object ,but we can give reference of other classes through abstract class.
abstract class Figure {
double dim1;  double dim2;
Figure(double a, double b) {
dim1 = a;  dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}  }
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}  }
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + t.area());
//System.out.println("Area is " + t.area());
//System.out.println("Area is " + r.area());
}  }
Note :-Abstract class can also supports 4 inheritance principles except multiple inheritance.
(check for more detailed examples  on abstraction in class notes )

No comments:

Post a Comment