Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class One { public void methodOne(int i) { } public void methodTwo(int i)

ID: 3773619 • Letter: P

Question

public class One {

public void methodOne(int i) {

}

public void methodTwo(int i){

}

public static void methodThree(int i){

}

public static void methodFour(int i){

}

}

public class Two extends One {

public satic void methodOne(int i) {

}

public void methodTwo(int i){

}

public void methodThree(int i){

}

public static void methodFour(int i){

}

}

One of the methods above overrides a method in the superclass. In a one- to two-page (250- to 500-word) paper, address the following issues: What are superclass and subclass? What is hiding and overriding? Which method is hidden and overriden in the code above? How or why? Describe when overriding and hiding a superclass makes a program more efficient or elegant.

Explanation / Answer

According to above code super class is One and sub class is Two because sub class is inherited using extends keyword according to inheritance concept

Syntax: Subclass extends superclass so

Subclass=Two

Superclass=One

methodTwo is overriding method

methodFour is hiding method

Two class(sub class ) overrides methodTwo in class one (super class)and hides static method methodFour

Method hiding: If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

Method overriding: An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

The difference between hiding a static method and overriding an instance method is :

overridden instance method (methodTwo,methodOne)that gets invoked is the one in the subclass.

The version of the hidden static method(methodFour) that gets invoked depends on whether it is invoked from the superclass or the subclass.

Two.java

class One {

     public void methodOne(int i) {

}

public void methodTwo(int i){

  

}

public static void methodThree(int i){

}

public static void methodFour(int i){

System.out.println("methodFour is method hiding in super class");

}

}

public class Two extends One {

//public static void methodOne(int i) {

//

//}

public void methodTwo(int i){

System.out.println("methodTwo in sub class ");

}

//public void methodThree(int i){

//}

public static void methodFour(int i){

    System.out.println("sub class method method four");

}

public static void main(String args[])

{

    Two t=new Two();

    One o =t;

    o.methodTwo(7);//methodTwo in one class(super class) overrides two class sub classs method methodTwo

    One.methodFour(6);//since static method as acced by class name it will access method in super class one

    t.methodFour(8);//it will access methos in sub class

}

}

output

methodTwo in sub class

methodFour is method hiding in super class

sub class method method four