SlideShare a Scribd company logo
1 of 70
Download to read offline
COMP 213 
Advanced Object-oriented Programming 
Lecture 16 
Abstract Classes
Abstract Classes for Generic Solutions 
There is still scope for making our Operators class more 
concise. 
The getPrecedence() methods all have the same form. 
For ‘and’: 
in class AndOperator 
public int getPrecedence() { 
return AND PREC; 
} 
For ‘or’: 
in class OrOperator 
public int getPrecedence() { 
return OR PREC; 
} 
etc.
Abstract Classes for Generic Solutions 
There is still scope for making our Operators class more 
concise. 
The getPrecedence() methods all have the same form. 
For ‘and’: 
in class AndOperator 
public int getPrecedence() { 
return AND PREC; 
} 
For ‘or’: 
in class OrOperator 
public int getPrecedence() { 
return OR PREC; 
} 
etc.
Digression: Generic vs Particular 
Consider a tool that lets a user draw shapes on a canvas: 
circles, squares, rectangles, ovals, etc. 
Suppose the design of such a tool has reached a stage where 
the following classes have been identified: 
Canvas — the ‘drawing area’ 
Color — to represent different colours for shapes 
AbstractShape — to represent the drawn shapes, 
with subclasses: 
Circle 
Square, etc.
Digression: Generic vs Particular 
Consider a tool that lets a user draw shapes on a canvas: 
circles, squares, rectangles, ovals, etc. 
Suppose the design of such a tool has reached a stage where 
the following classes have been identified: 
Canvas — the ‘drawing area’ 
Color — to represent different colours for shapes 
AbstractShape — to represent the drawn shapes, 
with subclasses: 
Circle 
Square, etc.
Digression: Generic vs Particular 
Consider a tool that lets a user draw shapes on a canvas: 
circles, squares, rectangles, ovals, etc. 
Suppose the design of such a tool has reached a stage where 
the following classes have been identified: 
Canvas — the ‘drawing area’ 
Color — to represent different colours for shapes 
AbstractShape — to represent the drawn shapes, 
with subclasses: 
Circle 
Square, etc.
Generic vs Particular 
The AbstractShape class is to have the following fields: 
Color color 
Point anchorPoint 
where anchorPoint represents some location representing the 
position of the shape on the canvas 
this might be the centre of a Circle, the top-left corner of a 
Square, etc.
Generic vs Particular 
The AbstractShape class is to have the following methods: 
void move(int dx, int dy) — to move a shape on the canvas 
void draw(Canvas c) — to actually draw the shape on the 
canvas
Generic vs Particular 
The tool will store all the drawn shapes in an array: 
AbstractShape[] shapes; 
Whenever necessary (e.g., refreshing a screen), all the shapes 
on the canvas can be drawn by 
for (int i=0; i < shapes.length; i++) { 
shapes[i].draw(theCanvas); 
} 
Thus all the shapes can be drawn in a generic way.
Generic vs Particular 
The tool will store all the drawn shapes in an array: 
AbstractShape[] shapes; 
Whenever necessary (e.g., refreshing a screen), all the shapes 
on the canvas can be drawn by 
for (int i=0; i < shapes.length; i++) { 
shapes[i].draw(theCanvas); 
} 
Thus all the shapes can be drawn in a generic way.
Generic vs Particular 
However, actually drawing the shapes is not generic: 
the code for drawing a Circle will be very different from the code 
for drawing a Square, and so on. 
(Sound familiar?) 
We might include a ‘dummy’ method in AbstractShape, which is 
overridden in the subclasses Circle, Square, etc.
Generic vs Particular 
However, actually drawing the shapes is not generic: 
the code for drawing a Circle will be very different from the code 
for drawing a Square, and so on. 
(Sound familiar?) 
We might include a ‘dummy’ method in AbstractShape, which is 
overridden in the subclasses Circle, Square, etc.
Abstract Shapes 
public class AbstractShape { 
private Color color; 
private Point anchorPoint; 
public void move(int dx, int dy) { 
anchorPoint.move(dx,dy); 
} 
public void draw(Canvas c) { } 
}
Circles 
This dummy method will be overridden in the subclasses. 
E.g., 
public class Circle extends AbstractShape { 
private int radius; 
public void draw(Canvas c) { 
// code to draw the circle 
} 
}
Overriding 
The method Circle.draw() overrides the method 
AbstractShape.draw() 
if shapes[i] happens to be an instance of Circle, then 
shapes[i].draw(theCanvas); 
executes the code in Circle.draw(). 
Similarly, if Square overrides AbstractShape.draw(), and 
shapes[i] happens to be an instance of Square, then the 
Square.draw() code is executed. 
This run-time determination of which code to execute is 
sometimes called dynamic binding.
Overriding 
The method Circle.draw() overrides the method 
AbstractShape.draw() 
if shapes[i] happens to be an instance of Circle, then 
shapes[i].draw(theCanvas); 
executes the code in Circle.draw(). 
Similarly, if Square overrides AbstractShape.draw(), and 
shapes[i] happens to be an instance of Square, then the 
Square.draw() code is executed. 
This run-time determination of which code to execute is 
sometimes called dynamic binding.
Overriding 
The method Circle.draw() overrides the method 
AbstractShape.draw() 
if shapes[i] happens to be an instance of Circle, then 
shapes[i].draw(theCanvas); 
executes the code in Circle.draw(). 
Similarly, if Square overrides AbstractShape.draw(), and 
shapes[i] happens to be an instance of Square, then the 
Square.draw() code is executed. 
This run-time determination of which code to execute is 
sometimes called dynamic binding.
Non-Generic Code 
Having different classes, and dynamic binding allows us to 
avoid horrible code like: 
in some not-very-nice class 
if (shapes[i] instanceof Circle) { 
Circle c = (Circle)(shapes[i]); 
// code to draw the circle 
} else if (shapes[i] instanceof Square) { 
Square s = (Square)(shapes[i]); 
// code to draw the square 
} else // etc., etc.
Generic vs Particular 
Clearly (I hope!) the generic code is better than the 
non-generic code. 
But having a ‘dummy’ AbstractShape.draw() method is rather 
inelegant. 
This kind of situation arises often enough for Java (and other 
OO languages) to have introduced the notion of abstract class.
Abstract Classes 
An abstract class is a class that contains an abstract method. 
An abstract method is a method declaration with no body; for 
example: 
public void draw(Canvas c); 
If a class contains an abstract method, it must declare itself and 
the method abstract:
Abstract Classes 
An abstract class is a class that contains an abstract method. 
An abstract method is a method declaration with no body; for 
example: 
public void draw(Canvas c); 
If a class contains an abstract method, it must declare itself and 
the method abstract:
Abstract Classes 
An abstract class is a class that contains an abstract method. 
An abstract method is a method declaration with no body; for 
example: 
public void draw(Canvas c); 
If a class contains an abstract method, it must declare itself and 
the method abstract:
abstract Shapes 
public abstract class AbstractShape { 
private Color color; 
private Point anchorPoint; 
public void move(int dx, int dy) { 
anchorPoint.move(dx,dy); 
} 
public abstract void draw(Canvas c); 
} 
NB - you cannot create an instance of an abstract type.
‘Concrete’ Subclasses 
Just as before, we want the subclasses of AbstractShape to 
contain the actual code for drawing circles, squares, etc. 
public class Circle extends AbstractShape { 
private int radius; 
public void draw(Canvas c) { 
// code to draw the circle 
} 
} 
NB — this class is not abstract because it does provide a 
‘concrete implementation’ of the abstract method draw(). 
Class Circle is not abstract, so we can create instances of 
Circle.
‘Concrete’ Subclasses 
Just as before, we want the subclasses of AbstractShape to 
contain the actual code for drawing circles, squares, etc. 
public class Circle extends AbstractShape { 
private int radius; 
public void draw(Canvas c) { 
// code to draw the circle 
} 
} 
NB — this class is not abstract because it does provide a 
‘concrete implementation’ of the abstract method draw(). 
Class Circle is not abstract, so we can create instances of 
Circle.
‘Concrete’ Subclasses 
Just as before, we want the subclasses of AbstractShape to 
contain the actual code for drawing circles, squares, etc. 
public class Circle extends AbstractShape { 
private int radius; 
public void draw(Canvas c) { 
// code to draw the circle 
} 
} 
NB — this class is not abstract because it does provide a 
‘concrete implementation’ of the abstract method draw(). 
Class Circle is not abstract, so we can create instances of 
Circle.
Polymorphism 
The code 
for (int i=0; i < shapes.length; i++) { 
shapes[i].draw(theCanvas); 
} 
works just as before. 
This is an example of how inheritance, abstract classes and 
dynamic binding allow for polymorphic code 
i.e., code that works for a number of classes in a generic way.
Polymorphism 
The code 
for (int i=0; i < shapes.length; i++) { 
shapes[i].draw(theCanvas); 
} 
works just as before. 
This is an example of how inheritance, abstract classes and 
dynamic binding allow for polymorphic code 
i.e., code that works for a number of classes in a generic way.
Remember Interfaces? 
Interfaces are completely abstract classes, but interfaces and 
abstract classes are treated differently in Java 
The members of an interface can only be either: 
constants (‘final’) 
abstract methods 
the keyword abstract is not used in interfaces 
a class can implement any number of interfaces 
class DoItAll implements ActionListener, 
MouseListener, ComponentListener { 
... 
} 
but can extend only one class, abstract or not
Remember Interfaces? 
Interfaces are completely abstract classes, but interfaces and 
abstract classes are treated differently in Java 
The members of an interface can only be either: 
constants (‘final’) 
abstract methods 
the keyword abstract is not used in interfaces 
a class can implement any number of interfaces 
class DoItAll implements ActionListener, 
MouseListener, ComponentListener { 
... 
} 
but can extend only one class, abstract or not
Remember Interfaces? 
Interfaces are completely abstract classes, but interfaces and 
abstract classes are treated differently in Java 
The members of an interface can only be either: 
constants (‘final’) 
abstract methods 
the keyword abstract is not used in interfaces 
a class can implement any number of interfaces 
class DoItAll implements ActionListener, 
MouseListener, ComponentListener { 
... 
} 
but can extend only one class, abstract or not
Remember Interfaces? 
Interfaces are completely abstract classes, but interfaces and 
abstract classes are treated differently in Java 
The members of an interface can only be either: 
constants (‘final’) 
abstract methods 
the keyword abstract is not used in interfaces 
a class can implement any number of interfaces 
class DoItAll implements ActionListener, 
MouseListener, ComponentListener { 
... 
} 
but can extend only one class, abstract or not
Abstract Classes for Generic Solutions: 
Back to Operators 
The getPrecedence() methods of our operator classes were all 
very similar. 
Why not capture this similarity of form with an abstract class? 
This class will: 
store the precedence value as a field, 
provide a concrete implementation of getPrecedence(), 
but no implementation of toString() 
(that is why it is abstract).
Abstract Classes for Generic Solutions: 
Back to Operators 
The getPrecedence() methods of our operator classes were all 
very similar. 
Why not capture this similarity of form with an abstract class? 
This class will: 
store the precedence value as a field, 
provide a concrete implementation of getPrecedence(), 
but no implementation of toString() 
(that is why it is abstract).
Abstract Classes for Generic Solutions: 
Back to Operators 
The getPrecedence() methods of our operator classes were all 
very similar. 
Why not capture this similarity of form with an abstract class? 
This class will: 
store the precedence value as a field, 
provide a concrete implementation of getPrecedence(), 
but no implementation of toString() 
(that is why it is abstract).
Abstract Classes for Generic Solutions: 
Back to Operators 
The getPrecedence() methods of our operator classes were all 
very similar. 
Why not capture this similarity of form with an abstract class? 
This class will: 
store the precedence value as a field, 
provide a concrete implementation of getPrecedence(), 
but no implementation of toString() 
(that is why it is abstract).
Abstract Classes for Generic Solutions: 
Back to Operators 
The getPrecedence() methods of our operator classes were all 
very similar. 
Why not capture this similarity of form with an abstract class? 
This class will: 
store the precedence value as a field, 
provide a concrete implementation of getPrecedence(), 
but no implementation of toString() 
(that is why it is abstract).
Getting Abstract 
In the Operators class: 
private static abstract class Op 
implements Operator { 
private int precedence; 
public Op(int p) { 
precedence = p; 
} 
public int getPrecedence() { 
return precedence; 
} 
}
Notes 
The class is private because we don’t want other classes 
outside Operators making instances of Op: 
all uses of operators go through the Operators class. 
The class is abstract because it doesn’t implement 
toString(). (it ‘inherits’ the abstract method by declaring 
that it implements Operator) 
The field precedence is private. It might seem a good idea 
to make it protected, because it will be useful in subclasses 
of Op (esp. in the toString() methods). But there’s no need: 
the getPrecedence() method is public. 
The class has a constructor — even though we can’t 
create instances of the class! Nevertheless, the 
functionality is useful (see later).
Notes 
The class is private because we don’t want other classes 
outside Operators making instances of Op: 
all uses of operators go through the Operators class. 
The class is abstract because it doesn’t implement 
toString(). (it ‘inherits’ the abstract method by declaring 
that it implements Operator) 
The field precedence is private. It might seem a good idea 
to make it protected, because it will be useful in subclasses 
of Op (esp. in the toString() methods). But there’s no need: 
the getPrecedence() method is public. 
The class has a constructor — even though we can’t 
create instances of the class! Nevertheless, the 
functionality is useful (see later).
Notes 
The class is private because we don’t want other classes 
outside Operators making instances of Op: 
all uses of operators go through the Operators class. 
The class is abstract because it doesn’t implement 
toString(). (it ‘inherits’ the abstract method by declaring 
that it implements Operator) 
The field precedence is private. It might seem a good idea 
to make it protected, because it will be useful in subclasses 
of Op (esp. in the toString() methods). But there’s no need: 
the getPrecedence() method is public. 
The class has a constructor — even though we can’t 
create instances of the class! Nevertheless, the 
functionality is useful (see later).
Notes 
The class is private because we don’t want other classes 
outside Operators making instances of Op: 
all uses of operators go through the Operators class. 
The class is abstract because it doesn’t implement 
toString(). (it ‘inherits’ the abstract method by declaring 
that it implements Operator) 
The field precedence is private. It might seem a good idea 
to make it protected, because it will be useful in subclasses 
of Op (esp. in the toString() methods). But there’s no need: 
the getPrecedence() method is public. 
The class has a constructor — even though we can’t 
create instances of the class! Nevertheless, the 
functionality is useful (see later).
Using the Abstract Class 
To use the class Op in creating the constants AND OP, etc., 
we could explicitly declare a concrete subclass of Op (i.e., with 
an implementation of toString()): 
in class Operators 
public static class AndOperator 
extends Op { 
public AndOperator(int prec) { 
super(prec); 
} 
public String toString(Prop[] ps, int prec) { 
... 
} 
}
Notes 
in class Operators 
public static class AndOperator 
extends Op { 
public AndOperator(int prec) { 
super(prec); 
} 
... 
} 
As before 
Inheriting the getPrecedence() method 
Even though Op is abstract, and we can’t create instances of 
the class, we can call the constructor to set the (private) 
precedence field. Odd, maybe, but useful.
Notes 
in class Operators 
public static class AndOperator 
extends Op { 
public AndOperator(int prec) { 
super(prec); 
} 
... 
} 
As before 
Inheriting the getPrecedence() method 
Even though Op is abstract, and we can’t create instances of 
the class, we can call the constructor to set the (private) 
precedence field. Odd, maybe, but useful.
Notes 
in class Operators 
public static class AndOperator 
extends Op { 
public AndOperator(int prec) { 
super(prec); 
} 
... 
} 
As before 
Inheriting the getPrecedence() method 
Even though Op is abstract, and we can’t create instances of 
the class, we can call the constructor to set the (private) 
precedence field. Odd, maybe, but useful.
Notes 
in class Operators$AndOperator 
public String toString(Prop[] ps, int prec) { 
String s = ps[0].toString(getPrecedence()) + 
" and " + 
ps[1].toString(getPrecedence()); 
if (prec < getPrecedence()) { 
s = "(" + s + ")"; 
} 
return s; 
} 
We can’t refer to the private field precedence; we can refer to 
the public method getPrecedence()
Notes 
in class Operators$AndOperator 
public String toString(Prop[] ps, int prec) { 
String s = ps[0].toString(getPrecedence()) + 
" and " + 
ps[1].toString(getPrecedence()); 
if (prec < getPrecedence()) { 
s = "(" + s + ")"; 
} 
return s; 
} 
We can’t refer to the private field precedence; we can refer to 
the public method getPrecedence()
Using the Abstract Class 
Or we can create an instance of an anonymous concrete 
subclass (with the concrete implementation of toString()). 
in class Operators 
public static final Operator AND OP 
= new Op(AND PREC) { 
public String toString(Prop[] ps, int prec) { 
String s = ps[0].toString(getPrecedence()) 
+ " and " 
+ ps[1].toString(getPrecedence()); 
if (prec < getPrecedence()) { 
s = "(" + s + ")"; 
} 
return s; 
} 
};
Using the Abstract Class 
Or we can create an instance of an anonymous concrete 
subclass (with the concrete implementation of toString()). 
in class Operators 
public static final Operator AND OP 
= new Op(AND PREC) { 
public String toString(Prop[] ps, int prec) { 
String s = ps[0].toString(getPrecedence()) 
+ " and " 
+ ps[1].toString(getPrecedence()); 
if (prec < getPrecedence()) { 
s = "(" + s + ")"; 
} 
return s; 
} 
};
Notes 
in class Operators 
public static final Operator AND OP 
= new Op(AND PREC) { 
public String toString(Prop[] ps, int prec) { 
... 
} 
}; 
Creating an instance of an anonymous class that extends Op 
Passing the parameter to the Op constructor 
(so setting the precedence field) 
We must provide a concrete implementation of the abstract 
method(s)
Notes 
in class Operators 
public static final Operator AND OP 
= new Op(AND PREC) { 
public String toString(Prop[] ps, int prec) { 
... 
} 
}; 
Creating an instance of an anonymous class that extends Op 
Passing the parameter to the Op constructor 
(so setting the precedence field) 
We must provide a concrete implementation of the abstract 
method(s)
Notes 
in class Operators 
public static final Operator AND OP 
= new Op(AND PREC) { 
public String toString(Prop[] ps, int prec) { 
... 
} 
}; 
Creating an instance of an anonymous class that extends Op 
Passing the parameter to the Op constructor 
(so setting the precedence field) 
We must provide a concrete implementation of the abstract 
method(s)
Notes 
We can create an instance of an (anonymous) class that 
implements an interface using the pattern: 
new InterfaceName() { 
method-definitions 
} 
We can do a similar thing with abstract classes, using the 
pattern: 
new AbstractClassName(actualParametersToConstructor) { 
method-definitions 
}
Notes 
We can create an instance of an (anonymous) class that 
implements an interface using the pattern: 
new InterfaceName() { 
method-definitions 
} 
We can do a similar thing with abstract classes, using the 
pattern: 
new AbstractClassName(actualParametersToConstructor) { 
method-definitions 
}
Why Stop Here? 
The concrete implementations of toString() for the binary 
operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle 
difference. . . ). 
We introduce a class BinOp as a subclass of Op that will 
provide a concrete implementation of the toString() method for 
binary operators. 
This avoids the need to rewrite the toString() method for ‘and’ 
and ‘or’ — clearly, this is only worth doing if we have enough 
binary operators (but remember we’re looking at a very simple 
language).
Why Stop Here? 
The concrete implementations of toString() for the binary 
operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle 
difference. . . ). 
We introduce a class BinOp as a subclass of Op that will 
provide a concrete implementation of the toString() method for 
binary operators. 
This avoids the need to rewrite the toString() method for ‘and’ 
and ‘or’ — clearly, this is only worth doing if we have enough 
binary operators (but remember we’re looking at a very simple 
language).
Why Stop Here? 
The concrete implementations of toString() for the binary 
operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle 
difference. . . ). 
We introduce a class BinOp as a subclass of Op that will 
provide a concrete implementation of the toString() method for 
binary operators. 
This avoids the need to rewrite the toString() method for ‘and’ 
and ‘or’ — clearly, this is only worth doing if we have enough 
binary operators (but remember we’re looking at a very simple 
language).
Infix Binary Operators 
in class Operators 
private static class BinOp extends Op { 
private String opName; 
BinOp(int prec, String s) { 
super(prec); 
opName = s; 
} 
// toString() to come here 
} 
inheriting getPrecedence(), and setting the precedence field 
storing the name of the operator
Infix Binary Operators 
in class Operators 
private static class BinOp extends Op { 
private String opName; 
BinOp(int prec, String s) { 
super(prec); 
opName = s; 
} 
// toString() to come here 
} 
inheriting getPrecedence(), and setting the precedence field 
storing the name of the operator
Infix Binary Operators 
in class Operators 
private static class BinOp extends Op { 
private String opName; 
BinOp(int prec, String s) { 
super(prec); 
opName = s; 
} 
// toString() to come here 
} 
inheriting getPrecedence(), and setting the precedence field 
storing the name of the operator
Infix Binary Operators 
in class Operators$BinOp 
public String toString(Prop[] as, int prec){ 
String s = as[0].toString(getPrecedence()) 
+ " " + opName + " " 
+ as[1].toString(getPrecedence()); 
if (prec < getPrecedence()) { 
s = "(" + s + ")"; 
} 
return s; 
} 
Generalising the code for ‘and’ and ‘or’
Using BinOp 
Now we can just declare: 
in class Operators v1.2 
public static final Operator AND = 
new BinOp(AND PREC, "and"); 
public static final Operator OR = 
new BinOp(OR PREC, "or");
Implies is a Special Case 
The toString() method can be overridden ‘on the fly’, using the 
notation for anonymous classes. 
in class Operators v1.2 
public static final Operator IMPLIES = 
new BinOp(IMPLIES PREC, "implies") 
{ 
public String toString(Prop[] as, int p) 
{ 
// code goes here 
} };
And On And On. . . 
It would also be useful to have another subclass of Op for 
constants, including variables. . . .
Encapsulation 
We say that a class encapsulates an abstract data type if it 
implements that data type correctly. 
Recall that an abstract data type consists of a set of values (the 
data values), together with certain operations for manipulating 
those values. 
A class correctly implements the data type if it: 
provides a representation for the data values, 
provides methods for the data type operations, and 
only those operations are provided.
Encapsulation 
We say that a class encapsulates an abstract data type if it 
implements that data type correctly. 
Recall that an abstract data type consists of a set of values (the 
data values), together with certain operations for manipulating 
those values. 
A class correctly implements the data type if it: 
provides a representation for the data values, 
provides methods for the data type operations, and 
only those operations are provided.
Encapsulation 
Only the specified operations are available as methods, and the 
implementation details are hidden by making the fields private. 
The same considerations are applicable to the new features we 
have seen: inner, static, and anonymous classes. 
Their scope and scope-modifiers can and should be used to 
ensure encapsulation.
Encapsulation 
The class Prop is intended to represent an abstract data type of 
Boolean terms. 
(They are implemented as tree structures.) 
This is a simple data structure: the only method required is to 
get a string representation with brackets only where necessary. 
How successful have we been?
That’s All, Folks! 
Summary 
Abstract Classes 
Overriding 
Anonymous Classes 
Next: 
Things fall apart

More Related Content

Viewers also liked (20)

Java generics final
Java generics finalJava generics final
Java generics final
 
On Parameterised Types and Java Generics
On Parameterised Types and Java GenericsOn Parameterised Types and Java Generics
On Parameterised Types and Java Generics
 
Java generics
Java genericsJava generics
Java generics
 
Parameters
ParametersParameters
Parameters
 
Java Generics: a deep dive
Java Generics: a deep diveJava Generics: a deep dive
Java Generics: a deep dive
 
MIP 40 Colin Tate
MIP 40 Colin TateMIP 40 Colin Tate
MIP 40 Colin Tate
 
Carta appartenenza + scheda genitore
Carta appartenenza + scheda genitoreCarta appartenenza + scheda genitore
Carta appartenenza + scheda genitore
 
Hydraker Technical Insight_ENG
Hydraker Technical Insight_ENGHydraker Technical Insight_ENG
Hydraker Technical Insight_ENG
 
L9
L9L9
L9
 
Paris 1900 dr
Paris  1900 drParis  1900 dr
Paris 1900 dr
 
L5
L5L5
L5
 
Real Estate
Real EstateReal Estate
Real Estate
 
L8
L8L8
L8
 
234
234234
234
 
456
456456
456
 
CFM56-7
CFM56-7CFM56-7
CFM56-7
 
Project On Pint OS (INTRODUCTION)
Project On Pint OS (INTRODUCTION)Project On Pint OS (INTRODUCTION)
Project On Pint OS (INTRODUCTION)
 
L3
L3L3
L3
 
PhD project
PhD projectPhD project
PhD project
 
Alexandre_Ravagnani_Bio
Alexandre_Ravagnani_BioAlexandre_Ravagnani_Bio
Alexandre_Ravagnani_Bio
 

Similar to L4

Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxMahmoodAlashqar
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)Satyam Gupta
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#Sireesh K
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract ClassOUM SAOKOSAL
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdfRihabBENLAMINE
 
Basic Graphics in Java
Basic Graphics in JavaBasic Graphics in Java
Basic Graphics in JavaPrakash Kumar
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2Rakesh T
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassOum Saokosal
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxfaithxdunce63732
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingMark Kilgard
 
Anurag Arpan (PPT on AutoCAD )
Anurag Arpan (PPT on AutoCAD )Anurag Arpan (PPT on AutoCAD )
Anurag Arpan (PPT on AutoCAD )Anurag Arpan
 

Similar to L4 (20)

Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptx
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)
 
14 class design
14 class design14 class design
14 class design
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
 
Basic Graphics in Java
Basic Graphics in JavaBasic Graphics in Java
Basic Graphics in Java
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
 
Highly Strung
Highly StrungHighly Strung
Highly Strung
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract Class
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and Culling
 
Anurag Arpan (PPT on AutoCAD )
Anurag Arpan (PPT on AutoCAD )Anurag Arpan (PPT on AutoCAD )
Anurag Arpan (PPT on AutoCAD )
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 

More from lksoo

More from lksoo (20)

Lo48
Lo48Lo48
Lo48
 
Lo43
Lo43Lo43
Lo43
 
Lo39
Lo39Lo39
Lo39
 
Lo37
Lo37Lo37
Lo37
 
Lo27
Lo27Lo27
Lo27
 
Lo17
Lo17Lo17
Lo17
 
Lo12
Lo12Lo12
Lo12
 
T3
T3T3
T3
 
T2
T2T2
T2
 
T1
T1T1
T1
 
T4
T4T4
T4
 
P5
P5P5
P5
 
P4
P4P4
P4
 
P3
P3P3
P3
 
P1
P1P1
P1
 
P2
P2P2
P2
 
L10
L10L10
L10
 
L7
L7L7
L7
 
L6
L6L6
L6
 
L2
L2L2
L2
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

L4

  • 1. COMP 213 Advanced Object-oriented Programming Lecture 16 Abstract Classes
  • 2. Abstract Classes for Generic Solutions There is still scope for making our Operators class more concise. The getPrecedence() methods all have the same form. For ‘and’: in class AndOperator public int getPrecedence() { return AND PREC; } For ‘or’: in class OrOperator public int getPrecedence() { return OR PREC; } etc.
  • 3. Abstract Classes for Generic Solutions There is still scope for making our Operators class more concise. The getPrecedence() methods all have the same form. For ‘and’: in class AndOperator public int getPrecedence() { return AND PREC; } For ‘or’: in class OrOperator public int getPrecedence() { return OR PREC; } etc.
  • 4. Digression: Generic vs Particular Consider a tool that lets a user draw shapes on a canvas: circles, squares, rectangles, ovals, etc. Suppose the design of such a tool has reached a stage where the following classes have been identified: Canvas — the ‘drawing area’ Color — to represent different colours for shapes AbstractShape — to represent the drawn shapes, with subclasses: Circle Square, etc.
  • 5. Digression: Generic vs Particular Consider a tool that lets a user draw shapes on a canvas: circles, squares, rectangles, ovals, etc. Suppose the design of such a tool has reached a stage where the following classes have been identified: Canvas — the ‘drawing area’ Color — to represent different colours for shapes AbstractShape — to represent the drawn shapes, with subclasses: Circle Square, etc.
  • 6. Digression: Generic vs Particular Consider a tool that lets a user draw shapes on a canvas: circles, squares, rectangles, ovals, etc. Suppose the design of such a tool has reached a stage where the following classes have been identified: Canvas — the ‘drawing area’ Color — to represent different colours for shapes AbstractShape — to represent the drawn shapes, with subclasses: Circle Square, etc.
  • 7. Generic vs Particular The AbstractShape class is to have the following fields: Color color Point anchorPoint where anchorPoint represents some location representing the position of the shape on the canvas this might be the centre of a Circle, the top-left corner of a Square, etc.
  • 8. Generic vs Particular The AbstractShape class is to have the following methods: void move(int dx, int dy) — to move a shape on the canvas void draw(Canvas c) — to actually draw the shape on the canvas
  • 9. Generic vs Particular The tool will store all the drawn shapes in an array: AbstractShape[] shapes; Whenever necessary (e.g., refreshing a screen), all the shapes on the canvas can be drawn by for (int i=0; i < shapes.length; i++) { shapes[i].draw(theCanvas); } Thus all the shapes can be drawn in a generic way.
  • 10. Generic vs Particular The tool will store all the drawn shapes in an array: AbstractShape[] shapes; Whenever necessary (e.g., refreshing a screen), all the shapes on the canvas can be drawn by for (int i=0; i < shapes.length; i++) { shapes[i].draw(theCanvas); } Thus all the shapes can be drawn in a generic way.
  • 11. Generic vs Particular However, actually drawing the shapes is not generic: the code for drawing a Circle will be very different from the code for drawing a Square, and so on. (Sound familiar?) We might include a ‘dummy’ method in AbstractShape, which is overridden in the subclasses Circle, Square, etc.
  • 12. Generic vs Particular However, actually drawing the shapes is not generic: the code for drawing a Circle will be very different from the code for drawing a Square, and so on. (Sound familiar?) We might include a ‘dummy’ method in AbstractShape, which is overridden in the subclasses Circle, Square, etc.
  • 13. Abstract Shapes public class AbstractShape { private Color color; private Point anchorPoint; public void move(int dx, int dy) { anchorPoint.move(dx,dy); } public void draw(Canvas c) { } }
  • 14. Circles This dummy method will be overridden in the subclasses. E.g., public class Circle extends AbstractShape { private int radius; public void draw(Canvas c) { // code to draw the circle } }
  • 15. Overriding The method Circle.draw() overrides the method AbstractShape.draw() if shapes[i] happens to be an instance of Circle, then shapes[i].draw(theCanvas); executes the code in Circle.draw(). Similarly, if Square overrides AbstractShape.draw(), and shapes[i] happens to be an instance of Square, then the Square.draw() code is executed. This run-time determination of which code to execute is sometimes called dynamic binding.
  • 16. Overriding The method Circle.draw() overrides the method AbstractShape.draw() if shapes[i] happens to be an instance of Circle, then shapes[i].draw(theCanvas); executes the code in Circle.draw(). Similarly, if Square overrides AbstractShape.draw(), and shapes[i] happens to be an instance of Square, then the Square.draw() code is executed. This run-time determination of which code to execute is sometimes called dynamic binding.
  • 17. Overriding The method Circle.draw() overrides the method AbstractShape.draw() if shapes[i] happens to be an instance of Circle, then shapes[i].draw(theCanvas); executes the code in Circle.draw(). Similarly, if Square overrides AbstractShape.draw(), and shapes[i] happens to be an instance of Square, then the Square.draw() code is executed. This run-time determination of which code to execute is sometimes called dynamic binding.
  • 18. Non-Generic Code Having different classes, and dynamic binding allows us to avoid horrible code like: in some not-very-nice class if (shapes[i] instanceof Circle) { Circle c = (Circle)(shapes[i]); // code to draw the circle } else if (shapes[i] instanceof Square) { Square s = (Square)(shapes[i]); // code to draw the square } else // etc., etc.
  • 19. Generic vs Particular Clearly (I hope!) the generic code is better than the non-generic code. But having a ‘dummy’ AbstractShape.draw() method is rather inelegant. This kind of situation arises often enough for Java (and other OO languages) to have introduced the notion of abstract class.
  • 20. Abstract Classes An abstract class is a class that contains an abstract method. An abstract method is a method declaration with no body; for example: public void draw(Canvas c); If a class contains an abstract method, it must declare itself and the method abstract:
  • 21. Abstract Classes An abstract class is a class that contains an abstract method. An abstract method is a method declaration with no body; for example: public void draw(Canvas c); If a class contains an abstract method, it must declare itself and the method abstract:
  • 22. Abstract Classes An abstract class is a class that contains an abstract method. An abstract method is a method declaration with no body; for example: public void draw(Canvas c); If a class contains an abstract method, it must declare itself and the method abstract:
  • 23. abstract Shapes public abstract class AbstractShape { private Color color; private Point anchorPoint; public void move(int dx, int dy) { anchorPoint.move(dx,dy); } public abstract void draw(Canvas c); } NB - you cannot create an instance of an abstract type.
  • 24. ‘Concrete’ Subclasses Just as before, we want the subclasses of AbstractShape to contain the actual code for drawing circles, squares, etc. public class Circle extends AbstractShape { private int radius; public void draw(Canvas c) { // code to draw the circle } } NB — this class is not abstract because it does provide a ‘concrete implementation’ of the abstract method draw(). Class Circle is not abstract, so we can create instances of Circle.
  • 25. ‘Concrete’ Subclasses Just as before, we want the subclasses of AbstractShape to contain the actual code for drawing circles, squares, etc. public class Circle extends AbstractShape { private int radius; public void draw(Canvas c) { // code to draw the circle } } NB — this class is not abstract because it does provide a ‘concrete implementation’ of the abstract method draw(). Class Circle is not abstract, so we can create instances of Circle.
  • 26. ‘Concrete’ Subclasses Just as before, we want the subclasses of AbstractShape to contain the actual code for drawing circles, squares, etc. public class Circle extends AbstractShape { private int radius; public void draw(Canvas c) { // code to draw the circle } } NB — this class is not abstract because it does provide a ‘concrete implementation’ of the abstract method draw(). Class Circle is not abstract, so we can create instances of Circle.
  • 27. Polymorphism The code for (int i=0; i < shapes.length; i++) { shapes[i].draw(theCanvas); } works just as before. This is an example of how inheritance, abstract classes and dynamic binding allow for polymorphic code i.e., code that works for a number of classes in a generic way.
  • 28. Polymorphism The code for (int i=0; i < shapes.length; i++) { shapes[i].draw(theCanvas); } works just as before. This is an example of how inheritance, abstract classes and dynamic binding allow for polymorphic code i.e., code that works for a number of classes in a generic way.
  • 29. Remember Interfaces? Interfaces are completely abstract classes, but interfaces and abstract classes are treated differently in Java The members of an interface can only be either: constants (‘final’) abstract methods the keyword abstract is not used in interfaces a class can implement any number of interfaces class DoItAll implements ActionListener, MouseListener, ComponentListener { ... } but can extend only one class, abstract or not
  • 30. Remember Interfaces? Interfaces are completely abstract classes, but interfaces and abstract classes are treated differently in Java The members of an interface can only be either: constants (‘final’) abstract methods the keyword abstract is not used in interfaces a class can implement any number of interfaces class DoItAll implements ActionListener, MouseListener, ComponentListener { ... } but can extend only one class, abstract or not
  • 31. Remember Interfaces? Interfaces are completely abstract classes, but interfaces and abstract classes are treated differently in Java The members of an interface can only be either: constants (‘final’) abstract methods the keyword abstract is not used in interfaces a class can implement any number of interfaces class DoItAll implements ActionListener, MouseListener, ComponentListener { ... } but can extend only one class, abstract or not
  • 32. Remember Interfaces? Interfaces are completely abstract classes, but interfaces and abstract classes are treated differently in Java The members of an interface can only be either: constants (‘final’) abstract methods the keyword abstract is not used in interfaces a class can implement any number of interfaces class DoItAll implements ActionListener, MouseListener, ComponentListener { ... } but can extend only one class, abstract or not
  • 33. Abstract Classes for Generic Solutions: Back to Operators The getPrecedence() methods of our operator classes were all very similar. Why not capture this similarity of form with an abstract class? This class will: store the precedence value as a field, provide a concrete implementation of getPrecedence(), but no implementation of toString() (that is why it is abstract).
  • 34. Abstract Classes for Generic Solutions: Back to Operators The getPrecedence() methods of our operator classes were all very similar. Why not capture this similarity of form with an abstract class? This class will: store the precedence value as a field, provide a concrete implementation of getPrecedence(), but no implementation of toString() (that is why it is abstract).
  • 35. Abstract Classes for Generic Solutions: Back to Operators The getPrecedence() methods of our operator classes were all very similar. Why not capture this similarity of form with an abstract class? This class will: store the precedence value as a field, provide a concrete implementation of getPrecedence(), but no implementation of toString() (that is why it is abstract).
  • 36. Abstract Classes for Generic Solutions: Back to Operators The getPrecedence() methods of our operator classes were all very similar. Why not capture this similarity of form with an abstract class? This class will: store the precedence value as a field, provide a concrete implementation of getPrecedence(), but no implementation of toString() (that is why it is abstract).
  • 37. Abstract Classes for Generic Solutions: Back to Operators The getPrecedence() methods of our operator classes were all very similar. Why not capture this similarity of form with an abstract class? This class will: store the precedence value as a field, provide a concrete implementation of getPrecedence(), but no implementation of toString() (that is why it is abstract).
  • 38. Getting Abstract In the Operators class: private static abstract class Op implements Operator { private int precedence; public Op(int p) { precedence = p; } public int getPrecedence() { return precedence; } }
  • 39. Notes The class is private because we don’t want other classes outside Operators making instances of Op: all uses of operators go through the Operators class. The class is abstract because it doesn’t implement toString(). (it ‘inherits’ the abstract method by declaring that it implements Operator) The field precedence is private. It might seem a good idea to make it protected, because it will be useful in subclasses of Op (esp. in the toString() methods). But there’s no need: the getPrecedence() method is public. The class has a constructor — even though we can’t create instances of the class! Nevertheless, the functionality is useful (see later).
  • 40. Notes The class is private because we don’t want other classes outside Operators making instances of Op: all uses of operators go through the Operators class. The class is abstract because it doesn’t implement toString(). (it ‘inherits’ the abstract method by declaring that it implements Operator) The field precedence is private. It might seem a good idea to make it protected, because it will be useful in subclasses of Op (esp. in the toString() methods). But there’s no need: the getPrecedence() method is public. The class has a constructor — even though we can’t create instances of the class! Nevertheless, the functionality is useful (see later).
  • 41. Notes The class is private because we don’t want other classes outside Operators making instances of Op: all uses of operators go through the Operators class. The class is abstract because it doesn’t implement toString(). (it ‘inherits’ the abstract method by declaring that it implements Operator) The field precedence is private. It might seem a good idea to make it protected, because it will be useful in subclasses of Op (esp. in the toString() methods). But there’s no need: the getPrecedence() method is public. The class has a constructor — even though we can’t create instances of the class! Nevertheless, the functionality is useful (see later).
  • 42. Notes The class is private because we don’t want other classes outside Operators making instances of Op: all uses of operators go through the Operators class. The class is abstract because it doesn’t implement toString(). (it ‘inherits’ the abstract method by declaring that it implements Operator) The field precedence is private. It might seem a good idea to make it protected, because it will be useful in subclasses of Op (esp. in the toString() methods). But there’s no need: the getPrecedence() method is public. The class has a constructor — even though we can’t create instances of the class! Nevertheless, the functionality is useful (see later).
  • 43. Using the Abstract Class To use the class Op in creating the constants AND OP, etc., we could explicitly declare a concrete subclass of Op (i.e., with an implementation of toString()): in class Operators public static class AndOperator extends Op { public AndOperator(int prec) { super(prec); } public String toString(Prop[] ps, int prec) { ... } }
  • 44. Notes in class Operators public static class AndOperator extends Op { public AndOperator(int prec) { super(prec); } ... } As before Inheriting the getPrecedence() method Even though Op is abstract, and we can’t create instances of the class, we can call the constructor to set the (private) precedence field. Odd, maybe, but useful.
  • 45. Notes in class Operators public static class AndOperator extends Op { public AndOperator(int prec) { super(prec); } ... } As before Inheriting the getPrecedence() method Even though Op is abstract, and we can’t create instances of the class, we can call the constructor to set the (private) precedence field. Odd, maybe, but useful.
  • 46. Notes in class Operators public static class AndOperator extends Op { public AndOperator(int prec) { super(prec); } ... } As before Inheriting the getPrecedence() method Even though Op is abstract, and we can’t create instances of the class, we can call the constructor to set the (private) precedence field. Odd, maybe, but useful.
  • 47. Notes in class Operators$AndOperator public String toString(Prop[] ps, int prec) { String s = ps[0].toString(getPrecedence()) + " and " + ps[1].toString(getPrecedence()); if (prec < getPrecedence()) { s = "(" + s + ")"; } return s; } We can’t refer to the private field precedence; we can refer to the public method getPrecedence()
  • 48. Notes in class Operators$AndOperator public String toString(Prop[] ps, int prec) { String s = ps[0].toString(getPrecedence()) + " and " + ps[1].toString(getPrecedence()); if (prec < getPrecedence()) { s = "(" + s + ")"; } return s; } We can’t refer to the private field precedence; we can refer to the public method getPrecedence()
  • 49. Using the Abstract Class Or we can create an instance of an anonymous concrete subclass (with the concrete implementation of toString()). in class Operators public static final Operator AND OP = new Op(AND PREC) { public String toString(Prop[] ps, int prec) { String s = ps[0].toString(getPrecedence()) + " and " + ps[1].toString(getPrecedence()); if (prec < getPrecedence()) { s = "(" + s + ")"; } return s; } };
  • 50. Using the Abstract Class Or we can create an instance of an anonymous concrete subclass (with the concrete implementation of toString()). in class Operators public static final Operator AND OP = new Op(AND PREC) { public String toString(Prop[] ps, int prec) { String s = ps[0].toString(getPrecedence()) + " and " + ps[1].toString(getPrecedence()); if (prec < getPrecedence()) { s = "(" + s + ")"; } return s; } };
  • 51. Notes in class Operators public static final Operator AND OP = new Op(AND PREC) { public String toString(Prop[] ps, int prec) { ... } }; Creating an instance of an anonymous class that extends Op Passing the parameter to the Op constructor (so setting the precedence field) We must provide a concrete implementation of the abstract method(s)
  • 52. Notes in class Operators public static final Operator AND OP = new Op(AND PREC) { public String toString(Prop[] ps, int prec) { ... } }; Creating an instance of an anonymous class that extends Op Passing the parameter to the Op constructor (so setting the precedence field) We must provide a concrete implementation of the abstract method(s)
  • 53. Notes in class Operators public static final Operator AND OP = new Op(AND PREC) { public String toString(Prop[] ps, int prec) { ... } }; Creating an instance of an anonymous class that extends Op Passing the parameter to the Op constructor (so setting the precedence field) We must provide a concrete implementation of the abstract method(s)
  • 54. Notes We can create an instance of an (anonymous) class that implements an interface using the pattern: new InterfaceName() { method-definitions } We can do a similar thing with abstract classes, using the pattern: new AbstractClassName(actualParametersToConstructor) { method-definitions }
  • 55. Notes We can create an instance of an (anonymous) class that implements an interface using the pattern: new InterfaceName() { method-definitions } We can do a similar thing with abstract classes, using the pattern: new AbstractClassName(actualParametersToConstructor) { method-definitions }
  • 56. Why Stop Here? The concrete implementations of toString() for the binary operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle difference. . . ). We introduce a class BinOp as a subclass of Op that will provide a concrete implementation of the toString() method for binary operators. This avoids the need to rewrite the toString() method for ‘and’ and ‘or’ — clearly, this is only worth doing if we have enough binary operators (but remember we’re looking at a very simple language).
  • 57. Why Stop Here? The concrete implementations of toString() for the binary operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle difference. . . ). We introduce a class BinOp as a subclass of Op that will provide a concrete implementation of the toString() method for binary operators. This avoids the need to rewrite the toString() method for ‘and’ and ‘or’ — clearly, this is only worth doing if we have enough binary operators (but remember we’re looking at a very simple language).
  • 58. Why Stop Here? The concrete implementations of toString() for the binary operators ‘and’ and ‘or’ are very similar (‘implies’ has a subtle difference. . . ). We introduce a class BinOp as a subclass of Op that will provide a concrete implementation of the toString() method for binary operators. This avoids the need to rewrite the toString() method for ‘and’ and ‘or’ — clearly, this is only worth doing if we have enough binary operators (but remember we’re looking at a very simple language).
  • 59. Infix Binary Operators in class Operators private static class BinOp extends Op { private String opName; BinOp(int prec, String s) { super(prec); opName = s; } // toString() to come here } inheriting getPrecedence(), and setting the precedence field storing the name of the operator
  • 60. Infix Binary Operators in class Operators private static class BinOp extends Op { private String opName; BinOp(int prec, String s) { super(prec); opName = s; } // toString() to come here } inheriting getPrecedence(), and setting the precedence field storing the name of the operator
  • 61. Infix Binary Operators in class Operators private static class BinOp extends Op { private String opName; BinOp(int prec, String s) { super(prec); opName = s; } // toString() to come here } inheriting getPrecedence(), and setting the precedence field storing the name of the operator
  • 62. Infix Binary Operators in class Operators$BinOp public String toString(Prop[] as, int prec){ String s = as[0].toString(getPrecedence()) + " " + opName + " " + as[1].toString(getPrecedence()); if (prec < getPrecedence()) { s = "(" + s + ")"; } return s; } Generalising the code for ‘and’ and ‘or’
  • 63. Using BinOp Now we can just declare: in class Operators v1.2 public static final Operator AND = new BinOp(AND PREC, "and"); public static final Operator OR = new BinOp(OR PREC, "or");
  • 64. Implies is a Special Case The toString() method can be overridden ‘on the fly’, using the notation for anonymous classes. in class Operators v1.2 public static final Operator IMPLIES = new BinOp(IMPLIES PREC, "implies") { public String toString(Prop[] as, int p) { // code goes here } };
  • 65. And On And On. . . It would also be useful to have another subclass of Op for constants, including variables. . . .
  • 66. Encapsulation We say that a class encapsulates an abstract data type if it implements that data type correctly. Recall that an abstract data type consists of a set of values (the data values), together with certain operations for manipulating those values. A class correctly implements the data type if it: provides a representation for the data values, provides methods for the data type operations, and only those operations are provided.
  • 67. Encapsulation We say that a class encapsulates an abstract data type if it implements that data type correctly. Recall that an abstract data type consists of a set of values (the data values), together with certain operations for manipulating those values. A class correctly implements the data type if it: provides a representation for the data values, provides methods for the data type operations, and only those operations are provided.
  • 68. Encapsulation Only the specified operations are available as methods, and the implementation details are hidden by making the fields private. The same considerations are applicable to the new features we have seen: inner, static, and anonymous classes. Their scope and scope-modifiers can and should be used to ensure encapsulation.
  • 69. Encapsulation The class Prop is intended to represent an abstract data type of Boolean terms. (They are implemented as tree structures.) This is a simple data structure: the only method required is to get a string representation with brackets only where necessary. How successful have we been?
  • 70. That’s All, Folks! Summary Abstract Classes Overriding Anonymous Classes Next: Things fall apart