Title: ITM 172
1ITM 172
- Chapter 8
- Class Inheritance and Interfaces
- Part A
2Inheritance
- One of the most important concepts in OO
programming is inheritance. - The idea of inheritance is that a class can be
derived from another class. In other words, class
B can be a class that is derived by adding on to
class A.
3Inheritance Youve been using it all along
- All the classes youve created up to now were
implicitly derived from class Object in the
java.lang package. - Classes can also be explicitly derived from
another class using the extends keyword in the
class header - Class B extends class A
-
4Package java.lang
5Super classes and Sub Classes
- If class B is derived from class A, class B is
called the sub class and class A is called the
super class. - The sub class inherits all the methods (except
the constructor(s)) and instance variables from
its super class. - Usually, the sub class adds its own additional
methods and instance variables.
6Example of Inheritance
- There could be super class Person
- With instance variables first name, last name,
address, birthdate, social security number and
methods setName( ), getName( ), getAddress( ),
setAddress( ), etc. - Class Employee could be derived from class Person
- With additional instance variables hire date,
position, salary and additional methods
setHireDate( ), getHireDate( ), getPosition( ),
etc. - Class Student could also be derived from class
Person - With additional instance variables major, admit
date, advisor and additional methods getMajor(
), setMajor( ), etc.
7Example of Inheritance
- Now if Class Employee is derived from Class
Person, then Class Employee has all the instance
variables and methods that Person has plus the
variables and methods it adds of its own. - So if Class Person has 4 instance variables and 8
methods, and Class Employee adds 2 more instance
variables and 4 more methods, Class Employee now
has 4 2 or 6 instance variables and 8 4
methods.
8Other features of inheritance
- An object of Class Employee is also an object of
Class Person (because an employee has all the
instance variables and methods that a person has)
but. - An object of Class Person is not also an object
of Class Employee.
9Therefore.
- If a method in say, class BankAccount, named
setBranchLocation( ), may require a Person object
as one of its incoming arguments. - You can pass that method either a Person object
or an Employee object (because an Employee is a
Person). - But if a method in class, say InsuranceCoverage
whose name is addBeneficiary( ) requires an
Employee object as one of its incoming arguments,
you can pass it an Employee object, but not a
Person object (because a Person is not
necessarily an Employee).
10Another example of Inheritance
- GUI components (windows, frames, icons,
textboxes, dialog boxes, scroll bars, menus,
etc.) are instances of objects whose classes are
defined in the javax.swing and the java.awt
packages. - Many of these objects are created by inheriting
the variables and methods of a more basic GUI
component. - For example, a Component -gt Container -gt Window
-gt DialogBox -gt JDialogBox - (see page 409)
11Lab 8.1 Class Cylinder
- Class Cylinder is created by extending Class
Circle. - Class Cylinder adds one additional instance
variable length and five additional methods,
two of which are its own constructor methods.
Therefore, a Cylinder object has a total of two
instance variables (radius and length) and eight
methods (five of its own, and three methods that
it inherited from Circle. - Note that super class constructor methods arent
inherited by the sub class.
12Page 308-309 (modified)
Here is Class Cylinder which is derived from
class Circle (from Chapter 6). Note that
Chapter6.Circle must be imported.
13Class Cylinders constructors
- Class Cylinder has two constructor(s). Each one
contains the super( ) command that explicitly
calls one of Class Circles constructors. - When objects are instantiated, and memory is
allocated for their members, objects are build
from the inside out That is, when an object of
Class Cylinder is instantiated, a Circle object
is instantiated first and then the Cylinder
object is created by adding on the additional
members.
14Here is one of Cylinders constructors
- public Cylinder( )
-
- super( )
- length 1 // default value of 1
-
- Cylinders constructor explicitly calls Circles
constructor using the super( ) command. But
recall that Circle has two constructor methods.
The compiler sees that there are no arguments
between the ( ), so he chooses this constructor - public Circle( )
- radius 1.0
The constructor that doesnt takes any arguments.
15Here is another one of Cylinders constructors
- public Cylinder (double r, double l)
-
- super (r)
- length l
-
- In this case, the super( ) command has r
between the parenthesis. Therefore, the compiler
automatically chooses this constructor method for
creating a Circle - public Circle (double r)
- radius r
The constructor that takes one argument of type
double.
16The accessor methods
- Because Cylinder added an instance variable, we
should now add a get and set method for that
variable. - He inherited getRadius( ) and setRadius( ) from
Circle.
17The calculation method(s)
- Class Cylinder adds one additional calculator
method findVolume( ) - public double findVolume( )
- return findArea( ) length
- Notice that the findVolume( ) method of class
Cylinder calls the findArea( ) method of class
Circle.
18 Lab 8.1 continuedClass TestCylinder
- An object of Class Cylinder doesnt exist until
another class comes along and creates/instantiates
it. - Next, create executable class TestCylinder with a
main( ) method who creates an instance of a
Cylinder, and then calls some of the eight
methods in class Cylinder.
19Page 309-310
An object of class Cylinder is instantiated by
the main( ) method of class TestCylinder. Because
two arguments (5.0 and 2.0) were passed to the
constructor, we can assume that the compiler
invoked the 2nd version of the constructor.
20Using the keyword super
- There are two reasons to use the keyword super
and both have to do with resolving ambiguity.
That is, when there is a situation where there is
more than one method with the same name. The
programmer may need to specify which method he is
calling.
21Using the keyword super to call a specific
constructor method
- Recall that Class Cylinders constructor used
super( ) to call Class Circles constructor. - But Class Circle has two constructors.
- Class Cylinders constructor wanted to be
specific about which one of the two Circle
constructors it was calling. He specifies by
either including arguments within the parenthesis
of the super( ) command or not either super( )
or super(r) - Class Cylinders constructor has to call Class
Circles constructor and if there is no explicit
call to super( ), the compiler will do so
automatically. But if the compiler calls Circles
constructor, he automatically calls Circles
default constructor.
22Using the keyword super to call a
non-constructor method
- It is possible that class Circle could have a
method named findArea( ) and class Cylinder could
also have a method named findArea( ). - Again, the keyword super is only necessary when
you want to be explicit about which method you
are calling. - Now Cylinder can call Circles findArea( ) method
or its own findArea( ) method. (But Circle can
only call its own findArea( ) method.) - If Cylinder wants to call Circles findArea( )
method (and not his own), he needs to do this - super.findArea( )
23Over-riding methods
- We just talked about the possibility that Circle
could have a findArea( ) method and so could
Cylinder have a findArea( ) method. - This is called over-riding a method From an
external perspective, Cylinders findArea( )
method cancels out Circles findArea( ) method. - Therefore, if another class, say TestCylinder
(who has an external perspective), has a method,
say main( ) that creates a Cylinder object, and
then calls findArea( ), TestCylinders main( )
can only invoke Cylinders findArea( ) method
(because Circles findArea( ) has been
over-ridden by Cylinders findArea( )).
24Lab 8.2 class Cylinderadd findArea( ) method
- This time we will give Class Cylinder his own
findArea( ) method. - But Cylinder already inherited a findArea( )
method from Class Circle. - But Circles findArea( ) method finds the area of
a Circle, and Cylinder needs a findArea( ) method
that finds the area of a Cylinder. - So we create another findArea( ) method in
Cylinder and because it has the same name, it
will override Circles findArea( )method.
25Cylinders findArea( )
- public double findArea( )
- return 2 super.findArea( ) 2
getRadius( ) Math.PI length
Cylinders findArea( ) can still invoke Circles
findArea( ) using the super prefix.
26Page 312-313
27Now there are 2 findArea( ) methods
- Inside Class Cylinder, two findArea( ) methods
exist and either can be invoked from within the
class definition. - Outside Class Cylinder, when findArea( ) is
invoked on a Cylinder object, its Cylinders
findArea( ) that is invoked, when findArea( ) is
invoked on a Circle object, its Circles
findArea( ) that is invoked.
28Lab 8.2 (continued)
- Next, we can add to our existing class
testCylinder by calling the findArea( ) method
for a Cylinder object. - The compiler invokes Cylinders findArea( ).
29Page 313
Lab 8.2 TestOverRideMethods
Another classs main( ) method comes along and
creates a Cylinder object and then calls
findArea( ). The compiler automatically invokes
Cylinders findArea( ) because it has over-ridden
Circles findArea( ) for a Cylinder object.
30Homework J8