0% found this document useful (0 votes)
52 views8 pages

Oop Lab 8 Practice Tasks

Uploaded by

Tube 360
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)
52 views8 pages

Oop Lab 8 Practice Tasks

Uploaded by

Tube 360
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/ 8

Riphah International University I-14

Main Campus
Faculty of Computing

Class Fall-2022 Subject OOP


Course Code CS2104 Class/Lab Instructor Saad Wazir

-------------------- LAB 08 --------------------


Learning Objective:
1. Polymorphism

a) Compile-time (Static) Binding, Overloading


b) Runtime (Dynamic) Binding, Overriding

Practice Task # 1 - Runtime (Dynamic) Binding, Method Overriding


if the same method is defined in both the superclass and the subclass, then the method of
the subclass class overrides the method of the superclass. This is known as method
overriding.
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}

class Dog extends Animal {


@Override
public void displayInfo() {
System.out.println("I am a dog.");
}
}

class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Notice the use of the @Override annotation in our example. In Java, annotations are the
metadata that we used to provide information to the compiler. Here, the @Override
annotation specifies the compiler that the method after this annotation overrides the
method of the superclass.

It is not mandatory to use @Override. However, when we use this, the method should follow
all the rules of overriding. Otherwise, the compiler will generate an error.
Java Overriding Rules:
 Both the superclass and the subclass must have the same method name, the same
return type and the same parameter list.
 We cannot override the method declared as final and static.
 We should always override abstract methods of the superclass

It is important to note that constructors in Java are not inherited. Hence, there is no such
thing as constructor overriding in Java.

However, we can call the constructor of the superclass from its subclasses. For that, we use
super().

Practice Task # 2 - Access Specifiers in Method Overriding


The same method declared in the superclass and its subclasses can have different access
specifiers. However, there is a restriction.

We can only use those access specifiers in subclasses that provide larger access than the
access specifier of the superclass. For example,

Suppose, a method myClass() in the superclass is declared protected. Then, the same method
myClass() in the subclass can be either public or protected, but not private.
class Animal {
protected void displayInfo() {
System.out.println("I am an animal.");
}
}

class Dog extends Animal {


public void displayInfo() {
System.out.println("I am a dog.");
}
}

class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
In the above example, the subclass Dog overrides the method displayInfo() of the superclass
Animal.

Whenever we call displayInfo() using the d1 (object of the subclass), the method inside the
subclass is called.

Notice that, the displayInfo() is declared protected in the Animal superclass. The same
method has the public access specifier in the Dog subclass. This is possible because the public
provides larger access than the protected.
class a {
public void displayInfo(int a) {
System.out.println("I am A." + a);
}
}

class b extends a {
@Override
public void displayInfo(int a) {
//super.displayInfo(20);
System.out.println("I am B." + a);
}
}

class main {
public static void main(String[] args) {
b d1 = new b();
d1.displayInfo(10);
}
}
Practice Task # 3 - Dynamic Method Dispatch (Runtime Polymorphism)

Dynamic method dispatch is the mechanism by which a call to an overridden method is


resolved at run time, rather than compile time.

When an overridden method is called through a superclass reference, Java determines which
version(superclass/subclasses) of that method is to be executed based upon the type of the
object being referred to at the time the call occurs. Thus, this determination is made at run
time.
At run-time, it depends on the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be executed
A superclass reference variable can refer to a subclass object. This is also known as upcasting.
Java uses this fact to resolve calls to overridden methods at run time.

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

public void speak(){


System.out.println(" .. ");
}
}

class Dog extends Animal{


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

public class main {


public static void main(String[] args) {
Animal a = new Animal();
Animal b = new Dog();

a.move();
b.move();
//b.dog_speak();
b.speak();
a.speak();
}
}
In Java, we can override methods only, not the variables(data members), so runtime
polymorphism cannot be achieved by data members.
class A
{
int x = 10;
}

class B extends A
{
int x = 20;
}

public class Test


{
public static void main(String args[])
{
A a = new B(); // object of type B

// Data member of class A will be accessed


System.out.println(a.x);
}
}
In above program, both the class A(super class) and B(sub class) have a common variable ‘x’.
Now we make object of class B, referred by ‘a’ which is of type of class A. Since variables are
not overridden, so the statement “a.x” will always refer to data member of super class.
Practice Task # 4 - Compile-time (Static) Binding, Method Overloading
In Java, two or more methods may have the same name if they differ in parameters (different
number of parameters, different types of parameters, or both). These methods are called
overloaded methods and this feature is called method overloading.

void func() { ... }


void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }

Here, the func() method is overloaded. These methods have the same name but accept
different arguments.

Note: The return types of the above methods are not the same. It is because method
overloading is not associated with return types. Overloaded methods may have the same or
different return types, but they must differ in parameters.

// 1. Overloading by changing the number of parameters

class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}

private static void display(int a, int b){


System.out.println("Arguments: " + a + " and " + b);
}

public static void main(String[] args) {


display(1);
display(1, 4);
}
}
// 2. Method Overloading by changing the data type of parameters

class MethodOverloading {

// this method accepts int


private static void display(int a){
System.out.println("Got Integer data.");
}
// this method accepts String object
private static void display(String a){
System.out.println("Got String object.");
}

public static void main(String[] args) {


display(1);
display("Hello");
}
}

// Here, both overloaded methods accept one argument. However, one accepts the
argument of type int whereas other accepts String object.

Practice Task # 5 -
class HelperService {

private String formatNumber(int value) {


return String.format("%d", value);
}

private String formatNumber(double value) {


return String.format("%.3f", value);
}

private String formatNumber(String value) {


return String.format("%.2f", Double.parseDouble(value));
}

public static void main(String[] args) {


HelperService hs = new HelperService();
System.out.println(hs.formatNumber(500));
System.out.println(hs.formatNumber(89.9934));
System.out.println(hs.formatNumber("550"));
}
}
Note: In Java, you can also overload constructors in a similar way like methods.
Two or more methods can have the same name inside the same class if they accept different
arguments. This feature is known as method overloading.
Method overloading is achieved by either:
 changing the number of arguments.
 or changing the data type of arguments.
It is not method overloading if we only change the return type of methods. There must be
differences in the number of parameters

You might also like