Introduction To Java Programming Language

105606
Back to Introduction To Java Programming Language

Module 3: Methods of Java

  1. 1(d). Which keyword refers to the current object? (i) super (ii) current (iii) this (iv) self20262m

    Module 3: Methods of Java

    Which keyword refers to the current object?

    (i) super
    (ii) current
    (iii) this
    (iv) self

    View this question on its own page →
    Worked Solution

    Answer: (iii) this

    this refers to the current object instance. It is often used to distinguish instance fields from parameters or to call another constructor using this(...).

    class Student {
        int id;
        Student(int id) { this.id = id; }
    }
    
  2. 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

    Runtime polymorphism in Java is primarily achieved through:

    (i) Method overriding
    (ii) Constructor overloading
    (iii) Method overloading
    (iv) Static variables

    View this question on its own page →
    Worked Solution

    Answer: (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(); // bark
    
  3. 5(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

    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.

    View this question on its own page →
    Worked Solution

    Method 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 triangleArea would be clearer in production code.

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

    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.

    View this question on its own page →
    Worked Solution

    Using super and constructor order

    super(...) 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, then Person info, Student info. The superclass constructor runs before the subclass constructor body.

  5. 9(a). Write a short note on Runtime Polymorphism.20267m

    Module 3: Methods of Java

    Write a short note on Runtime Polymorphism.

    View this question on its own page →
    Worked Solution

    Runtime 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(); // Circle
    

    Benefits include extensibility, loose coupling and writing code against abstractions. Static, private and final methods are not overridden in the normal runtime-dispatch sense.

  6. 9(c). Write a short note on Method Overriding.20267m

    Module 3: Methods of Java

    Write a short note on Method Overriding.

    View this question on its own page →
    Worked Solution

    Method 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 @Override to 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.