Professional Documents
Culture Documents
Abstract Class PDF
Abstract Class PDF
Page 2
Java Programming
es
2-2
ss
Java Class Design – Abstract Classes
la
C
ct
ra
st
Ab
Abstrac Class in Java
Page 3
Overview
es
• Use Abstract Classes
ss
• Use the instanceof operator to compare object types
la
C
• Use virtual method invocation
ct
• Use upward and downward casts
ra
st
Ab
3
Abstrac Class in Java
Page 4
Abstract Classes
es
classes extend.
ss
• Abstract classes can provide:
la
– Implemented methods: Those that are fully implemented and
C
their child classes may use them.
ct
– Abstract methods: Those that have no implementation and
ra
require child classes to implement them.
st
4
Abstrac Class in Java
Page 5
Abstract Classes
• An abstract class:
es
– Must be defined by using the abstract keyword.
ss
– Cannot be instantiated into objects.
la
– Can have local declared variables.
C
– Can have method definitions and implementations.
ct
– Must be subclassed rather than implemented.
ra
st
Ab
5
Abstrac Class in Java
Page 6
es
Inheritance precludes inheriting from another class.
ss
• Abstract classes are important when all derived classes
la
should share certain methods.
C
• The alternative is to use an interface, then define at least one
ct
concrete class that implements the interface.
ra
st
• This means you can use the concrete class as an object type
Ab
6
Abstrac Class in Java
Page 7
es
classes or both.
ss
• An abstract class usually has a stronger relationship between
la
itself and the classes that will be derived from it than
C
interfaces.
ct
• Classes and Interfaces can implement multiple interfaces.
ra
Where a class can only be a sub class of one abstract class.
st
Ab
7
Abstrac Class in Java
Page 8
es
class.
ss
public interface InterfaceBankAccount{
la
public final String bank= "JavaBank";
public void deposit(int amt);
C
public void withdraw(int amt);
ct
public int getbalance();
} ra
• We could have used an abstract class instead.
st
Ab
8
Abstrac Class in Java
Page 9
• Interface
es
public interface InterfaceBankAccount
ss
{
public final String bank= "JavaBank";
la
public void deposit(int amt);
public void withdraw(int amt);
C
public int getbalance();
ct
}
• Abstract Class
ra
st
public abstract class AbstractBankAccount {
Ab
9
Abstrac Class in Java
Page 10
es
abstract class over an interface.
ss
• We would be better using an interface especially if all bank
la
accounts had to implement only those defined methods.
C
• The interface approach would also allows us to use other
ct
interfaces in our class design.
ra
st
• If we wanted some base functionality to be defined then we
Ab
10
Abstrac Class in Java
Page 11
es
int balance;
public void deposit(int amt)
ss
{
balance=balance+amt;
la
}
public void withdraw(int amt)
C
{
balance=balance-amt;
ct
}
ra
public void setaccountname(String name)
{
st
accountname = name;
}
Ab
11
Abstrac Class in Java
Page 12
es
an object.
ss
• It takes an object on the left side of the operator and a type
la
on the right side of the operator and returns a boolean value
C
indicating whether the object belongs to that type or not.
• For example:
ct
ra
– (String instanceof Object) would return true.
st
Ab
12
Abstrac Class in Java
Page 13
es
types call methods specific to their type.
ss
public class CustomerAccounts {
la
private InterfaceBankAccount[] bankaccount = new
C
InterfaceBankAccount[10];
public CustomerAccounts() {
ct
InterfaceBankAccount[] b = {
new Account("Sanjay Gupta",11556,300),
ra
new CreditAccount("Sally Walls",66778,1000,500)
st
};
this.bankaccount = b;
Ab
}
}
13
Abstrac Class in Java
Page 14
es
the CreditAccounts in an array and add them to a String
ss
called list.
la
public String getAllCreditAccounts() {
C
String list = "";
for (int i = 0; i < this.bankaccount.length; i++) {
ct
if (this.bankaccount[i] instanceof CreditAccount) {
if (list.length() == 0) {
ra
list += this.bankaccount[i]; }
st
else {
list += ", " + this.bankaccount[i];
Ab
}
}
}
return list;
}
14
Abstrac Class in Java
Page 15
es
– The array is defined using the InterfaceBankAccount interface. It
ss
can hold any class that solely implements its methods and
la
extends them with other specialization methods and variables.
C
• As the program reads through the array, it assigns the string
ct
value from the toString() method to the list variable.
ra
• You should always consider overridding the toString(), hash(),
st
and equals() methods in your user-defined classes.
Ab
15
Abstrac Class in Java
Page 16
es
the type of subclass and calls its implementation of a method.
ss
• When the Java virtual machine invokes a class method, it
la
selects the method to invoke based on the type of the object
C
reference, which is always known at compile time.
ct
• On the other hand, when the virtual machine invokes an
ra
instance method, it selects the method to invoke based on
st
the actual class of the object, which may only be known at
Ab
runtime.
16
Abstrac Class in Java
Page 17
es
...
public String toString() {
ss
String list = "";
for (int i = 0; i < this.bankaccount.length; i++) {
la
if (list.length() == 0) {
C
list += this.bankaccount[i]; }
else {
ct
list += "\n : " +this.bankaccount[i];}
} ra
return super.toString() + "\n" + list + "\n"
st
}
...
Ab
17
Abstrac Class in Java
Page 18
es
• The toString() method uses virtual method invocation by
ss
calling the toString() method of the different subclasses.
la
• The decision is made by JVM to call the toString method
C
implemented in the subclasses rather than the toString
ct
method in the superclass or the Object class.
ra
st
Ab
18
Abstrac Class in Java
Page 19
es
assignment.
ss
• Upcasting loses access to specialized methods in the
la
subclassed object instance.
C
• Downcasting gains access to specialized methods of the
subclass.
ct
ra
st
Ab
19
Abstrac Class in Java
Page 20
es
• For example, when downcasting a double to an int, the values
ss
to the right of the decimal point are lost.
la
• This is a loss of precision.
C
ct
• For that reason, you must explicitly specify the downcast
ra
type.
st
• For example:
Ab
20
Abstrac Class in Java
Page 21
• Upcasting does not risk the loss of precision nor require you
es
to specify the new data type in parentheses.
ss
la
C
ct
ra
st
Ab
21
Abstrac Class in Java
Page 22
Upcasting Example
es
Account so it lost access to its Credit Account specific
ss
methods and fields.
la
• Attempts to access subclass methods fail at compile time.
C
CreditAccount is a subclass of Account.
ct
Account is a superclass.
...
ra
Account account =
st
(Account) new CreditAccount("Sally Walls",66778,1000,500);
Ab
...
account.getcreditlimit();
...
Fails because the method belongs to the
CreditAccount subclass and access to it is lost
when upward casting.
22
Abstrac Class in Java
Page 23
es
primitive data types.
ss
• Upcasting from a specialized subclass to a generalized
la
superclass loses access to the specialized variables and
C
methods. CreditAccount is a subclass of Account.
ct
Account is a superclass.
ra
...
Account account =
st
(Account) new CreditAccount("Sally Walls",66778,1000,500);
Ab
...
account.getcreditlimit();
...
Fails because the method belongs to the
CreditAccount subclass and access to it is lost
when upward casting.
23
Abstrac Class in Java
Page 24
Downcasting Example
es
object is actually a CreditAccount instance.
ss
• The Account instance is first cast to a subclass of itself before
la
running the CreditAccount instance method.
C
CreditAccount is a subclass of Account.
ct
Account is a superclass.
...
ra
Account account =
st
(Account) new CreditAccount("Sally Walls",66778,1000,500);
Ab
...
((CreditAccount)account).getcreditlimit();
...
Succeeds because the method belongs to the
CreditAccount subclass.
24
Abstrac Class in Java
Page 25
es
returns access to the specialized variables and methods if
ss
they were there from a previous upcast.
la
C
CreditAccount is a subclass of Account.
ct
Account is a superclass.
...
ra
Account account =
st
(Account) new CreditAccount("Sally Walls",66778,1000,500);
Ab
...
((CreditAccount)account).getcreditlimit();
...
Succeeds because the method belongs to the
CreditAccount subclass.
25
Abstrac Class in Java
Page 26
Terminology
es
• Abstract class
ss
• instanceof
la
C
• Casting
• Downward cast
ct
ra
• Interface
st
Ab
• Upward cast
• Virtual method invocation
26
Abstrac Class in Java
Page 27
Summary
es
• Model business problems using Java classes
ss
• Use Abstract Classes
la
C
• Use the instanceof operator to compare object types
ct
• Use virtual method invocation
ra
• Use upward and downward casts
st
Ab
27