chapter 6 oop
TRANSCRIPT
6.1 INTRODUCTIONOOP 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)
NOTE : You must use the keyword super to call the superclass. Invoking a superclass constructor’s 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 superclasses’s
constructors are not inherited in the subclass. They can only be invoked from the subclasses’s
constructors, using the keyword super. If the keyword super is not explicitly used, the
superclass’s no-arg constructor is automatically invoked
6.3 Constructor ChainingConstructor may invoke an overloaded
constructor or it’s superclass’s constructor.
If none of them is invoked explicity, the compiler puts super() as the first statement in the constructor.
For example:
6.4 Overriding MethodsA 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.
6.5 Overriding vs. OverloadingOverloading 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.
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 1:
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.
NOTE 2:
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.
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);}
Equals method
Even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same ISBN number.
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.
toString method
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.
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()
clone method