Java i lecture_10_upd1

114
Java I--Copyright © 2000 Tom Hunter

Transcript of Java i lecture_10_upd1

Page 1: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Page 2: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Chapter 9

Object-Oriented Programming: Part I

Page 3: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and

Subclasses

Page 4: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So far I this course we have covered two of the Big Three principles of Object-Oriented Programming:

1.) Encapsulation—completely covered

2.) Inheritance—briefly covered

3.) Polymorphism—still to come.

Superclasses and Subclasses

Page 5: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• As we know, in Inheritance we stand on the shoulders of earlier work.

• We receive all the methods and variables of an earlier class.

Superclasses and Subclasses

Page 6: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Polymorphism lets us write programs in a general way.

• Although we write the programs in a general way, the objects themselves cause the behavior to be specific.

Superclasses and Subclasses

Page 7: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• This lets us write programs that will work correctly even for objects that haven’t been invented yet.

Superclasses and Subclasses

Page 8: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Inheritance

Page 9: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• We have already been using the terms Superclass and Subclass.

• A Superclass is the parent class.

• A Subclass is the child class.

• A Subclass inherits the behavior and variables of the Superclass.

Superclasses and Subclasses: Inheritance

Page 10: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• A Subclass inherits all the methods and all the encapsulated data variables of its Direct Superclass.

(the Superclass immediately above a Subclass is called its Direct Superclass.)

Superclasses and Subclasses

Page 11: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Class JOptionPane inherits from class JComponent.

So that means JOptionPane inherits all the methods and all the encapsulated data variables that lie within JComponent.

Page 12: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• JComponent is the Superclass

• JOptionPane is the Subclass

Superclasses and Subclasses: Inheritance

Page 13: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Correct. So, that tells us JComponent inherits all the methods and all the encapsulated data variables of Container .

• Container is the Superclass.

Superclasses and Subclasses: Inheritance

• But wait a second. Isn’t JComponent the Subclass of Container?

Page 14: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So, when JOptionPane inherits everything from JComponent (which itself inherited everything from

Container), that must mean JOptionPane inherits everything from both JComponent and Container. Is that true?

Superclasses and Subclasses: Inheritance

Yes

Page 15: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So, let’s put this together: the JOptionPane inherits all the methods and all the encapsulated data variables in…

ALL THE SUPERCLASSES ABOVE IT!

Superclasses and Subclasses

How could it do that?

Page 16: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• How?

When we want to create a copy of some object, what do we do?

Superclasses and Subclasses

• Answer:

We use the new keyword to fire off the Subclass object’s Constructor . We instantiate the object.

• And so, if we want to create a copy of the SuperSuperclass, we need to...

Somehow, fire off the SuperSuperclass object’s Constructor.

Page 17: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Whenever you extended another class, (a Superclass) you have created an instance of every class above your classevery class above your class in the class hierarchy.

• When the Constructor for Sales fires, it silently also fires the Constructors for JApplet and every other class above JApplet in the hierarchy.

Superclasses and Subclasses

public class Sales extends JApplet{

...}

Page 18: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• The Subclass adds to what it inherits from the Superclass.

• Because we say it adds, that implies it already has everything else!

• By the time your Subclass is created, it already has an instance of every Superclass above it and every single thing the Superclasses contain!!!!!

Superclasses and Subclasses

Page 19: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Every method, every data variable present in the Superclass is also present in the Subclass.

• Make sure you understand this:

If my class has everything above it—then I can say it isis an example of the classes above it.

Superclasses and Subclasses

Page 20: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Finally, since the Subclass has everything the Superclass has, we can say:

Superclasses and Subclasses

“Every Subclass is also an

object of the Superclass.”

Page 21: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• I’m a resident of Omaha, with everything that implies.

Superclasses and Subclasses: an Analogy

• But, at the same time I’m an Omahan, I’m also a resident of Nebraska.

• When I become a resident of Omaha, I inherit everything it means to be a Nebraskan.

• Likewise, when I moved from Russia to Omaha, I not only became an Omahan, I also became a Nebraskan and a United States resident.

Page 22: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So, let’s ponder this: JOptionPane inherits from JComponent.

• The JOptionPane inherits everything JComponent possesses.

Superclasses and Subclasses

Page 23: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So, we can say—at the same time

JOptionPane is a JComponent.

JOptionPane is a Container.

JOptionPane is a Component.

JOptionPane is an Object.

Superclasses and Subclasses

Page 24: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Classes Object, Component and Container are Indirect Superclasses to the JOptionPane.

Superclasses and Subclasses

• JComponent is the Direct Superclass to the JOptionPane.

Page 25: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• We say JOptionPane is a JComponent, is a Container, is a Component, is an Object.

Superclasses and Subclasses

Object

Component

Container

JComponent

JOptionPane

This little arrow pointing upwards indicates this is an

“is a” relationship.

Page 26: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Impact on Access

Modifiers

Page 27: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Impact on Access Modifiers

• Access Modifiers are among the thorniest and most confusing aspects of OOP.

• But, remember, encapsulation is one of the primary benefits of object orientation, so access is important.

Superclasses and Subclasses

Page 28: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Impact on Access Modifiers

• The familiar private access modifier lets us shield our instance variables from the prying eyes of the outside world.

• Relying on Access Modifiers, you can shield both your class’s private instance variables and its private methods.

Superclasses and Subclasses

Page 29: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Impact on Access Modifiers• With those ends in mind, Java lets review the four different levels of access:

private,

protected,

public

and the default if you don’t specify package.

Superclasses and Subclasses

Page 30: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• A summary of the various types of access modifier:

Specifier class subclass package worldprivate X

package X X

protected X X X

public X X X X

Page 31: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• As you can see, a class always has access to its own instance variables, with any access modifier.

Specifier class subclass package worldprivate X

package X X

protected X X X

public X X X X

Page 32: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• The second column shows that Subclasses of this class (no matter what package they are in) have access to public (obviously) and protected variables.Specifier class subclass package worldprivate Xpackage X Xprotected X X Xpublic X X X X

Page 33: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Therefore, the important point?

Subclasses cancan reach protected-access variables, but can’tcan’t reach package-access variables…

unless the Subclasses happen to be saved in the same package.

Page 34: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• The third column “package” shows that classes in the same package as the class (regardless of their parentage) have access to data variables.Specifier class subclass package worldprivate Xpackage X Xprotected X X Xpublic X X X X

Page 35: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• In the fourth column, we see that anything and anyone has access to a public data variable or method—defeating the purpose of encapsulation.Specifier class subclass package worldprivate Xpackage X Xprotected X X Xpublic X X X X

Page 36: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

private

Page 37: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Impact on Access Modifiers: private

• When my class inherits from a Superclass, I cannot access the Superclass’s private data variables. Private data is Secret!

• In other words, the Subclass cannot automatically reach the private data variables of the Superclass.

• A private data variable is accessible only to the class in which it is defined.

Superclasses and Subclasses

Page 38: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Objects of type Alpha can inspect or modify the iamprivate variable and can call privateMethod, but objects of any other type cannot.

class Alpha{

private int iamprivate;

public Alpha( int iam ){

iamprivate = iam;}

private void privateMethod(){ iamprivate = 2; System.out.println(“” + iamprivate);}

}

Page 39: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

The Beta class, for example, cannot access the iamprivate variable or invoke privateMethod on an object of type Alpha because Beta is not of type Alpha .

class Beta{

public void accessMethod(){

Alpha a = new Alpha();a.iamprivate = 10; // Invalida.privateMethod(); // Invalid

}}

• The compiler would complain if you tried this.

Page 40: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Can one instance of an Alpha object access the private data variables of another instance of an Alpha object?

Objects of the same type have access to one another’s private members.

Yes!

Page 41: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

package

Page 42: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Varieties of Access Modifiers: package

• If you do not specify the access for either a method or an encapsulated data variable, then it is given the default access:

package

Superclasses and Subclasses

Page 43: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Varieties of Access Modifiers: package

• This access allows other classes in the same package as your class to access its data variables as if they were their own.

package

• This level of access assumes that classes in the same package as your class are friends who won’t harm your class’ data.

Superclasses and Subclasses

Page 44: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

package Greek;class Delta{

int iamprivate;void privateMethod(){

System.out.println(“privateMethod”);}

}

• Notice that no access modifier is declared. So, iamprivate and method privateMethod both default to package access.• All classes declared in the package Greek, along with class Delta, have access to iamprivate and privateMethod.

Page 45: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Varieties of Access Modifiers: package

package

• If you use multiple objects from the same package, they can access each other’s package-access methods and data variables directly, merely by referencing an object that has been instantiated.

Superclasses and Subclasses

Page 46: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Varieties of Access Modifiers: package

package

• With package access, other objects in the package don’t have to bother going through the methods.

They can get right to the variables.

Superclasses and Subclasses

Page 47: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• So, when you don’t include any access modifier, you are in fact giving your variable package access.

int x; // package access instance variable

• Notice, it’s declared neither public nor private.

Superclasses and Subclasses

Page 48: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

protected

Page 49: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• protected allows the class itself, Subclasses and all classes in the same package to access the members.

• Generally speaking, protected offers greater access than package access.

Superclasses and Subclasses

Page 50: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Use the protected access level when it’s appropriate for a class’s Subclasses to have access to the member, but not for unrelated classes to have access.

• protected members are like family secrets—you don’t mind if someone in the family knows—but you don’t want outsiders to know.

Superclasses and Subclasses

Page 51: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• The Access Modifier protected can be used on either a method or an encapsulated data variable.

• protected serves as a middle ground between the private and public access modifier.

Page 52: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• A Superclass’s protected data variables may be accessed only by:

—methods of the Superclass—methods of the Subclass—methods of other classes in the same package.

Or we can summarize:

protected members have package access.

Page 53: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

protected

• protected access means the variable is:—public to the class, —private outside the class, and —public to the subclasses.

• It’s like the Subclasses can reach up and get access to the data variables of the Superclass.

Page 54: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

The super

Reference

Page 55: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Already, we know that the Superclass-Subclass interaction has a big effect on the Constructor.

• The Superclass Constructors are either implicitly or explicitly called first in the Constructor of a Subclass.

The super Reference

super( whatever the Superclass needs)

Page 56: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

The super Reference

super( whatever the Superclass needs)

• Using this super reference, you can call the call the ConstructorConstructor of your Subclass’ Direct Superclass.

• If you use this super reference, then it must be the very first statement in the Subclass’ Constructor.

Page 57: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class Employee{ String name; String SSN;

public Employee( String nm, String soc ) {

name = nm;SSN = soc;

}

public double calc_pay() {

return 0.0; }}

Page 58: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class HourlyEmployeeextends Employee

{ double hourly_rate; int hours;

public HourlyEmployee() {

// implicit (hidden) call to Superclass Constructor.hourly_rate = 0.0;hours = 0;

}

public HourlyEmployee( String n, String s, double r, int h ) {

// implicit (hidden) call to Superclass Constructor.hourly_rate = r;hours = h;

}

public double calc_pay() { return hours * hourly_rate; } }

Page 59: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class HourlyEmployeeextends Employee

{ double hourly_rate; int hours;

public HourlyEmployee() {

super( n, s ); // Explicit call to Superclass Constructorhourly_rate = 0.0;hours = 0;

}

public HourlyEmployee( String n, String s, double r, int h ) {

super( n, s );n // Explicit call to Superclass Constructorhourly_rate = r;hours = h;

}

public double calc_pay() { return hours * hourly_rate; } }

Page 60: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• There is a similar statement that is used to call your Subclass’ constructor within your Subclass.

• Don’t get the two confused.

The super Reference

super( whatever the Superclass needs)

this( whatever the Subclass needs)

Page 61: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Subclass can be Treated

as an Object of the

Superclass

Page 62: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• A Subclass contains more than its Superclass contains.

• Because of that, we say inside of a Subclass is a complete copy of its Superclass.

Page 63: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• On previous slides, we said a Subclass is an object of the Superclass type.

• Now, we take that one step further.

Page 64: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• A Subclass contains everything ( and more) required to make a complete example of the Superclass...

• Thus, we say an object of a Subclass can be treatedtreated as an object of its Superclass.

• In other words, in certain situations, we can ignore the difference between the Superclass and Subclass objects.

Page 65: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

“An object of a Subclass can be treated as an object of its Superclass.”

• What would stop this from being true?

• Is there any part of the Superclass that is missing from the Subclass?

Nope—it’s all there.

Page 66: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

“An object of a Subclass can be treated as an object of its Superclass.”

• Certainly if we are asking a Subclass to fill in for its parentfill in for its parent, we throw away the extra stuff that the Subclass added, but—because a Subclass “is an” example of its Superclass—we can “treat the Subclass as an example of the Superclass.”

Page 67: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• And, if we only want to call the methods

from the Superclass

and access the data variables that come

from the Superclass—what’s the difference?

Page 68: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• Doing this might be interesting.

• As long as we treat the reference like it only refers to the Superclass, we can handle a Subclass object from a Superclass “reference.”

• As long as we only call the methods that exist in the Superclass, this will work fine.

Page 69: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class Employee{ String name; String SSN;

public double calc_pay() {

return 0.0; }}

public class HourlyEmployeeextends Employee

{ double hourly_rate; int hours;

public double calc_pay() { return hours * hourly_rate; } }

public class SalariedEmployeeextends Employee

{ double monthlySalary;

public double calc_pay() { return monthlySalary; } }

Page 70: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class TestEmp{

public static void main( String args[] ){ double money = 0.0; HourlyEmployee hour; hour = new HourlyEmployee();

money = hour.calc_pay(); System.out.println( “Money=” + money );}

}

Page 71: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class TestEmp{

public static void main( String args[] ){ double money = 0.0; SalariedEmployee salr; salr = new SalariedEmployee();

money = salr.calc_pay(); System.out.println( “Money=” + money );}

}

Page 72: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class TestEmp{

public static void main( String args[] ){ double money = 0.0; Employee empl; empl = new SalariedEmployee();

money = empl.calc_pay(); System.out.println( “Money=” + money );}

}

• As long as we only call the methods that exist in the Superclass, this will work fine.

Page 73: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class TestEmp{

public static void main( String args[] ){ double money = 0.0; Employee empl; empl = new SalariedEmployee();

money = empl.calc_pay(); System.out.println( “Money=” + money );}

}

• So, why in the world might we want to do this?

Page 74: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class RetiredEmployeeextends Employee

{ double monthlyPension; public double calc_pay() { return monthlyPension; } } • Imagine if—after we built our system—we

decided to add a class, for Retired Employees.

• If we had built our program on a Superclass reference, then we wouldn’t have to rebuild anything.• The runtime environment sees what kind of object we have instantiated, and calls the right method override !

Page 75: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

public class TestEmp{

public static void main( String args[] ){ double money = 0.0; Employee empl; empl = new RetiredEmployee();

money = empl.calc_pay(); System.out.println( “Money=” + money );}

}

• Caution, this works only when you’re calling a method that exists in your Superclass, not one that exists only in the Subclass.(And so now you see the advantage of identifying a bunch of empty methods in your Superclass.)

Page 76: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• And (drumroll…) the whole process is called...

Page 77: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• More on Polymorphism

• We can create a Superclass reference that is an array.

• Then, when we instantiate the array, we can attach all different kinds of Subclass objects to the Superclass array reference.

Page 78: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• As long as we’re only calling methods that exist in the Superclass, we can work our way through the array and it will call all the correct overridden versions of each individual Subclass object.

Page 79: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• The Superclass reference only knows about the methods that exist in the Superclass.

• The Superclass only tries to call the methods it knows about.

• That’s perfectly fine, because all the methods in the Superclass are available in its Subclass.

Page 80: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• Therefore the Superclass isn’t aware that the object it references can do a whole lot more than the Superclass thinks it can.

Page 81: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• So, “We can create a Superclass reference array that actually points to a Subclass object.”

• As long as we treat those Subclass objects as if they were Superclass objects, (meaning we only call the methods in the Superclass) we have no problems.

Page 82: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• The Subclass knows how to do everything its parent Superclass can do.

• The kids can do everything the parent can.

Page 83: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• However, if we try to do it the other way around—treating the Superclass as if it were one of its children—then we can have...

Superclasses and Subclasses

problemsproblems

• Why? The Subclass can do many things the Superclass cannot.

• The kids can do many things the parent cannot.

Page 84: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• If we called the Superclass with the Subclass’s reference, then you might expect the Subclass reference can do all the things the kids can—and you’d be wrong!

Page 85: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• If you want to go the other way, attach a Superclass object to a Subclass reference, you have to do an explicit cast, as a way of informing the compiler that you really want to do this dumb thing.

Page 86: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• “A Superclass object is notnot a Subclass object.”

An HourlyEmployee is an Employee(Subclass is a Superclass object)

but…

An Employee is NOT an HourlyEmployee (Superclass is NOT a Subclass object)

Page 87: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Superclasses and Subclasses

• If you want to actually call some of the methods that don’t exist in the Superclass, then you have to first cast the object back to a reference of its own kind.

Page 88: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class:

The Rosetta Stone of Inheritance

Page 89: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

• Chapter 9, verse 9.4, page 391:

• Point is the Superclass, although it (secretly)inherits off of Object, the ultimate Superclass.

• It has two protected variables x and y that hold the location of the point.

• Look at line 8, the “no argument Constructor.What is happening there?

A Case Study of Inheriting a Class

Page 90: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Point p;p = new Point();

A Case Study of Inheriting a Class

• You recognize what is above. “Point p” creates a reference “p ” to an object of type Point.

• p = new Point(); fires the default Constructor for this class, thereby initializing the object and making sure it starts in a stable state.

Page 91: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Point p;p = new Point();

A Case Study of Inheriting a Class

• Line 11 calls another method of the class called setPoint( 0, 0 ).

• Since this is the default or “no argument” constructor, the Constructor goes ahead and hard codes the values “0, 0” that it sends to the setPoint( 0, 0 )method.

Page 92: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Look on line 10. See the comment there about the “implicit ( meaning unspoken) call to Superclass constructor occurs here.”

What does that mean?

• Think about this carefully: we have always said that the Subclass contains the Superclass object inside of it. Point is the (Subclass) to Object (Superclass).

Page 93: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• On line 10, right before we fire off the Subclass’s constructor, we first fire the Superclass’s constructor.

• On line 15 is the argument-taking Constructor. It again fires the implicit constructor for the Superclass (Object) object.

Page 94: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Circle inherits from Point. (extends Point)To create a circle you need a center point and a radius.

• Since class Point already has the center point instance variables, we don’t need to create our own instance variables—we inherit them from Point.

• We do need to create our radius variable, however.

Page 95: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Look at Line 48. This is the default no-argument constructor.

• But, before we can initialize our Subclass, we have to first initialize our Superclass, and so line 47 shows where the implicit call to the (Point) Superclass happens.

• And, of course, we know the first thing the Point constructor does is initialize Object, then Point.

Page 96: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• (page 392) Look at line 54, The Rosetta Stone of InheritanceThe Rosetta Stone of Inheritance: an explicit call to the Superclass constructor.

• This is the argument-taking Constructor: before we can initialize the Circle, we have to initialize Point, and before that, we have to initialize Object.

• On line 54, we see how we call the Superclass constructor: super.

Page 97: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• When you use the explicit (meaning stated) call to a Superclass Constructor, it must be the very first statement in the Constructor.

Page 98: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Let’s pause and take down all the methods in our Superclass Point:

Point()Point( a , b)setPoint( a, b)getX()getY()toString()

• These are all the abilities of class Point.

• So, a Point reference would be aware of these methods, and would be able to call them.

Page 99: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

toString()• What these methods do is fairly clear, with the exception of the toString().

• This method is inherited from Object.

• We are supposed to override toString() in every one of our classes.

• It turns the instance variables into a String and adds the name of the class. This is used for debugging.

Page 100: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Now go to page 392, lines 83-84: We are declaring some Point and Circle references.

• On lines 87-88—as you would expect—we are instanciating these objects by calling their constructors.

• In line 90 we are calling the p.toString() reference, and the c.toString() reference.

• Now, look at line 95. What is happening?

Page 101: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• On line 95, we are making it so that p (the Superclass) is pointing to the same object as c.

Point p = new Point();

Circle c = new Circle();

Page 102: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Point p;

p = new Point();

p

Circle c;

c = new Circle();

c

Page 103: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

p = c;

p

c

What if we try to call only the methods that both share?

Will this “p” reference work correctly?

This will work because any methodsone would expect to find in a Point reference (p) already exist in the Circle object.

Page 104: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

c = p;

p

c

No ! A Circle reference would expect many methods that you won’t find in a Point object.

Will this work for “c”?

For this to even remotely work, you would need to do a “downcast” ?

Page 105: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• On page 393, line 114, we’re attempting to do this error by casting a Subclass reference to a Superclass object.

circleRef = (Circle) pointRef;

• Notice how we use the operator:

p instanceof Circle

• This operator will tell us if p is a reference to the class Circle.

Page 106: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

A Case Study of Inheriting a Class

• Attempting to have a Superclass reference point to a Subclass object would lead to an error known as a ClassCastException.

Page 107: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructorsand

Finalizersin

Subclasses

Page 108: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• Whenever you instantiate a Subclass object, the Constructors for the Superclass should be called to initialize the instance variables of the Superclass.

• This instantiation can be done either implicitly or explicitly (via the super reference).

• These calls to instantiate the Superclass object must the first thing in your class’s Constructor method.

Page 109: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• The opposite of the Constructor method is the finalizer method.

• If your class bothers to define a finalizer method, then the implicit or explicit call to the Superclass finalizer must be the last thing in the finalizer method.

Page 110: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• You will always want to define your finalizer method as protected, so that Subclasses that inherit from your class can access its finalizer.

• Classes that only use Superclass objects (not inherit them) cannot use the Superclass’s finalizer methods.

Page 111: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• Look at the output for Fig 9.5.

Point Constructor

Circle Constructor

Circle Finalizer

Point Finalizer

Page 112: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• Turn to page 398. Lines 1-31 define class Point, which is our Superclass object.

• On line 34, we define class Circle, a Subclass of Point.• On page 399, line 54 is class Circle’s finalizer method. The last thing that Circle’s finalizer method does is call its Superclass (Point)’s finalizer method.

super.finalizer();

Page 113: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• Warning! You must always use this “super” reference when you intend to call the finalizer of the Superclass.

• If you omit the super, you’re actually calling the finalizer of the method you’re already inside of--which is called infinite recursion.

super.finalizer();

Page 114: Java i lecture_10_upd1

Java I--Copyright © 2000 Tom Hunter

Constructors and Finalizers in Subclasses

• You cannot do a cascading call to a Superclass. Instead, you have to rely on the Superclass itself to call its own Superclass

super.super.finalizer();