Sie sind auf Seite 1von 28

CHAPTER 6 : INHERITANCE & POLYMORPHISM

DDC 3143 - OOP

6.1 INTRODUCTION

OOP allows new classes derived from existing classes. This is called inheritance important & powerful concept in java.

6.2 SUPERCLASSES & SUBCLASSES

In OOP terminology:

a class C1 derived from another class C2 is called subclass. C2 is called superclass A subclass inherits accessible data fields and methods from its superclass and may also add new data fields and methods.

Example: There is superclass Person having subclasses Employee , Teacher & Student. Employee class have subclasses Permanent Employee & Contract Employee

As per above example Person class reference can hold of its sub classes i.e. Employee, teacher, student, Permanent Employee or Contract class object.

6.2.1 USING the super KEYWORD


The keyword super refers to the superclass of the class in which super appears. It can be used in two ways : To call a superclass constructor Syntax super(); or super(parameters); To call a superclass method Syntax super.method(parameters)

Program that illustrates inheritance in java using person class

NOTE : You must use the keyword super to call the superclass. Invoking a superclass constructors name in a subclass causes a syntax error. Java requires the statement that uses the keyword super appear first in the constructor. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclassess constructors are not inherited in the subclass. They can only be invoked from the subclassess constructors, using the keyword super. If the keyword super is not explicitly used, the superclasss no-arg constructor is automatically invoked

6.3 Constructor Chaining


Constructor may invoke an overloaded constructor or its superclasss constructor. If none of them is invoked explicity, the compiler puts super() as the first statement in the constructor. For example:

6.4 Overriding Methods


A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overidding.

Example:

6.5 Overriding vs. Overloading


Overloading a method is a way to provide more than one method with the same name but with different signatures to distinguish them. To override a method, the method must be defined in the subclass using the same signature and same return type as in its superclass.

Overrides

Overloading

NOTE 1:

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.

NOTE 2:

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.

6.6 OBJECT CLASS and Its toString() Method


Every class in Java is decended from the java.lang.Object class. If no inherintance is specified when a class is defined, the superclass of the class is Object. Classes like String, StringBuffer, StringTokenizer, Loan, Circle are implicity the subclasses of Object.

Three frequently used methods in the Object Class are: equals method toString method Clone method

Equals method

Used to test whether two objects are equal. The syntax : Object1.equals(object2); Default implementation in the Object class: public boolean equals(Object Obj) { return(this == obj); }

Example:

Even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same ISBN number.

toString method

Return a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object.

The code displays something like Cylinder@15037e5

This message is not very helpful or informative Usually you should override the toString method so that it returns a digestible string representation of the object.

clone method

Sometimes you need to copy of an object. Mistakenly, you might use the assignment statement, as follows: newObject = someObject; This statement does not create a duplicate object. It simply assigns the reference of someObject to newObject. To create a new object with separate memory space, use the clone() method. newObject = someObject.clone()

Das könnte Ihnen auch gefallen