Abstraction: Abstraction is the methodology of hiding implementation details from user and only providing the functionality to the users.
- Have idea; but not fully baked
We can not instantiate the abstract class because the methods are not fully implemented.
In order to use the abstract class, extend the abstract class in normal class
There are two types of methods:
- normal method: contains method declaration and implementation
- abstract method: contains method declaration but no implementation, and abstract method ends with semi colon.
- To represent method as abstract, use abstract modifier
There are two types of classes:
- Normal class: contains normal methods
- Abstract class: if a class contain at least one abstract method, that class is said to be abstract class. However, abstract class may or may not contain abstract methods. i.e. if a class has at least one abstract method then it is mandatory to declare class as abstract but if a class is abstract we may write only normal methods in it.
Example I:
//abstract class and abstract method:
abstract class Test3{
void m1() {//normal method
System.out.println("m1 method");//implementation
}
void m2() {
System.out.println("m2 method");
}
abstract void m3();//abstract method
}
abstract class Test4{
void m1() {
System.out.println("m1 method");
}
void m2() {
System.out.println("m2 method");
}
void m3() {
System.out.println("m3 method");
}
public static void main(String[] args) {
}
}
- To represent the class as abstract, use abstract modifier
- object creation is not possible with abstract class
- abstract class may or may not contain abstract method but it is impossible to create the object of abstract class
- it is possible to declare main method inside the abstract class
- declare the class as abstract to prevent the object creation
- it is possible to write constructor inside the abstract class
- program execution always starts from main method
- it is possible to declare instance and static blocks inside the abstract class
Example II:
package com.cerotid.oo.principles;
public class AbstractDemo2 {
public static void main(String[] args) {
System.out.println("main method");
}
}
Abstraction: The methodology of hiding the implementation details from user and only providing the functionality to them. If a class contains one or more than one abstracted method then it is called abstract class. Abstract class:
- must be declared with abstract keyword
- can have abstract and non abstract methods
- can not be instantiated
- can have constructor and static methods
- can have final method
- can not extend multiple classes
An interface is a blueprint of a class which contains static constants and abstract methods. class extends class and class implements interface. But interface extends interface.
Example III:
package com.cerotid.oo.principles;
abstract class Test1{
abstract void m1();
abstract void m2();
abstract void m3();
void m4() {
System.out.println("m4 method");
}
}
class Test2 extends Test1{
void m1() {
System.out.println("m1 method");
}
void m2() {
System.out.println("m2 method");
}
void m3() {
System.out.println("m3 method");
}
}
public class AbstractDemo {
public static void main(String[] args) {
//Test1 t1 = new Test1();// object creation of abstract class is not possible
Test2 t2 = new Test2();
t2.m1();
t2.m2();
t2.m3();
t2.m4();
//Parent p = new Child();
Test1 t = new Test2();
t.m1(); //compile: Test1 runtime: Test2
t.m2(); //compile: Test1 runtime: Test2
t.m3(); //compile: Test1 runtime: Test2
t.m4(); //compile: Test1 runtime: Test2
}
}
Example IV:
package com.cerotid.oo.principles;
abstract class Test5{
abstract void m1();
abstract void m2();
abstract void m3();
}
abstract class Test6 extends Test5{
void m1() {
System.out.println("m1 method");
}
}
abstract class Test7 extends Test6{
void m2() {
System.out.println("m2 method");
}
}
class Test8 extends Test7{
void m3() {
System.out.println("m3 method");
}
}
public class AbstractDemo1 {
public static void main(String[] args) {
Test8 t = new Test8();
t.m1();
t.m2();
t.m3();
}
}
Output:
m1 method
m2 method
m3 method
Example V: The best example ever for abstract class
public class AbstractDemo3 {
public static void main(String[] args) {
//Iphone3 obj = new Iphone(); //can not instantiate the abstract class
Iphone3 obje1 = new Iphone5();
new Iphone5().call();
new Iphone5().takePicture();
new Iphone5().callFacetime();
new Iphone5().takeSelfie();
}
}
abstract class Iphone3{
public void call() {
System.out.println("call");
}
public abstract void takePicture(); //if a method does not contain implementation
public abstract void callFacetime(); //declaring the method and the class with abstract keyword is mandatory
public abstract void takeSelfie();
}
abstract class Iphone4 extends Iphone3{
public void takePicture() {
System.out.println("take picture");
}
}
class Iphone5 extends Iphone4{ //Iphone5 is normal class (concrete class)
public void callFacetime() {
System.out.println("call facetime");
}
public void takeSelfie() {
System.out.println("take selfie");
}
}
Output:
call
take picture
call facetime
take selfie
Example VI: The best example ever for abstract class with real life application
public class AbstractClassDemo {
public static void main(String[] args) {
//creating object of normal class but reference variable is the abstract class reference variable
Bank b1 = new Chase(); //creating reference of Bank and giving the memory of ChaseBank
b1.deposit();
b1.withdraw();
b1.calcualteInterest();
Bank b2 = new Suntrust();
b2.deposit();
b2.withdraw();
b2.calcualteInterest();
}
}
abstract class Bank{
public void deposit() {
System.out.println("Common deposit method implementation accross all banks");
}
public void withdraw() {
System.out.println("Common withdraw method implementation accross all banks");
}
public abstract void calcualteInterest();
}
class Chase extends Bank{
@Override
public void calcualteInterest() {
System.out.println("Chase bank calculate interest implementation");
// TODO Auto-generated method stub
}
}
class Suntrust extends Bank {
@Override
public void calcualteInterest() {
System.out.println("Suntrust bank calcualte interst implementation");
// TODO Auto-generated method stub
}
}
Output:
Common deposit method implementation accross all banks
Common withdraw method implementation accross all banks
Chase bank calculate interest implementation
Common deposit method implementation accross all banks
Common withdraw method implementation accross all banks
Suntrust bank calcualte interst implementation
It is possible to declare constructor inside the abstract class
No comments:
Post a Comment