Introduction To Java Programming Language
105606Module 3: Methods of Java
1(d). Which keyword refers to the current object? (i) super (ii) current (iii) this (iv) self20262m
Module 3: Methods of Java
View this question on its own page →Which keyword refers to the current object?
(i) super
(ii) current
(iii) this
(iv) selfWorked SolutionAnswer: (iii)
thisthisrefers to the current object instance. It is often used to distinguish instance fields from parameters or to call another constructor usingthis(...).class Student { int id; Student(int id) { this.id = id; } }1(e). Runtime polymorphism in Java is primarily achieved through: (i) Method overriding (ii) Constructor overloading (iii) Method overloading (iv) Static variables20262m
Module 3: Methods of Java
View this question on its own page →Runtime polymorphism in Java is primarily achieved through:
(i) Method overriding
(ii) Constructor overloading
(iii) Method overloading
(iv) Static variablesWorked SolutionAnswer: (i) Method overriding
Runtime polymorphism occurs when a superclass reference refers to a subclass object and an overridden instance method is selected according to the object's actual type at runtime.
class Animal { void sound() { System.out.println("sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("bark"); } } Animal a = new Dog(); a.sound(); // bark5(b). Construct overloaded methods for calculating the area of a circle, rectangle and triangle. Analyse how Java selects the appropriate method based on the parameter list.20267m
Module 3: Methods of Java
View this question on its own page →Construct overloaded methods for calculating the area of a circle, rectangle and triangle. Analyse how Java selects the appropriate method based on the parameter list.
Worked SolutionMethod overloading for areas
Overloading means methods share a name but have different parameter lists. Java resolves the call at compile time using argument count and types.
class Area { static double area(double r) { return Math.PI * r * r; } static double area(double length, double width) { return length * width; } static double area(double base, double height, boolean triangle) { return 0.5 * base * height; } public static void main(String[] args) { System.out.println(area(2.0)); System.out.println(area(4.0, 3.0)); System.out.println(area(4.0, 5.0, true)); } }Return type alone cannot distinguish overloads. The extra boolean is used here to give the triangle method a distinct signature; a named method such as
triangleAreawould be clearer in production code.6(a). In Java, a subclass needs to invoke a constructor and method of its superclass. Construct a suitable program using super and analyse the order in which superclass and subclass constructors execute.20267m
Module 3: Methods of Java
View this question on its own page →In Java, a subclass needs to invoke a constructor and method of its superclass. Construct a suitable program using
superand analyse the order in which superclass and subclass constructors execute.Worked SolutionUsing
superand constructor ordersuper(...)invokes a superclass constructor and must be the first statement in a constructor.super.method()calls an accessible superclass method implementation.class Person { Person(String name) { System.out.println("Person: " + name); } void info() { System.out.println("Person info"); } } class Student extends Person { Student(String name) { super(name); System.out.println("Student created"); } @Override void info() { super.info(); System.out.println("Student info"); } } class Demo { public static void main(String[] args) { Student s = new Student("Asha"); s.info(); } }Output order:
Person: Asha,Student created, thenPerson info,Student info. The superclass constructor runs before the subclass constructor body.9(a). Write a short note on Runtime Polymorphism.20267m
Module 3: Methods of Java
View this question on its own page →Write a short note on Runtime Polymorphism.
Worked SolutionRuntime polymorphism
Runtime polymorphism allows a superclass reference to invoke a subclass's overridden instance method. The method implementation is chosen at runtime using dynamic method dispatch.
class Shape { void draw() { System.out.println("Shape"); } } class Circle extends Shape { @Override void draw() { System.out.println("Circle"); } } Shape s = new Circle(); s.draw(); // CircleBenefits include extensibility, loose coupling and writing code against abstractions. Static, private and final methods are not overridden in the normal runtime-dispatch sense.
9(c). Write a short note on Method Overriding.20267m
Module 3: Methods of Java
View this question on its own page →Write a short note on Method Overriding.
Worked SolutionMethod overriding
Overriding occurs when a subclass provides a compatible implementation of an inherited instance method. The method signature must match; the return type must be the same or covariant, access cannot be more restrictive, and checked exceptions cannot be broadened beyond the parent's declaration. Use
@Overrideto catch mistakes.class Animal { void speak() { System.out.println("..."); } } class Cat extends Animal { @Override void speak() { System.out.println("Meow"); } }Overriding supports runtime polymorphism. It differs from overloading, where methods share a name but have different parameter lists.