Oop Lab 8 Practice Tasks
Oop Lab 8 Practice Tasks
Main Campus
Faculty of Computing
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().
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 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)
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");
}
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;
}
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.
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
class MethodOverloading {
// 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 {