Unit III - Abstract class in JAVA
Abstract class in Java
Java abstract class is a class that cannot be initiated by itself, it needs to be sub classed
by another class to use its properties. An abstract class is declared using the “abstract”
keyword in its class definition.
A class which is declared with the abstract keyword is known as an abstract class
in Java. It can have abstract and non-abstract methods (method with the body).
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
Example for abstract class and method
package Examples;
abstract class Bank // bank abstract class
{
abstract void interest( ); // abstract method
public void output( ) // non abstract method / concrete method
{
System.out.println("Abstract method");
}
}
class TMB extends Bank // inherit the abstract class bank
{
void interest( ) // override
{
System.out.println("TMB interest is 10%");
}
}
class SBI extends Bank // inherit the abstract class bank
{
void interest( ) // override
{
System.out.println("SMB interest is 15%");
}
}
class Result
{
public static void main(String []arg)
{
Bank obj1=new TMB( );
obj1.output( );
obj1.interest( );
Bank obj2=new SBI( );
obj2.interest( ) ;
}
}
Output
Abstract method
TMB interest is 10%
SBI interest is 15%