xxxxxxxxxx
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
xxxxxxxxxx
Abstraction is nothing but the quality of dealing with ideas rather than
events. It basically deals with hiding the internal details and showing
the essential things to the user.
xxxxxxxxxx
class MyMainEmployee{
private int id;
private String name;
public MyMainEmployee(int id, String name){ // Constructor
setId(id); // use abstraction to hide setter and for low coupling
this.name = name;
}
private setId(int id){ // use setter and validator together
if (id>=0){
this.id = id;
}
else {
throw IllegalArgumentException("Id cannot be negative");
}
}
}
xxxxxxxxxx
Sometimes we may come across a situation
where we cannot provide implementation to
all the methods in a class. We want to leave the
implementation to a class that extends it.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBase class as super
class of the all page classes.
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
Also
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
override all methods of abstract class
4)An abstarct class can contain either
0 or more abstract method.
java abstraction simple example
xxxxxxxxxx
// ***** abstraction problem solved ********
abstract class shape{ // abstract class
double a, b;
shape(double a, double b){ // abstract class constructor
this.a = a;
this.b = b;
}
abstract void area(); // abstract class Method
}
class triangle extends shape{
//a,b, area(); // Already abstract class property inheritance
triangle(double a, double b){ // triangle constructor
super(a,b); // super class used to code re-usability
}
void area() { // abstract method over-right to only void method
double result = 0.5 * a * b; // triangle formula
System.out.println("Triangle area: " + result);
}
}
class rectangle extends shape{
//a,b, area(); // Already abstract class property inheritance
rectangle(double a, double b){ // rectangle constructor
super(a,b); // super class used to code re-usability
}
void area() {
double result = a * b; // rectangle formula
System.out.println("Rectangle area: " + result);
}
}
class circle extends shape{
//a,b, area(); // Already abstract class property inheritance
circle(double r){ // circle constructor but circle 1 parameter
super(r,r); // super class used to code re-usability but 2 parameter constructor
}
void area() {
double result = Math.PI * a * b; // circle formula
System.out.println("Circle area: " + result);
}
}
public class main{
public static void main(String[] args) {
shape obj; // abstract class reference variable create
obj = new rectangle(12, 34); // shape class reference variable to rectangle class
obj.area(); // rectangle class shape class abstract area() method over-right to rectangle class
obj = new triangle(12, 34); // shape class reference variable to triangle class
obj.area(); // rectangle class shape class abstract area() method over-right to triangle class
obj = new circle(12); // shape class reference variable to circle class
obj.area(); // rectangle class shape class abstract area() method over-right to circle class
}
}