Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Abstract Classes: Abstract Static Void Print Wrong

An abstract class can contain abstract methods that have no body but must be implemented in subclasses, and abstract classes cannot be instantiated directly but must be extended by subclasses that provide implementations for the abstract methods, with examples given of abstract Account classes for checking and savings accounts that are extended by Checking and Savings subclasses.

Uploaded by

Pavan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Abstract Classes: Abstract Static Void Print Wrong

An abstract class can contain abstract methods that have no body but must be implemented in subclasses, and abstract classes cannot be instantiated directly but must be extended by subclasses that provide implementations for the abstract methods, with examples given of abstract Account classes for checking and savings accounts that are extended by Checking and Savings subclasses.

Uploaded by

Pavan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Abstract Classes

1. An abstract class is a class that can have abstract methods (i.e a


method with only heading with no body of executable statements)
2. We can not create an object of abstract classes i.e abstract class
objects can not be instantiated
3. An abstract class needs to be extended by sub classes to provide
the implementation for the abstract methods.
4. Abstract classes may contain static methods
5. abstract and static keyword combination is wrong
abstract static void print(); wrong
6. Abstract classes may extend either another abstract class or
concrete class
7. Abstract classes may include constructors, nested classes and
interfaces
8. Abstract classes has either public, protected, private or package
accessibility
Abstract Classes
• Syntax :
abstract class <classname>
{
...........................
abstract <return type> methodname(<parameter List>);
abstract <return type> methodname(<parameter List>);
}
Note:
1. Abstract class can have one or more abstract
methods
2. Abstract classes may extend another class ,
implements another interface , may have concrete
methods
Example 1

abstract class A
{
public int show()
{
System.out.println("Class A");
return 0;
}
} What’s wrong with the code

Nothing Wrong

Code will compile


successfully
abstract class A Example 2
{
public int show()
{
System.out.println("Class A");
return 0;
}
}

class sample
{
public static void main(String args[])
{
A a1 = new A();
} What’s wrong with the code
}

sample.java:14: A is abstract; cannot be instantiated


A a1 = new A();
^
abstract class A
{
Example 3
public int show()
{
System.out.println("Class A"); What’s wrong with the code
return 0; sample.java:8: missing method body,
}
or declare abstract
void print();
} // End of class A void print();
^
1 error

abstract class A
{
public int show()
{
System.out.println("Class A");
return 0;
}
abstract void print();
} // End of class A
EXAMPLES ABSTRACT CLASS

abstract class Account


{ <<abstract>> ACCOUNT

private String name;


CHECKING SAVING
private String actno;
private double balance;
private Address addr;

// Overloaded Constructors
Account(String n,String a) { }
Account(String n,String a,double b) { }

// Accessor Methods
String getName() { return name;}
String getactno() { return actno;}
double getbalance() { return balance;}
// provide abstract methods
abstract double withdraw(double amount);
abstract void deposit(double amount);
}
abstract class Account // Accessor Methods
{ String getName() { return name;}
private String name; String getactno() { return actno;}
private String actno; double getbalance() { return balance;}
private double balance;
private Address addr; // Mutator Method only for balance
void setbalance(double amount)
// Overloaded Constructors { this.balance = amount;}
Account(String n,String a) void showAccountDetails()
{ {
name = n; System.out.println("Name :"+this.getName());
actno= a; System.out.println("Account
balance = 0.0; No :"+this.getactno());
} System.out.println("Balance :"+this.getbalanc
Account(String n,String a,double ));
b) }
{ // provide abstract methods
name = n; abstract double withdraw(double amount);
actno= a; abstract void deposit(double amount);
balance = b; } // END OF Account CLASS
}
class Saving extends Account
{
Saving(String n,String a)
{
super(n,a);
System.out.println("Saving Account Created");
System.out.println("Name :"+this.getName());
System.out.println("Account No :"+this.getactno());
System.out.println("Balance :"+this.getbalance());
showAccountDetails();
}
Saving(String n,String a,double b)
{
super(n,a,b);
System.out.println("Saving Account Created");
System.out.println("Name :"+this.getName());
System.out.println("Account No :"+this.getactno());
System.out.println("Balance :"+this.getbalance());
showAccountDetails();
}
double withdraw(double amount)
{
/*
if( balance == 0) return 0.0;
if( balance < amount ) return 0.0;
balance = balance - amount;
*/

if(this.getbalance() == 0) return 0.0;


if(this.getbalance() < amount ) return 0.0;
setbalance(getbalance() - amount);
return amount;
}
void deposit(double amount)
{
setbalance(getbalance() + amount);
return ;
}
}//end of Saving class
class Checking extends Account
{
Checking(String n,String a,double b)
{
super(n,a,b);
System.out.println("Checking Account
void deposit(double amount)
Created");
{
showAccountDetails();
setbalance(this.getbalance() + 0.9 *
}
amount) ;
double withdraw(double amount)
return ;
{
}
/*
}//end of Checking class
if( balance - 100 == 0) return 0.0;
if( balance -100 < amount ) return 0.0;
balance = balance - amount - 100;
*/

if(this.getbalance() - 100 == 0) return 0.0;


if(this.getbalance() - 100 < amount ) return 0.0;
setbalance(this.getbalance() - amount);
return amount;
}
class AccountTest
{
public static void main(String args[])
{
Checking c1 = new Checking("Rahul Sharma","C106726",100000);
Checking c2 = new Checking("Raman Kumar","C106727",100000);

Saving s1 = new Saving("Kumar Sharma","S106726",100000);


Saving s2 = new Saving("Mohan Lal","S106727");

c1.withdraw(2000);
c1.showAccountDetails();
c2.deposit(10000);
c2.showAccountDetails();
s1.deposit(900);
s1.showAccountDetails();
s2.withdraw(400);
s2.showAccountDetails();
}
}

You might also like