10.8. Key Terms

 
[Page 331 ( continued )]

Chapter Summary

  • You can derive a new class from an existing class. This is known as class inheritance . The new class is called a subclass , child class , or extended class . The existing class is called a superclass , parent class , or base class .

  • A constructor is used to construct an instance of a class. Unlike properties and methods , the constructors of a superclass are not inherited in the subclass. They can only be invoked from the constructors of the subclasses, using the keyword super .

  • A constructor may invoke an overloaded constructor or its superclass's constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor.

  • To override a method, the method must be defined in the subclass using the same signature as in its superclass.

  • An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

  • Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

  • Every class in Java is descended from the java.lang.Object class. If no inheritance is specified when a class is defined, the superclass of the class is Object .

  • If a method's parameter type is a superclass (e.g., Object ), you may pass an object to this method of any of the parameter's subclasses (e.g., Circle or String ). When an object (e.g., a Circle object or a String object) is used in the method, the particular implementation of the method of the object that is invoked (e.g., toString ) is determined dynamically.


    [Page 332]
  • It is always possible to cast an instance of a subclass to a variable of a superclass, because an instance of a subclass is always an instance of its superclass. When casting an instance of a superclass to a variable of its subclass, explicit casting must be used to confirm your intention to the compiler with the ( SubclassName ) cast notation.

  • You can override an instance method, but you cannot override a field (instance or static) or a static method. If you declare a field or a static method in a subclass with the same name as one in the superclass, the one in the superclass is hidden, but it still exists. The two fields or static methods are independent. You can reference the hidden field or static method using the super keyword in the subclass. The hidden field or method can also be accessed via a reference variable of the superclass's type.

  • When invoking an instance method from a reference variable, the actual class of the object referenced by the variable decides which implementation of the method is used at runtime . When accessing a field or a static method, the declared type of the reference variable decides which method is used at compile time .

  • You can use obj instanceof AClass to check whether an object is an instance of a class.

  • You can use the protected modifier to prevent the data and methods from being accessed by non-subclasses from a different package.

  • You can use the final modifier to indicate that a class is final and cannot be a parent class.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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