Beruflich Dokumente
Kultur Dokumente
6.1 INTRODUCTION
OOP allows new classes derived from existing classes. This is called inheritance important & powerful concept in java.
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.
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
Example:
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.
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.
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()