Sunday, February 7, 2021

Abstract java | Abstract class in Java | Abstraction in oops-AapKiApniSchool

 

In this blog we will discuss what is Abstract class? How Abstract class is linked with Abstraction? We will check the use of Abstract class in Java. Also we will discuss why do we use Abstract class in Java?We will understand the real scnearion of Abstract class with examples. We will discuss Abstraction in java with example. We will unstandard the purpose of abstraction in OOP and also we will check that we can create constructor in abstract class.

 

Table of Abstract class:

 

  1. What is Abstraction? 

     1.1 Abstraction in java with real time example.

    1.2 How do you achieve abstraction?

  2. Abstract Class

    2.1 Use of Abstract Class

    2.2 Why do we use Abstract class in Java?

   2.3 What is the synatx of Abstract Class in Java?

   2.4 Abstract method in java

  2.5 Where do we use abstract class in real time?

 2.6 Can abstract class have constructor?

 2.7 What is the difference between data hiding and data abstraction?

 

Abstraction in Java: 

Abstraction is a process that hide the implementation and show only functionality. For example you have a car and you know how to drive car but how exactly internally clutch,brake and gear box work, we don't know. And it is not needed for driving car.Here implemetation of clutch,brake and gear is hidden. We know only functionality. Abstract class is sed to implement Abstraction process.

Ways to achieve Abstraction


There are two ways to achieve abstraction in java

 

  • Abstract class (0 to 100%)
  • Interface (100%)

 

Abstract java:

If any java class is declared with abstract keyword that class is called Abstract class. Abstract class can contain abstract method and non-abstract method. Abstract method means method is declared but don't have any body.Non-Abstract method have body.Abstract method is declared with Abstract keyword. Abstract method can declare only in abstract class.
 

Key Point Of Abstract Class:

 
  • It can have abstract and non-abstract methods.
  • It can contain static and non-static methods. 
  •  We can't create object of abstract class.
  •   It can have final methods and constructor. 
 

Use of Abstract class:

 
Abstract class is used for implementing abstraction. It hide the complexity and show only functionality. For example: Car. A car is having  lots of . Like braking system,gear etc. Car is hiding implementation as we don't know how braking systems or gear is working. Without knowing implementation we can use object this is called Abstraction that is implemented by Abstract class.
 

Why do we use Abstract class in Java?

Abstract class in java is used to achive Abstaraction that hide complexity.

 

What is the synatx of Abstract Class in Java?

abstract class A{

}

 

Abstract method in java:

In this class vehicle is an abstract class that is containg one abstract method. This class implementation is given by Car class.

 

abstract class Vehicle{
abstract void run();
}
class Car extends Vehicle{
void run(){System.out.println("Car running safely");}   
public static void main(String args[]){
Vehicle obj = new Car ();
obj.run();
}
}
 

Output:

 Car running safely

 

Abstract class having constructor, data member and methods:

Abstract class can contain constructor,data member,abstract method,non-abstract method and static methods also and even main method.
 
File: Test.java
 
package youtubeClass;
 
public class Test {
 
public static void main(String[] args) {
 Bank bank= new SBI();
 
 System.out.println("Sbi Bank Interest Rate "+bank.getFdInterestRate());
 bank.rbiStndardRules();
 
 bank= new CBI();
 
 System.out.println("Cbi Bank Interest Rate "+bank.getFdInterestRate());
 
 bank.rbiStndardRules();
}
}
 
 
 
File:SBI.java
 
package youtubeClass;
 
public class SBI extends Bank {
 
@Override
int getFdInterestRate() {
int totalInterestRate=minimumInterestRate+5;
return totalInterestRate;
}
 
}
 
 
File: CBI.java
 
package youtubeClass;
 
public class CBI extends Bank {
 
@Override
int getFdInterestRate() {
int totalInterestRate=minimumInterestRate+4;
return totalInterestRate;
}
 
}
 
 
File: Bank.java
 
package youtubeClass;
 
public abstract class Bank {
 
static int minimumInterestRate=10;
public Bank() {
System.out.println("Abstract Class constructor called");
}
 
abstract int getFdInterestRate();
 
void rbiStndardRules()
{
System.out.println("Rbi Standard Rules");
}
 
}
 
 
 

Output:

 
Abstract Class constructor called
Sbi Bank Interest Rate 15
Rbi Standard Rules
Abstract Class constructor called
Cbi Bank Interest Rate 14
Rbi Standard Rules
 
 

Another real scenario of abstract class: 

The best example of abstract class is Java itself as we know the uses of java class but how these classes internally implemented it is hidden for us.

 
 

Can abstract class have constructor?

Abstract class can have constructor, methods with body or Without body(Abstract method). We can not create object of Abstract class.
 
 

What is the difference between data hiding and data abstraction?

 
The main difference is that data hiding is used to secure data but abstraction is used to hide complexity and show only functionality. Data hiding is implemented by encapsulation. Where we encapsulate our data member and methods in single unit. That unit is called class in Java.
 
 

Where do we use abstract class in real time?

 
The best example of abstract class in real time is ATM. We can withdraw money,check balance,register mobile number etc. Without knowing implementation of this operations. It provide the security from unauthorized object. It is showing only functionality and hiding complexity.
 
 
 

 

 
 
 
 

No comments:

Post a Comment

Featured Post

Interface in java-AapKiApniSchool

Interface in java:  interface is a blueprint of class in java that is used to define contracts. It can contain static constant and abstract ...

Popular Posts