0% found this document useful (0 votes)
8 views4 pages

Abstract Class

An abstract class in programming contains abstract methods without implementations and can also have concrete methods. Subclasses must implement all abstract methods from the abstract class, and an abstract class cannot be instantiated directly. The document provides examples of abstract classes and methods, demonstrating their usage in scenarios such as animal behavior and banking operations.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Abstract Class

An abstract class in programming contains abstract methods without implementations and can also have concrete methods. Subclasses must implement all abstract methods from the abstract class, and an abstract class cannot be instantiated directly. The document provides examples of abstract classes and methods, demonstrating their usage in scenarios such as animal behavior and banking operations.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Abstract Class

Abstract class- an abstract class is an class which contains an abstract keyword. It contains
both abstract method as well as normal methods(Concrete methods).
Abstract method - A method which is declared as abstract and does not have implementation
is known as an abstract method.
Example of abstract method

abstract void printStatus();//no method body and abstract

The abstract method is implemented(body) in the subclass where the abstract class in
inherited. All subclasses of the abstract class must inherit the abstract method compulsory,
where overriding becomes compulsory in abstract class.

Syntax
abstract class classname{
abstract methods;
concrete methods { }
}

 A class which contains the abstract keyword in its declaration is known as abstract
class.
 Abstract classes may or may not contain abstract methods, i.e., methods without body
( public void get(); )
 But, if a class has at least one abstract method, then the class must be declared
abstract.
 If a class is declared abstract, it cannot be instantiated.
 To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
 If you inherit an abstract class, you have to provide implementations to all the abstract
methods in it.

1. Create an abstract class 'Animals' with two abstract methods 'cats' and 'dogs'. Now
create a class 'Cats' with a method 'cats' which prints "Cats meow" and a class 'Dogs'
with a method 'dogs' which prints "Dogs bark", both inheriting the class 'Animals'. Now
create an object for each of the subclasses and call their respective methods.
abstract class Animals{
abstract void cat();
abstract void dog();
}
class Cats extends Animals{
void cat(){
System.out.println("Cats meow ");
}
void dog(){ }
// if you don’t want to use this method then also you need to implement it with empty
body
}
class Dogs extends Animals{
void cat(){ }
// if you don’t want to use this method then also you need to implement it with empty
body
void dog(){
System.out.println("Dogs bark ");
}
}
public class animaldemo{
public static void main(String args[]){
Animals c=new Cats();
c.cat();
Animals d=new Dogs();
d.dog();
}
}
2. Consider an banl application in which you have a class BankAccount that has a
method deposit() and withdraw() and the subclasses of it like SavingsAccount,
CurrentAccount, FixedDepositAccount, etc. Since the process of deposit and
withdrawal differs from one account to another, there is no point to implement these
two methods in the parent class BankAccount. This is because every child class must
override these methods and provide an implementation of them. Hence we make the
BankAccount class as abstract class in which methods deposit() and withdraw() will
be abstract method which should be overridden in all BankAccount subclasses.

//abstract parent class


abstract class BankAccount
{
static double balance;
//abstract methods
public double getbalance(){
return this.balance;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
//SavingsAccount class extends BankAccount class
class SavingsAccount extends BankAccount
{
public void deposit(double amount)
{
System.out.println("\nThis is the concrete deposit method of
SavingsAccount\n"+amount+" is Deposited successfully ");
super.balance+=amount;
System.out.println("Current balance is "+super.getbalance());
}
public void withdraw(double amount)
{
System.out.println("This is the concrete withdraw method of SavingsAccount\n");
if(amount>super.balance)
System.out.println("insuffient balance");
else{
super.balance-=amount;
System.out.println("Current balance is "+super.getbalance());
}

}
}
//CurrentAccount class extends BankAccount class
class CurrentAccount extends BankAccount
{
public void deposit(double amount)
{
System.out.println("\nThis is the concrete deposit method of
CurrentAccount\n"+amount+" is Deposited successfully ");
super.balance+=amount;
System.out.println("Current balance is "+super.getbalance());
}
public void withdraw(double amount)
{
System.out.println("This is the concrete withdraw method of CurrentAccount\n");
if(amount>super.balance){
System.out.println("insuffient balance, overdraft");
super.balance-=amount;
System.out.println("Current balance is "+super.getbalance());
}
else{
super.balance-=amount;
System.out.println("Current balance is "+super.getbalance());
}
}
}

public class AbstractClassDemo


{
public static void main(String args[])
{
BankAccount.balance=5000.00;
BankAccount obj = new SavingsAccount();
obj.deposit(200.00);
obj.withdraw(5000.00);
BankAccount obj1 = new CurrentAccount();
obj1.deposit(500.00);
obj1.withdraw(2000.00);
}
}

Assignment
1. We have to calculate the area of a rectangle, a square and a circle. Create an abstract
class 'Shape' with three abstract methods namely 'RectangleArea' taking two
parameters, 'SquareArea' and 'CircleArea' taking one parameter each. The parameters
of 'RectangleArea' are its length and breadth, that of 'SquareArea' is its side and that
of 'CircleArea' is its radius. Now create another class 'Area' containing all the three
methods 'RectangleArea', 'SquareArea' and 'CircleArea' for printing the area of
rectangle, square and circle respectively. Create an object of class 'Area' and call all
the three methods.
2. We have to calculate the percentage of marks obtained in three subjects (each out of
100) by student A and in four subjects (each out of 100) by student B. Create an
abstract class 'Marks' with an abstract method 'getPercentage'. It is inherited by two
other classes 'A' and 'B' each having a method with the same name which returns the
percentage of the students. The constructor of student A takes the marks in three
subjects as its parameters and the marks in four subjects as its parameters for student
B. Create an object for eac of the two classes and print the percentage of marks for
both the students.
3. Why the below class is showing compilation error?
abstract class AbstractClass
{
abstract void abstractMethod()
{
System.out.println("First Method");
}
}
4. What is the output of the following code?
abstract class A{
abstract void firstMethod();
void secondMethod() {
System.out.println("SECOND");
firstMethod();
}
}
abstract class B extends A{
void firstMethod() {
System.out.println("FIRST");
thirdMethod();
}
abstract void thirdMethod();
}
class C extends B{
void thirdMethod() {
System.out.println("THIRD");
}
}
public class MainClass{
public static void main(String[] args) {
C c = new C();
c.firstMethod();
c.secondMethod();
c.thirdMethod();
}
}

You might also like