Java Reference
In-Depth Information
abstract methods in Java defines a specification whereas declaring a concrete (non-abstract) method defines
an implementation. For example, the Shape class has specification for a draw() method, which is inherited by its
subclasses (e.g. Rectangle and Circle) . It does not provide any implementation for the draw() method. All concrete
subclasses of the Shape class must provide implementation for its draw() method.
Multiple inheritance is defined as having a class inherit from more than one superclass. It poses some problems
when a class inherits an implementation from multiple superclasses. Suppose there are two classes, Singer and
Employee , and both provide implementation for processing salary (say pay() method). Further, suppose you inherit
a class SingerEmployee , which inherits from the Singer and Employee classes. The new class, SingerEmployee ,
inherits the pay() method from two different superclasses, which have different implementations. When the pay()
method is invoked on a SingerEmployee , which pay() method should be used—from the Employee class or from
the Singer class? Multiple inheritance makes programmer's job as well as language designer's job complex. Java
supports multiple inheritance of interfaces (or types), not implementations. It has a construct, called interface ,
which is different from a class . An interface can inherit from multiple interfaces. A class can implement multiple
interfaces. Java's approach to support only multiple inheritance of types avoids problems for programmers as well
as its designers. Multiple types inheritance is easier to understand and design than the multiple implementations
inheritance.
Summary
Inheritance lets you define a class based on the definition on another class. Inheritance is one of the techniques to
implement inclusion polymorphism. It promotes code reuse. It lets you write code in terms of a class that works for
the class and all its subclasses. The subclass inherits members on its superclass based on some rules. Constructors are
not members of a class and they are not inherited by subclasses.
The keyword extends is used to inherit a class from another class. If a class declaration does not contain the
keyword extends , the class implicitly inherits from the Object class. Inheritance creates a tree-like class
hierarchy—the Object class being at the top of all class hierarchy. The Object class itself does not have a superclass.
Java supports two types of binding: early binding and late binding. In early binding, the compiler determines
the fields and methods that will be accessed, based on the compile-time type of the references accessing the fields
and methods. Java uses early binding for accessing all types of fields and static methods. In late binding, the runtime
type of the reference variable determines the method that is executed. Inheritance along with late binding makes it
possible to use runtime polymorphism in Java. Java uses late binding for accessing instance methods.
A variable of a superclass can always be assigned a reference of its subclasses. This is called upcasting. When
a variable of a subclass is type cast and assigned to a variable of the superclass, it is called downcasting. For a
downcasting to succeed at runtime, the variable of the superclass must contain a reference of the subclass or the
subclasses of the subclass. The instanceof operator is used to test whether a reference variable is an instance of a
specific class.
You can declare abstract classes and methods. The keyword abstract is used to declare abstract classes and
methods. Abstract classes cannot be instantiated. If a class contains an abstract method, the class must be declared
abstract . A class can be declared abstract even if it contains no abstract methods. Abstract methods are supposed
to be overridden and provided an implementation by subclasses.
A subclass may access the constructors, methods, and fields of its superclass using the keyword super . The call to
access the constructor of the superclass must be the first statement in the constructor of the subclass.
Redefining the static methods of a superclass inside a subclass is called method hiding. A field with the same
name as a field in the superclass hides the field in the superclass and it is called field hiding. The hidden methods
may be accessed from the subclass using the superclass class name as the qualifier for the method. You can use the
keyword super to access the hidden fields from the subclass.
Classes and methods may be declared final . A final class cannot be subclasses. A final method cannot be
overridden. Declaring all constructors of a class private also stops subclassing for that class.
 
Search WWH ::




Custom Search