3.6 BLOCKING INHERITANCE


3.6 BLOCKING INHERITANCE

Sometimes a compiler can do a better job of optimizing the function calls and data access if it is told in advance that a class will not be extended. Security considerations may also dictate that a user not be allowed to extend a vendor-supplied class.

Java gives you a convenient way to prevent a class from being extended. If the keyword final is used as a prefix in the header of a class, that class cannot be extended. We use this mechanism in line (A) of the following example to keep StudentUser from being extended in line (B).

 
//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(); } }

In Java, we can also be selective in controlling inheritance by declaring only certain methods to be final. When a method of a superclass is declared to be final, it cannot be overridden in a subclass. The subclass inherits the method alright, but it cannot provide its own implementation code for the method. Consider the following example in which print of User is final in line (C). Any attempt to override this function by providing a definition in the derived class StudentUser, as in line (E), will elicit an error report from the compiler.

 
//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) } }

The invocation

      us.print(); 

in line (F) will now use print as inherited from the superclass User. As a result, only the User slice of the StudentUser object will be printed out:

      name: Zaphlet age: 10 

The keyword final in Java programs has another role also that is similar to the role played by const in C++ programs. If the data member of a class is declared to be final, its value cannot be changed after initialization. By the same token, if a variable or a function parameter is declared to be final, its value cannot be changed after initialization. This const like role of final is discussed further in Chapter 7 for variables and in Chapter 11 for the data members of a class.

C++ does not provide a keyword like final for keeping a class from being extended or for blocking the override mechanism on a selective basis. However, a C++ class can be made non-extendible by placing its constructors in the private section of the class. For illustration, in the following example, class X cannot be extended since the X's constructor needed in class Y is not publically available. Restricting access to constructors in C++ is discussed in greater detail in Chapter 11.

 
//BlockInheritance.cc class X { int n; X( int nn ) { n = nn; } // constructor is private }; /* class Y : public X { // Error. X cannot be extended int m; public: Y( int nn, int mm ) : X( nn ) { m = mm; } }; */ int main() {}




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net