Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error Quest for OO Inheritance  RSS feed

 
edirg559
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

class A {
protected int method1(int a, int b) { return 0; }
}

Which two are valid in a class that extends class A? (Choose two)
A. public int method1(int a, int b) { return 0; }
B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0: }
E. static protected int method1(int a, int b) { return 0; }

According to my answer sheet, it claims: A & C.

My opinion:
A, yes, because it has the same signature as the class A method1. But, C, I disagree! The method1 takes 2 int arguments, how could we override it with one int and one long argument?

Am I wrong or right? Should the answer A & B instead?

Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right in that C isn't overriding - it's overloading, which is allowed, though.

B isn't valid, because a subclass is not allowed to reduce the visibility of a method (that would violate Liskov's Substitution Principle).
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how could we override it with one int and one long argument


Since the argument types are also part of method siganture, C has a different method signature compared to superclass method. So it is no longer an overridden method, but is an overloaded method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!