0% found this document useful (0 votes)
49 views

Course Instructor: Nazish Basir Page: 1/7 Institute of Information and Communication Technology, University of Sindh

Method overriding allows a child class to provide its own implementation of a method defined in the parent class. When an object of the child class is referenced by the parent class reference, the method called is determined at runtime based on the object's type, which is known as dynamic method dispatch. The super keyword allows a method in the child class to call the implementation of the same method in the parent class. Abstract classes cannot be instantiated and contain at least one abstract method that subclasses must implement.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Course Instructor: Nazish Basir Page: 1/7 Institute of Information and Communication Technology, University of Sindh

Method overriding allows a child class to provide its own implementation of a method defined in the parent class. When an object of the child class is referenced by the parent class reference, the method called is determined at runtime based on the object's type, which is known as dynamic method dispatch. The super keyword allows a method in the child class to call the implementation of the same method in the parent class. Abstract classes cannot be instantiated and contain at least one abstract method that subclasses must implement.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LECTURE NOTES AND LAB HANDOUTS

OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

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.

PROGRAM 1: Demonstrate simple method overriding

class Animal {
public void move() {
System.out.println("Animals can move");
}
}

class Dog extends Animal {


public void move() {
System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {


Animal a = new Animal(); // Animal reference and object
Dog b = new Dog(); // Animal reference but Dog object

a.move(); // runs the method in Animal class


b.move(); // runs the method in Dog class
}
}

METHOD OVERRIDING AND DYNAMIC METHOD DISPATCH


Method Overriding is an example of runtime polymorphism. When a parent class reference points to the
child class object then the call to the overridden method is determined at runtime, because during
method call which method(parent class or child class) is to be executed is determined by the type of
object. This process in which call to the overridden method is resolved at runtime is known as dynamic
method dispatch.

Course Instructor: Nazish Basir Page: 1/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

PROGRAM 2: Demonstrate Method Overriding and Dynamic Method Dispatch

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. */

A obj = new A();


obj.disp();

/* When parent class reference refers to the child class object then
the method of child class is called. */

A obj2 = new B ();


obj2.disp();
}
}

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

Course Instructor: Nazish Basir Page: 2/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

RULES OF METHOD OVERRIDING IN JAVA

 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.

SUPER KEYWORD IN METHOD OVERRIDING


When invoking a superclass version of an overridden method the super keyword is used.

PROGRAM 3: Demonstrate Method Overriding with super keyword

class Animal {
public void move() {
System.out.println("Animals can move");
}
}

class Dog extends Animal {


public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {


Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}

Course Instructor: Nazish Basir Page: 3/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

PROGRAM 4: Demonstrate Method Overriding with super keyword

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
}
}

PROGRAM 5: Demonstrate Method Overriding

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;
}
}

Course Instructor: Nazish Basir Page: 4/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

class Rectangle extends Figure {


Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}

class Triangle extends Figure {


Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}

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:

accessModifier abstract class ClassName

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:

accessModifier abstract return_type methodName (argument list);

Course Instructor: Nazish Basir Page: 5/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

RESTRICTION FOR DEFINING ABSTRACT CLASS AND METHODS

PROGRAM 6: Demonstrates Abstract Employee class

abstract class Employee {


private String name;
private String address;
private int number;

public Employee(String name, String address, int number) {


System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}

abstract public double computePay();

abstract public void mailCheck();

public String toString() {


return name + " " + address + " " + number;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public void setAddress(String newAddress) {


address = newAddress;
}

Course Instructor: Nazish Basir Page: 6/7


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 8

public int getNumber() {


return number;
}
}

class Salary extends Employee {


private double salary; // Annual salary

public Salary(String name, String address, int number, double salary) {


super(name, address, number);
setSalary(salary);
}

public void mailCheck() {


System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " +
salary);
}

public double getSalary() {


return salary;
}

public void setSalary(double newSalary) {


if(newSalary >= 0.0) {
salary = newSalary;
}
}

public double computePay() {


System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}

public class AbstractDemo {

public static void main(String [] args) {


Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}

Course Instructor: Nazish Basir Page: 7/7


Institute of Information and Communication Technology, University of Sindh.

You might also like