Introduction to Java Programming
Prof. K. ADISESHA Prof. K. ADISESHA Prof. K. ADISESHA Prof. K. ADISESHA www.adisesha.9f.com | email:
[email protected] 48
oc1.method7();
oc1.method8(100);
System.out.println("oc1.field1 : " + oc1.field1);
System.out.println("oc1.field2 : " + oc1.field2);
System.out.println("sc3.field1 : " + sc3.field1);
System.out.println("sc3.field2 : " + sc3.field2);
sc3.method5();
OverridingClass overClass = new OverridingClass() ;
SuperClassWithDifferentMethods supClass = (SuperClassWithDifferentMethods) overClass;
supClass.method5();
supClass.method1();
}
}
Output : MethodOverridingDemo.java
OverridingClass.method1()
SuperClassWithDifferentMethods.method2()
OverridingClass.method5()
OverridingClass.method6()
OverridingClass.method7()
OverridingClass.method8()
oc1.field1 : 30
oc1.field2 : 40
sc3.field1 : 10
sc3.field2 : 20
SuperClassWithDifferentMethods.method5()
SuperClassWithDifferentMethods.method5()
OverridingClass.method1()
The new method definitions in the subclass OverridingClass have the same signature and the same return type as
the methods in the superclass SuperClassWithDifferentMethods. The new overridden method6 definition
specifies a subset of the exceptions (CustomException). The new overridden method7 definition also widens the
accessibility to public from private. The overriding method8 also declares the parameter to be final, which is not a
part of the method signature and Method Overriding holds good. A static method cannot be overridden to be non-
static instance method as shown in the overridden method declaration of method9. A static method is class-
specific and not part of any object, while overriding methods are invoked on behalf of objects of the subclass.
There are no such restrictions on the fields, as for fields only the field names matter. A final method cannot be
overridden, an attempt to which will result in a compile-time error. A private method is not accessible outside the
class in which it is defined; therefore, a subclass cannot override it.
A subclass must use the super keyword in order to invoke an overridden method in the superclass. A subclass
cannot override fields of the superclass, but it can hide them. Code in the subclass can use the keyword super to
access members, including hidden fields.
The following distinction between invoking instance methods on an object and accessing fields of an object must
be noted. When an instance method is invoked on an object using a reference, it is the class of the current object
denoted by the reference, not the type of the reference, that determines which method implementation will be
executed. When a field of an object is accessed using a reference, it is the type of the reference, not the class of
the current object denoted by the reference, that determines which field will actually be accessed. This is
demonstrated in the above program
Java toString Method
Implementing toString method in java is done by overriding the Objects toString method. The java toString()
method is used when we need a string representation of an object. It is defined in Object class. This method can
be overridden to customize the String representation of the Object. Below is a program showing the use of the
Objects Default toString java method.