CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Doc 9, Inheritance pt 2, Overriding and Hiding
[To Lecture Notes Index]
San Diego State University -- This page last updated Oct 1, 1996
Contents of Doc 9, Inheritance pt 2, Overriding and Hiding
Why not Use the Same rule for both Methods and Fields?
- The OO gods have decreed that overriding methods is good
- The OO gods have decreed that resolving overridden methods dynamically is
very good
- This is polymorphism, examples showing why this is good later
- Resolving hidden fields can not be done dynamically
class Parent {
public int name = 5;
public void test() {
System.out.println( "In Parent \t" + (name / 3) );
}
}
class Child extends Parent {
public String name = "Can't divide me";
}
class FieldsMustBeResolveStaticly {
public static void main( String args[] ) {
Child trouble = new Child();
trouble.test();
}
}
- Either resolving hidden fields statically or do not allow fields to be
hidden