BLockInheritance.java
class User { private String name; private int age; public User( String str, int yy ) { name = str; age = yy; } public void print() { System.out.print( "name: " + name + " age: " + age ); } } //StudentUser cannot be extended final class StudentUser extends User { //(A) private String schoolEnrolled; public StudentUser( String nam, int y, String sch ) { super(nam, y); schoolEnrolled = sch; } public void print() { super.print(); System.out.println( " school: " + schoolEnrolled ); } } //Wrong: //class UndergradStudentUser extends StudentUser { } //(B) class Test { public static void main( String[] args ) { StudentUser us = new StudentUser( "Zaphlet", 10, "Cosmology" ); us.print(); } }
The results of uncommenting line (B):
C:\classes\ece538\work\ch03>javac BlockInheritance.java BlockInheritance.java:24: cannot inherit from final StudentUser class UndergradStudentUser extends StudentUser { } //(B) ^ BlockInheritance.java:24: cannot find symbol symbol : constructor StudentUser() location: class StudentUser class UndergradStudentUser extends StudentUser { } //(B) ^ 2 errors
BLockInheritance2.java
// This code example is from the following source: // // Book Title: Programming with Objects, A Comparative Presentation // of Object Oriented Programming with C++ and Java // // Chapter: Chapter 3 ---- The Notion Of A Class And Some Other Key Ideas // // Section: Section 3.6 -- Blocking Inheritance // // The links to the rest of the code in this book are at // // http://programming-with-objects.com/pwocode.html // // For further information regarding the book, please visit // // http://programming-with-objects.com // //BlockInheritance2.java class User { private String name; private int age; public User( String str, int yy ) { name = str; age = yy; } //cannot be overridden: final public void print() { //(C) System.out.print( "name: " + name + " age: " + age ); } } //cannot be extended: final class StudentUser extends User { //(D) private String schoolEnrolled; public StudentUser( String nam, int y, String sch ) { super(nam, y); schoolEnrolled = sch; } /* public void print() { // ERROR //(E) super.print(); System.out.println( "school: " + schoolEnrolled ); } */ } class Test { public static void main( String[] args ) { StudentUser us = new StudentUser( "Zaphlet", 10, "Cosmology" ); us.print(); //(F) } }
Maintained by John Loomis, last updated 30 Dec 2006