• No results found

Registration started Contact 9742013378 or 9742024066 and lock your project at the earliest Dynamic Method Dispatch

In document Java 7th Sem Ashokkumar (Page 63-68)

Concept:

Here we will focus on method overriding in bit detail. We know that a base class reference can be given to a derived class object. The simple question I am going to answer in this topic is what if we call an overridden method on this object?

Example: class Animal {

public void eat() {

System.out.println("EATING THRICE A DAY"); }

// Overridden method

public void sleep() {

System.out.println("SLEEPING FOR 8 HOURS"); }

}

class Dog extends Animal {

// Overriding method

public void sleep() {

System.out.println("DOG SLEEPING FOR 4 HOURS"); }

public void bark() {

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

}

class DomesticDog extends Dog { public void sleep() {

System.out.println("DOG SLEEPING FOR 6 HOURS"); }

}

class WildDog extends Dog { public void sleep() {

System.out.println("DOG SLEEPING FOR 2 HOURS"); }

}

public class Example {

public static void main(String [] args) {

Animal animal1 = new Animal(); Animal animal2 = new Dog();

Animal animal3 = new DomesticDog();

animal1.sleep(); // calls Animal's sleep()

animal2.sleep(); // calls Dog's sleep()

animal3.sleep(); // calls DomesticDog's sleep()

} }

Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.

Output

We can conclude from the above program, it is 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.

Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through superclass reference variable, different versions of the method are executed. Since this decision should be made base on the type of the object, the JVM can make this decision only at runtime since the object will not be available at the compile time.

Definition:

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.

Note:

If you want to invoke an overridden method (base class method) from the overriding method (derived class method), you should use 'super' keyword as illustrated in below example.

class DomesticDog extends Dog { public void sleep() {

super.sleep(); // calls Dog' sleep()

System.out.println("DOG SLEEPING FOR 6 HOURS"); }

}

Remember, it’s not mandatory that your class should override the base class method. It is completely dependent on your implementation. Two questions for you at this point of time.

1. How will you enforce a restriction that a particular method in base class should never be overridden in the derived class? 2. How will you enforce a restriction that a particular method in base class should mandatorily be overridden in the derived

class?

Yes, Java provides a feature to enforce such restrictions. Following couple of topics deals with them.

2.2.3 "final" Keyword in Java

You can use final keyword in java for three different uses.  Variables can be made final

 Methods can be made final  Class can be made final

'final' variables

Concept and Definition

Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.

in a class then it must assigned a value in a class constructor. Attempting to change the value of final variable/field will generate error.

Syntax:

final <datatype> <var_name>; Example

class Animal { int age = 8;

final int legs = 4; // RIGHT: final variables can be assigned a value here

final int tails; A () {

tails = 1; // RIGHT: final variable can be assigned a value in constructor

}

void increment() {

age = age + 10;

legs = legs + 1; // ERROR: Can't modify the final variable

tails = tails + 1; // ERROR: Can't modify the final variable

} }

'final' methods: To prevent Overriding

Concept and Definition

If you want to enforce a restriction like, a particular method in the base class should never be overridden in the derived class, simply prefix that method definition with the 'final' keyword.

A final method cannot be overridden by sub class. Example:

class Animal {

// This is a final method

final public void eat() {

System.out.println("EATING THRICE A DAY"); }

public void sleep() {

System.out.println("SLEEPING FOR 8 HOURS"); }

}

class Dog extends Animal {

// WRONG: Cannot override a final method

public void eat() {

System.out.println("EATING TWICE A DAY"); }

// RIGHT

public void sleep() {

System.out.println("SLEEPING FOR 4 HOURS"); }

}

'final' classes: To prevent Inheritance

Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.

Unless a class definition has been prefixed with 'final', any other class can override it. There is no restriction on that. Java provides a feature where you can restrict a class from being inherited. This can be done by prefixing a class definition with 'final' keyword.

A class declared final cannot be sub classed. Other classes cannot extend final class. It provides some benefit to security and thread safety.

Example

class Animal { }

// RIGHT

final class Dog extends Animal { }

// WRONG: Cannot subclass the final class Dog

class DomesticDog extends Dog { }

2.2.4 Abstract classes in Java

Concept

There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.

To put it differently, you may encounter some situations where you need to enforce a restriction that some functions in the base class has to mandatorily overridden by the derived class. If you want to enforce this restriction on any method in base class, simply prefix its declaration with 'abstract' keyword. Remember, you should not give the definition for abstract functions in the base class because mandatorily it will be defined in derived classes.

If a class contains one or more abstract methods, it's like a class containing one or more methods without any definition (just the declaration), so it's obvious that the class is not complete until any other class inherits it and gives the definition for these methods. The conclusion is you cannot create a direct objects of the abstract class, all you can do is having its reference to the object of the derived class.

Definitions

Abstract method

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon) like this: abstract void fun(int a);

Abstract class

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. If a class includes abstract methods, the class itself must be declared abstract, as in:

class A {

abstract void fun(int a); }

Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract

Example:

// abstract class

abstract class Animal { void sleep() {

System.out.println("Sleeping for 8 hours"); }

abstract void sound(); // abstract function

}

class Dog extends Animal { public void sound() {

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

}

class Cat extends Animal { public void sound() {

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

}

public class Example {

public static void main(String [] args) {

Animal animal1 = new Animal(); // WRONG: Abstract class cannot be initiated

Animal animal2 = new Dog(); // RIGHT

Animal animal3 = new Cat(); // RIGHT

} }

Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.

In document Java 7th Sem Ashokkumar (Page 63-68)