Title: ObjectOriented Programming
1Object-Oriented Programming Objectives To
understand inheritance and software reuse. To
understand superclasses and subclasses.
2Introduction In this section we discuss
object-oriented programming, examining one of its
main key component technologies -
inheritance. Inheritance is a mechanism that
facilitates software reuse New classes are
created from existing classes by taking
their attributes and methods, adding to them to
provide new, but related functionality. Software
reuse (if done well!) saves time in program
development. On creating a new class (subclass),
instead of writing completely new instance
variables and methods, the class can inherit
existing elements from a previously defined
superclass.
3Introduction A subclass can also be the
superclass in the definition of some other class.
(Superclass)
JApplet
Is_A
MyApplet
(Subclass)
The direct superclass of a subclass is the class
from which the subclass inherits attributes and
behaviour (via the extends keyword).
4Introduction A subclass normally adds variables
and methods of its own, so a subclass is usually
larger than its superclass. A subclass is more
specific than its superclass, as it represents
a smaller group of objects. Every object of
subclass is also an object of its superclass -
but the converse is not true. Two relationships
may be defined Is_A an object of a subclass
may be treated as an object of its superclass
type. Has_A A given object has one or more
objects as members (composition).
5Introduction We define a new form of member
access control protected Subclass methods and
methods in the same package(directory) as the
superclass, can access protected superclass
members. Methods of a subclass may require
access to members of a superclass. These should
be marked public or protected. No subclass
access to private members of a superclass is
possible. Sometimes, the behaviour of an
inherited method is inappropriate in a
subclass. Such a method can be overridden (or
re-implemented) in a subclass.
6Superclasses and Subclasses Often an object of
one class is an object of another class as
well. A rectangle is a quadrilateral in the
class sense, class Rectangle would inherit from
class Quadrilateral. A rectangle is a specific
type of quadrilateral, but its not true to
say that a quadrilateral is a rectangle (the
quadrilateral could be a parallelogram). Some
other examples follow below Superclass Subclas
s Student Postgraduate Undergraduate Shape
Circle Triangle Rectangle
7Superclasses and Subclasses Inheritance
relationships form tree-like structures
CommunityMember
Alumni
Student
Employee
Staff
Faculty
Deitel Deitel Fig. 9.2
Administrator
Teacher
Shape
ThreeDimensionalShape
TwoDimensionalShape
Triangle
Square
Circle
Tetrahedron
Cube
Sphere
Deitel Deitel Fig. 9.3
8- Protected Members
- Public members of a superclass object can be
accessed from any point - in a program (given a reference to the object, or
subclass object). - Protected members of a superclass may be accessed
only by - methods of the superclass
- methods of subclasses
- methods of other classes in the same
package(directory) -
9Method Overriding When a subclass defines a
method with the same name, return type and
arguments as a method of its superclass, the
method overrides the definition in the
superclass. When the method is called for an
object of the subclass, its the new definition
thats called. We have already seen this in
operation in applet definition when providing
definitions for init and paint methods. The init
and paint methods defined in JApplet are
overridden to provide specific implementations in
our applets.
10Relationship between Superclass and Subclass
Objects An object of a subclass can be treated
as an object of its superclass. Despite the fact
that object of a variety of classes derived from
a common superclass will be quite different from
one another, they can be treated as objects of
the superclass. Using this idea, an array
containing elements of distinct type can
be defined. class Pet .. class Cat extends
Pet .. class Dog extends Pet .. class
Mouse extends Pet .. Pet animals new
Pet3
animals
Butch (Dog)
Tom (Cat)
Jerry (Mouse)
11Relationship between Superclass and Subclass
Objects An explicit cast can be used to convert
a superclass reference to a subclass
reference. Dog d (Dog)animals2 This only
be legitimately done if the suberclass reference
is actually referencing a subclass object of the
correct subtype. Dog d (Dog)animals0
This cast would cause a problem as animals0
is actually an instance of Cat, and would result
in a ClassCastException. The actual subclass
type can be tested for using the instanceof
operator animals0 instanceof Dog (false)
animals0 instanceof Cat (true)
12Relationship between Superclass and Subclass
Objects public class Point protected int x,
y //co-orinates of point public Point()
setPoint(0,0) //simple constructor
public Point(int a, int b) //constructor
setPoint(a,b) public void setPoint(int a,
int b) x a y b public int
getX() // get x co-ordinate return x
public int getY() // get y co-ordinate
return y public String toString()
return "" x ", " y ""
13Relationship between Superclass and Subclass
Objects All classes implicitly extends class
Object public class Point extends Object
. When the constructor of a subclass is called,
the constructor of the superclass is
automatically called. We can now use the
definition of class Point to define a class
Circle.
14Relationship between Superclass and Subclass
Objects public class Circle extends Point
protected double radius public Circle()
setRadius(0) // simple constructor
public Circle(double r, int a, int b)
super(a,b) // call constructor of superclass
setRadius(r) public void
setRadius(double r) if (r gt 0.0) radius
r else radius 0.0 public double
getRadius() return radius public
double area() return Math.PIradiusradius
public String toString() return
"Center "super.toString()" Radius
"radius
15Relationship between Superclass and Subclass
Objects import javax.swing. import
java.text.DecimalFormat public class
InheritanceTest public static void
main(String args) Point pointRef, p
Circle circleRef, c String output
p new Point(30,50) c new
Circle(2.7,120,89) output "Point p
"p.toString() "\nCircle c " c.toString()
pointRef c //assign Circle to pointRef
output "\n\nCircle c (via pointRef) "
pointRef.toString() circleRef
(Circle)pointRef output "\n\nCircle c
(via circleRef) "circleRef.toString()
DecimalFormat precision2 new DecimalFormat("0.00
") output "\nArea of c (via
circleRef) "
precision2.format(circleRef.area())
16 if (p instanceof Circle)
circleRef (Circle)p output
"\n\nCast successful" else
output "\n\np does not refer to a Circle"
JOptionPane.showMessageDialog(null,output,"Test
", JOptionPane.INFORMATION_MESSA
GE) System.exit(0)
17Constructors in Subclasses When an object of a
superclass is created, the superclass
constructor should be called to do any
initialisation of the superclass
instance variables. An explicit call to the
superclass constructor (via the super reference)
can be provided as the first statement of the
subclass constructor. If no such call is
provided, the system will automatically call
the default superclass constructor. Superclass
constructors are not inherited by subclasses.
18Mixing and Matching Subclass and Superclass
References There are four main rules governing
this activity Referring to a supeclass object
with a superclass reference is straightforward.
Referring to a subclass object with a subclass
reference is straightforward. Referring to a
subclass object with a superclass reference is
safe. Referring to s superclass object with a
subclass reference is a syntax error. The
subclass reference must first be cast to s
superclass reference.
19Case Study Circle, Point, Cylinder Using the
definitions of Point and Circle defined
previously, we may extend the hierarchy to add a
Cylinder class.
Point
Circle
Cylinder
20Case Study Circle, Point, Cylinder public class
Cylinder extends Circle protected double
height public Cylinder() setHeight(0)
public Cylinder(double h, double r, int a,
int b) super(r,a,b) setHeight(h)
public void setHeight(double h) if (h gt0)
height h else height 0 public
double getHeight() return height
public double area() return 2
super.area() 2 Math.PI radius height
public double volume() return
super.area() height public String
toString() return super.toString() "
Height " height
21Case Study Circle, Point, Cylinder import
javax.swing. import java.text.DecimalFormat pub
lic class Test public static void main(String
args) Cylinder cyl new
Cylinder(5.7,2.5,12,23) DecimalFormat
precision2 new DecimalFormat("0.00")
String output "X coordinate is " cyl.getX()
"\nYcoordinate is "
cyl.getY() "\nRadius is "
cyl.getRadius()
"\nHeight is " cyl.getHeight()
cyl.setHeight(10) cyl.setRadius(4.25)
cyl.setPoint(2,2) output "\n\nThe
new location, radius " "and
height of cyl are\n" cyl
"\nArea is " precision2.format(cyl.area())
"\nVolume is " precision2.format(cyl
.volume()) JOptionPane.showMessageDialog(
null,output,"Test",
JOptionPane.INFORMATION_MESSAGE)
System.exit(0)
22Case Study Circle, Point, Cylinder