Professional Documents
Culture Documents
Oop Chapter 4
Oop Chapter 4
Chapter Three
Inheritance
- In inheritance, a new class is defined by means of an older, pre-existing class.
This leads to a situation in which, the new class has all the functionality of the
older, pre-existing class and, additionally, introduces its own specific
functionality. We say the new class inherits the functionality of another existing
class.
- While speaking of inheritance, the existing class is called the superclass (base
class) and the new class is called subclass (derived class).
Concept of Inheritance
- One advantage of OOP is the re-usage of code. The capability to define custom
data types using classes enables us to reuse the code that we develop. This works
well if we always need exactly the same object. In real world, however, we may
need an object that is almost similar to an already developed object but not
exactly similar. Inheritance enables us to reuse an object more quickly; thus
making slight adjustments where necessary.
Shape
TwoDimensionalShap ThreeDimensionalShape
1
Chapter Four Object Oriented Programming with Java
- Objects of all classes that extend a common superclass can be treated as objects
of that superclass. (i.e., such objects have an “is-a” relationship with the
superclass). However, superclass objects can’t be treated as objects of their
subclasses. For example, all cars are vehicles, but not all vehicles are cars.
Protected Members
- A superclass’s protected members can be accessed by members of that superclass,
by members of any class derived from that superclass and by members of other
classes in the same package. (protected members have package access).
Overriding a method
- If a class declares an instance method, then the declaration of that method is said
to override any and all methods with the same signature in the superclass.
- If a method declaration overrides the declaration of another method, then a
compile-time error occurs if they have different return types or if one has a
return type and the other is void.
- The access modifier of the overriding method must provide at least as much
access as the overridden method, or a compile-time error occurs. on more detail:
If the overridden method is public, then the overriding method must be public;
otherwise, a compile-time error occurs.
If the overridden method is protected, then the overriding method must be
protected or public, otherwise, a compile-time error occurs.
If the overridden method has default (package) access, the overriding method
must not be private; otherwise, a compile-time error occurs.
2
Chapter Four Object Oriented Programming with Java
- The second form of super keyword is some what similar to that of this pointer.
The difference is, super() always refers to the superclass of the subclass in which
it is used. this usage has the following general form:
super.member;
3
Chapter Four Object Oriented Programming with Java
- Here, member can either be a method or an instance variable. This second form of
super() is most applicable to situations in which member names of a subclass hide
members by the same name in the superclass. Consider the following example.
}//end class
- Although the instance variable i in ClassB hides the i in ClassA, super allows
access to the i defined in the superclass. Super can also be used to call methods
that are hidden by subclasses.
4
Chapter Four Object Oriented Programming with Java
- Final prevents inheritance. Sometimes, you will want to prevent a class from
being inherited. To do this, precede the class declaration with the keyword final.
Declaring a class as final implicitly declares all of its methods as final too.
Consider the following example.
Constructors in subclasses
- Instantiating a subclass object begins a chain of constructor calls in which the
subclass constructor, before performing its own tasks, invokes its direct
superclasse’s constructor either explicitly (via the super reference) or implicitly
(calling the superclasse’s default constructor or no-argument constructor).
Similarly, if the superclass was derived from another class, the superclass
constructor would be required to invoke the constructor of the next class up in the
hierarchy, and so on. The last constructor called in the chain is always the
constructor of class Object. The original subclass constructor’s body finishes
executing last.