Question

In: Computer Science

Q. Explain how to invoke a superclass method from a subclass method for the case in...

Q. Explain how to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method and the case in which the subclass method does not override a superclass method. [1 Mark]

this is a correct answer but i want answer it  in different way   :

  • In the case where the subclass method overrides the superclass method, it is necessary to explicitly use the keyword super, to invoke the method of the superclass as super.methodName().

    When the subclass method does not override the superclass method, the superclass method can be invoked simply by using its name and appropriate arguments.

Solutions

Expert Solution

ANSWER:

Case 1: When a method of super class is overridden by a method of the sub class

To call a method of a superclass from a subclass when the method being called is overridden by a method of the subclass, super keyword is used. For example:-

class SuperClass {
    SuperClass() {}

    public void foo(){
        System.out.println("In super class's foo");
    }
}

class SubClass extends SuperClass {
    SubClass() {}

    //Overridden method of superclass
    @Override
    public void foo(){
        System.out.println("In sub class's foo");
    }

    //Method of sub class which calls foo() method of super class
    public void bar() {
        super.foo();
    }
}

//Main class
public class Main {
    public static void main(String[] args) {
        SubClass obj3 = new SubClass();
        obj3.bar();
    }
}

In this example, the method bar() of SubClass calls the method foo() of SuperClass which is overridden in SubClass. In this case, there is no other way of calling the foo() method of SuperClass other than using super keyword.

Case 2: When a method of super class is not overridden by a method of the sub class

In this case, to call a method of the superclass we can simply use the name, with proper parameters, of the method of the superclass to call the method. For example:-

class SuperClass {
    SuperClass() {}

    public void foo(){
        System.out.println("In super class's foo");
    }
}

class SubClass extends SuperClass {
    SubClass() {}

    //Method of sub class which calls foo() method of super class
    public void bar() {
        foo();
    }
}

//Main class
public class Main {
    public static void main(String[] args) {
        SubClass obj3 = new SubClass();
        obj3.bar();
    }
}

In this example, the foo() method of SuperClass has not been overridden in the SubClass. Therefore, we can simply call the foo() method of the SuperClass by using its name.


Related Solutions

Apply inheritance to write a superclass and subclass to compute the triangle area and the surface...
Apply inheritance to write a superclass and subclass to compute the triangle area and the surface area of the triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both superclass and subclass so they will return the data of a triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at least two objects...
Could you expound in database modeling, how subclass and superclass are used? Maybe you can use...
Could you expound in database modeling, how subclass and superclass are used? Maybe you can use these terms(cardinality constraint , total participation constraint, weak entity type, partial key, IS-A relationship, specialization and generalization, specific (or local) attribute, specific relationship, aggregation)
Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a...
Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a call o.a(), will never get into an infinite loop.
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
Q. EXPLAIN COLLEY METHOD AND GIVE EXAMPLE?
Q. EXPLAIN COLLEY METHOD AND GIVE EXAMPLE?
Q. In the worst-case scenario related to casing collapse pressure, explain from a practical point of...
Q. In the worst-case scenario related to casing collapse pressure, explain from a practical point of view why the casing internal pressure is considered partially or completely empty. In other words, under what conditions the casing string can be empty or partially empty. (Note: This question needs to be answered in the context of Petroleum Drilling Engineering advance level)
Explain the question in each method Q: Many businesses are struggling in the midst of the...
Explain the question in each method Q: Many businesses are struggling in the midst of the pandemic, and face termination or filing for bankruptcy. Can you imagine a scenario, based on the information above, that could be used to save and/or grow a business through any of the methods taking into consideration the view of the present financial situation Which method will be best in this pandemic situation? Methods • Merger with another business • A consolidation with another business...
Explain how to record losses from uncollectible accounts using the direct write-off method. Explain how to...
Explain how to record losses from uncollectible accounts using the direct write-off method. Explain how to record losses from uncollectible accounts using the allowance method based on the percentage-of-sales method. Explain how to record losses from uncollectible accounts using the allowance method based on the percentage-of-receivables method. Explain how to record losses from uncollectible accounts using the allowance method based on aging-of-receivables method. Explain how to record the recovery of an account previously written off when the direct write-off method...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden.​​​​​​ Source code below: public abstract class Employee10A {    private String firstName, lastName; static int counter = 0;    public Employee10A(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }    @Override public String toString() { return ("The employee's full name is " + firstName...
C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that...
C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that each instance of the class relies on a random permutation of letters for its mapping.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT