Beruflich Dokumente
Kultur Dokumente
Chapter 9
Object-Oriented Programming:
Part I
Yes
• 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?
Java I--Copyright © 2000 Tom Hunter
Superclasses and Subclasses
JOptionPane is a JComponent.
JOptionPane is a Container.
JOptionPane is a Component.
JOptionPane is an Object.
• Classes
Object,
Component and
Container
are
Indirect Superclasses
to the JOptionPane.
• JComponent is the
Direct Superclass to the JOptionPane.
JComponent
JOptionPane
Java I--Copyright © 2000 Tom Hunter
Impact on
Access
Modifiers
private,
protected,
public
class Beta
{
public void accessMethod()
{
Alpha a = new Alpha();
a.iamprivate = 10; // Invalid
a.privateMethod(); // Invalid
}
}
Yes!
Objects of the same type have access to one
another’s private members.
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.
package
• With package access, other objects in the package
don’t have to bother going through the methods.
Or we can summarize:
public HourlyEmployee()
{
// implicit (hidden) call to Superclass Constructor.
hourly_rate = 0.0;
hours = 0;
}
public HourlyEmployee()
{
super( n, s ); // Explicit call to Superclass Constructor
hourly_rate = 0.0;
hours = 0;
}
money = hour.calc_pay();
System.out.println( “Money=” + money );
}
}
money = salr.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
• More on Polymorphism
problems
• Why? The Subclass can do many things the
Superclass cannot.
An HourlyEmployee is an Employee
(Subclass is a Superclass object)
but…
Point p;
p = new Point();
Point p;
p = new Point();
Circle c;
c = new Circle();
No ! A Circle
reference would
c expect many
methods that you
For this to even remotely won’t find in a
work, you would need to do a Point object.
“downcast” ?
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.
p instanceof Circle
Point Constructor
Circle Constructor
Circle Finalizer
Point Finalizer
super.finalizer();
super.super.finalizer();