Course Instructor: Nazish Basir Page: 1/7 Institute of Information and Communication Technology, University of Sindh
Course Instructor: Nazish Basir Page: 1/7 Institute of Information and Communication Technology, University of Sindh
METHOD OVERRIDING
Declaring a method in sub class which is already present in parent class is known as method overriding.
Overriding is done so that a child class can give its own implementation to a method which is already
provided by the parent class. In this case the method in parent class is called overridden method and the
method in child class is called overriding method.
The main advantage of method overriding is that the class can give its own specific implementation to an
inherited method without even modifying the parent class code. In object-oriented terms, overriding
means to override the functionality of an existing method.
This is helpful when a class has several child classes, so if a child class needs to use the parent class
method, it can use it and the other classes that want to have different implementation can use overriding
feature to make changes without touching the parent class code.
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class A{
public void disp()
{
System.out.println("disp() method of parent class");
}
}
class B extends A{
public void disp(){
System.out.println("disp() method of Child class");
}
public void newMethod(){
System.out.println("new method of child class");
}
}
Class Demo{
public static void main( String args[]) {
/* When Parent class reference refers to the parent class object then
in this the method of parent classis called. */
/* When parent class reference refers to the child class object then
the method of child class is called. */
In the above example the call to the disp() method using second object (obj2) is runtime polymorphism
(or dynamic method dispatch).
Note: In dynamic method dispatch the object can call the overriding methods of child class and all the
non-overridden methods of base class but it cannot call the methods which are newly declared in the
child class.
In the above example the object obj2 is calling the disp(). However if you try to call
the newMethod() method (which has been newly declared in B class) using obj2 then you would give
compilation error with the following message:
Demo.java:26: error: cannot find symbol
obj2.newMethod();
^
symbol: method newMethod ()
location: variable obj2 of type A
The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in the original
overridden method in the superclass.
The access level cannot be more restrictive than the overridden method's access level. For
example: If the superclass method is declared public then the overridding method in the sub class
cannot be either private or protected.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
If a method cannot be inherited, then it cannot be overridden.
A subclass within the same package as the instance's superclass can override any superclass
method that is not declared private or final.
A subclass in a different package can only override the non-final methods declared public or
protected.
An overriding method can throw any uncheck exceptions, regardless of whether the overridden
method throws exceptions or not. However, the overriding method should not throw checked
exceptions that are new or broader than the ones declared by the overridden method. The
overriding method can throw narrower or fewer exceptions than the overridden method.
Constructors cannot be overridden.
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
super.show();
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
System.out.println("Area is " + f.area());
System.out.println("Area is " + r.area());
System.out.println("Area is " + t.area());
}
}
ABSTRACT CLASS
An abstract class is a class that is not completely implemented. Usually, an abstract class contains at least
one abstract method, that is, a method that specifies an API that subclasses should implement, but does
not provide an implementation for the method.
Because an abstract class is not complete, it cannot be used to instantiate objects. An abstract class can
be extended, however, so that its subclasses can complete the implementation of the abstract methods
and can be instantiated.
A class is declared to be abstract by including the abstract keyword in the class header, as shown in the
following syntax:
An abstract method is defined by including the abstract keyword in the method header and by using a
semicolon to indicate that there is no code for the method, as shown in the following syntax: