• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Error Quest for OO Inheritance  RSS feed

 
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.
 
author
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).
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!