Title: Announcements
1Announcements
- Project 4 due last night
- Project 5 has been posted
- Milestone due March 1st
- Final Submission due March 8th
2Inheritance
3Introduction to Inheritance
- Inheritance allows us to define a general class
and then define more specialized classes simply
by adding new details to the more general class
definition. - A more specialized class inherits the properties
of the more general class, so that only new
features need to be programmed.
4Introduction to Inheritance, cont.
- example
- General class Vehicle might have instance
variables for weight and maximum occupancy. - More specialized class Automobile might add
instance variables for wheels, engine size, and
license plate number. - General class Vehicle might also be used to
define more specialized classes Boat and Airplane.
5Programming Example A Base Class
- class Person
-
- private String name
- public Person()
- name no name
-
- public Person(String _name)
- name _name
-
- //set and get name methods
- //sameName method
- //writeOutput method
6Derived Classes
- Consider a college record-keeping system with
records about students, faculty and staff.
7Derived Classes, cont.
- Even though your program may not need any Person
or Employee objects, these classes can be useful
for consolidating and representing features
common to all subclasses. - For example, all students, faculty, and staff
have names, and these names may need to be
initialized, changed, retrieved, or printed.
8Derived Classes, cont.
public class Student extends Person private int
studentNumber //default constructor goes
here public Student(String _name, int
_num) super(_name) studentNumber
_num //more methods not in Person
class //writeOutput ? overwrites the method in
Person class
- class Student is a derived class of class Person
and class Person is called the base class.
9Derived Classes, cont.
- When you define a derived class, you declare only
the added instance variables and you define only
the added and overridden methods. - The variables and methods of the parent class
which are not declared private are inherited
automatically. - Protected variables are available only to classes
derived from the class ? different than public
10Derived Classes, cont.
11Overriding Method Definitions
- Notice that class Student has a method
writeOutput with no parameters, and class Person
also has a method writeOutput with no parameters,
that class Student inherits. - When a derived class defines a method with the
same name and the same number and types of
parameters as a method in the base class, the
method in the derived class overrides the method
in the base class.
12Overriding Method Definitions, cont.
- When overriding a method, you can change the
method definition to anything you wish, but you
cannot change the methods heading or the
methods return type.
13Overriding vs. Overloading
- When you override a method, the new method
definition in the derived class has the same name
and the same number of types of parameters as the
method definition in the base class. - When the name is the same, but the number or
types of the parameters differs, whether in the
base class or in the derived class, the method is
overloaded in the derived class.
14The final Modifier
- You can prevent a method definition from being
overridden by adding the word final to the method
heading. - example
- public final void someMethod()
-
-
- This is used rarely, but it produces more
efficient code.
15Constructors in Derived Classes
- A base class has its own constructors.
- Their purpose typically is to initialize the
instance variables declared in the base class. - A derived class has its own constructors.
- Their purpose typically is to call a constructor
in the base class, and then to initialize the
instance variables declared in the derived class.
16Using super
- The call to the constructor in the base class
(using super) must be the first action taken in
the constructor of a derived class. - When no call to the constructor in the base class
is included, Java automatically includes a call
to the default constructor in the base class. - super(initialName)
- not
- Person(initialName) //ILLEGAL
17Using super, cont.
- equivalent definitions
- public Student()
-
- super()
- studentNumber 0
-
- and
- public Student()
-
- studentNumber 0
18The this Method
- Within the definition of one constructor, it can
be appropriate to call another constructor in the
same class. - The keyword this is used to call another
constructor in the same class. - Saves effort if code is changed later
- example
- this(initialName, 0)
19The this Method, cont.
- Any use of this must be the first action in the
constructor definition. - Thus, a constructor definition cannot contain a
call using super and a call using this. - To use both super and this, include a call using
this in one constructor and a call using super in
the constructor called using this.
20Calling an Overridden Method
- super can be used to call a method in the base
class that has been overridden in the derived
class. - Does not have to be the first line of the method
like in the constructor - example
- super.writeOutput()
- However, you cannot repeat the use of super to
invoke a method in some ancestor class other than
the immediate base (parent) class.
21Programming Example Multilevel Derived Classes
- Class Undergraduate can be derived from class
Student which is derived from class Person. - Class Undergraduate will have all the instance
variables and methods of class Student which has
all the instance variables and methods of class
Person.
22Programming Example Multilevel Derived Classes,
cont.
class Undergraduate private int level public
Undergraduate() super() level
1 //more methods specific to an
undergraduate //overwrite writeOutput
23An Object Can Have More than One Type
- If class Undergraduate is derived from class
Student and class Student is derived from class
Person, then every object of class Undergraduate
is also an object of class Student and an object
of class Person. - A reference to an object of class Undergraduate
can be substituted for a reference to an object
of class Student or a reference to an object of
class Person.
24An Object Can Have More than One Type, cont.
- Given
- public static void compareNumbers (Student s1,
Student s2) - then either
- SomeClass.compareNumbers
- (studentObject, undergradObject)
- or
- SomeClass.compareNumbers
- (undergradObject, studentObject)
- could be used.
25An Object Can Have More than One Type, cont.
- However, a reference to an object of class person
cannot be substituted for a reference to an
object of class Student or an object of class
Undergraduate. - A reference to an object of an ancestor cannot be
substituted for a reference to an object of a
derived class.
26An Object Can Have More than One Type, cont.
- Hence, given
- public static void compareNumbers (Student s1,
Student s2) - neither
- SomeClass.compareNumbers
- (studentObject, personObject)
- nor
- SomeClass.compareNumbers
- (personObject, studentObject)
- could be used.
27The Class Object
- In Java, every class descends from (and inherits
features from) the Object class. - Therefore, every object of every class is of type
Object. - Unless a class is declared explicitly to be a
descendant of some other class, it is an
immediate descendant of the class Object.
28The Class Object, cont.
- An object of any class can substituted when a
parameter of type Object is expected. - Every class inherits some methods from the class
Object - equals()
- toString()
- but usually these methods are overridden by the
derived class or by an intermediate ancestor
class.
29Method toString
- Inherited method toString takes no arguments.
- Typically, method toString is coded to produce
and return a string which contains everything of
interest about the object.
30Abstract Classes
- An abstract class is not intended to be used to
create objects. - By declaring one or more methods to be abstract
and by omitting the method body, only objects of
derived classes which override the method(s) can
be instantiated. - example
- public abstract void drawHere()
- A class that has at least one abstract method
must be declared abstract.
31Abstract Classes, cont.
Public abstract class Figure private int
offset public abstract void drawHere() public
void drawAt(int num) int count for(count
0 count lt num count) System.out.println()
drawHere()
32Interfaces
- An interface specifies the headings for methods
that must be defined for any class that
implements the interface.
33Interfaces, cont.
34Interfaces, cont.
- To implement an interface, a class must
- include the phrase
- implements Interface_Name
- at the start of the class definition
- example
- implements MyInterface, YourInterface
- implement all the method headings listed in the
definition of the interface.
35Dynamic Binding
- Different objects can invoke different method
definitions using the same method name. - For example, if b references a Box and t
references a Triangle, b and t invoke different
definitions of method drawAt even if b and t are
declared to be objects of type Figure.
36Dynamic Binding, cont.
- Handling the invocation of a method that may be
overridden later is called dynamic binding or
late binding. - The type of object being referenced at the time
of the method call, not the type of reference
that was declared, determines which method is
invoked.
37Type Checking and Dynamic Binding
- Recall that an object reference to an ancestor
class can refer to an object of a descendant
class. - Employee e new Employee()
- Person p
- p e
- However, you can invoke only a method in class
Person with the variable p.
38Type Checking and Dynamic Binding, cont.
- However, if a method is overridden in the class
Employee, and variable p references an Employee
object, then the method in class Employee is
used. - The variable determines what methods can be used,
but the type referenced by the object determines
which definition of the method will be used.
39Type Checking and Dynamic Binding, cont.
- To use a method name in the class Employee with
an object named by the variable p of type Person,
use a type cast. - example
- Employee e (Employee)p
- e.setEmployeeNumber(5678)
40Type Checking and Dynamic Binding, cont.
- However, even a type cast cannot fool Java
- example
- Box b new Box (1, 4, 4)
- Figure f (Figure)b
- f. drawHere()
- will use the definition of drawHere given in
- class Box, not the definition of drawHere
- given in class Figure.
41Another Dynamic Binding Example
- Recall the Person class
- Person x new Person100
- x0 new Student()
- x1 new Graduate()
- x2 new Staff()
- How does the compiler decide which method
xi.writeOutput() is calling?
42Determining Class Type
- In order to access a method in a subclass from an
object with a static type of the superclass, need
to downcast - Downcasting is unsafe! Need to know that
downcast is legal. - Use instanceof keyword or other methods
- x instanceof A true if x is an instance of
class A or a subclass of A - x.getClass().equals(A.class) true if x is an
instance of class A - A.class.isAssignableFrom(x.getClass()) is true if
x is an instance of class A or a subclass of A
43Determining Class Type
public class Animal protected int
age public void makeSound() SOP(Make
sound!) public class Ferret extends Animal
public Ferret() super() public void
eatSweets() SOP(Yummy!) public void
makeSound() SOP(Squeal!) public class
Platypus extends Animal public Platypus()
super() public void swim() SOP(Swim!)
public void makeSound() SOP(Quack!)
Animal a new Ferret() if (a instanceof
Ferret) ((Ferret)a).eatSweets() a new
Platypus() if (a.getClass()
Platypus.class) ((Platypus)a).swim() if
(Platypus.class.isAssignableFrom(a)) ((Platypus)a
).swim() ________________________________ Animal
a new Ferret() ((Platypus)a).swim() //
compiles, but runtime error!
44Subtle Difference
- Dynamic binding refers to the process carried out
by the computer. - Polymorphism can be thought of as something
objects do. - Polymorphism, encapsulation, and inheritance, and
considered to be the main features of
object-oriented programming.
45The ActionListener Interface
- The ActionListener interface has only one method
heading that must be implemented. - public void actionPerformed
- (ActionEvent e)
- A listener that responds to button clicks in an
applet or in a JFrame must implement the
ActionListener interface.
46Programming Example Smiley Face as a JFrame
- Class JFrame and every class derived from class
JFrame has a paint method. - The paint method can be redefined to draw a
figure.
47Programming Example Smiley Face as a Jframe,
cont.
48Programming Example Smiley Face as a Jframe,
cont.
49Programming Example Smiley Face as a Jframe,
cont.
50Summary
- You have become acquainted with inheritance.
- You have learned how to define and use derived
classes. - You have learned about dynamic binding and
polymorphism. - (optional) You have learned about the class
JFrame used to produce windowing interfaces.