This post covers the topic Abstraction
ABSTRACTION:
- Hiding the implementation part is called abstraction
- It has 2 types,
- Partial abstraction (abstract class)
- Full abstraction (interface)
1. Partial Abstraction (Abstract class):
- It will support abstract method and non-abstract method.
- We can’t create object for abstract class because in the method signature we didn't mention any business logic.
- In abstract method, we only mention abstract signature, won't create business logic
- It have 2 class, abstract class (sub class) and super class. we create object and business logic only in super class, won't create in abstract class
Example Program:
abstract class
public abstract class Bank {abstract void saving(); //method signature
abstract void current();
abstract void salary();
abstract void joint();
public void branchDetails(){
System.out.println("chennai");
}
}
super class
public class AxisBank extends Bank {public void saving() { // method signature
System.out.println("saving regular"); // business logic
}
public void current() {
System.out.println("current");
}
public void salary() {
System.out.println("salary");
}
public void joint() {
System.out.println("joint");
}
public static void main(String[] args) {
AxisBank info = new AxisBank();
info.branchDetails();
info.salary();
info.saving();
}
}
Output:
chennai
salary
saving
2. INTERFACE / FULL ABSTRACTION;
- It will support only abstract method, won't support non abstract method
- In interface "public abstract" is default. we no need to mention
- It is using implements keywords
Example Program:1
interface
public interface Bank {
abstract void saving();
abstract void current();
abstract void salary();
abstract void joint();
public void branchDetails();
}
super class
public class AxisBank implements Bank {
public void saving() {
System.out.println("saving regular");
}
public void current() {
System.out.println("current");
}
public void salary() {
System.out.println("salary");
}
public void joint() {
System.out.println("joint");
}
public void branchDetails() {
System.out.println("chennai");
}
public static void main(String[] args) {
AxisBank info = new AxisBank();
info.branchDetails();
info.salary();
info.saving();
}
}
Output:
chennai
salary
saving regular
- multiple inheritance its won’t support in java but using interface its support
- here we have to create 2 interface(super class) and one sub class(normal). In the sub class we implement both interface
Example Program:2
interface
public interface ABank {
public void test();
}
public interface HBank {
public void test();
}
sub class (normal class)
public class Bank implements ABank, HBank{
@Override
public void test() {
}
}
Abstract class | Interface |
---|---|
Partial abstraction | Full abstraction |
It support both abstract method and non-abstract method | It support only abstract method |
It’s using "extends" keyword | It’s using "implement" keyword |
Here "public abstract" have to mention | "public Abstract" is default. no need to mention |
We can use whatever access specifier we want | Here we use only public (access specifier) |
Comments
Post a Comment