Categories: Java

Abstract Class & Method in Java

In Java, an abstract class is specified with the abstract keyword. Both abstract and non-abstract methods can be used (method with the body).
Note: Abstraction is the process of hiding implementation details from the user and only displaying functionality.

  • A class that is declared using the abstract keyword is known as an abstract class.
  • Abstract classes are identical to regular classes, except they can also feature abstract methods in addition to concrete methods.
  • We are unable to generate abstract class objects.
  • The code below creates the abstract class MyAbstractClass.
 abstract class MyAbstractClass {
    // Class body
 }  
  • The abstract keyword must be used when declaring an abstract class.
  • There are both abstract and non-abstract methods that can be used.
  • It isn’t possible to instantiate it.
  • This class may also include constructors and static methods.
  • It can have final methods, which prevent the subclass from changing the method’s body.
Abstract Method in Java:

An abstract method is a method that is declared as abstract but does not have any implementation.

An example of an Abstract class that has an abstract method

Bike, in this case, is an abstract class with only one abstract method, run. The Honda class is responsible for its implementation.

abstract class Bike{  
  abstract void run();  
}  
class Honda4 extends Bike{  
void run(){System.out.println("running safely");}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  
}  
Output:
running safely
Bank example using abstract class & method:
abstract class Bank{    
abstract int getRateOfInterest();    
}    
class SBI extends Bank{    
int getRateOfInterest(){return 8;}    
}    
class PNB extends Bank{    
int getRateOfInterest(){return 6;}    
}    
    
class TestBank{    
public static void main(String args[]){    
Bank b;  
b=new SBI();  
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
b=new PNB();  
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
}}    
Output:
Rate of Interest is: 8 %
Rate of Interest is: 6 %

Note: also read about the Sub packages in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago