• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

which are the correct answer

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one help us really confuse
1. class A {
2. protected int method1(int a, int b) { return 0; }
3. }
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; }
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
As of iam thinking,
ans should be A & C, reason is :

In overridden method you can not assign weaker privileges.

As of in option A. access privilege is public that is perfectly valid.
in option B. access privilege is private which is weaker then the
protected hence invalid.
in option C. access privilege is private but the signature of method
is different from the signature of method in class A so
this is the case of overloading not overriding hence it
is valid.
in option D. For overriding return type of overridden method should
be same as the return type of overiding method , in this
option return type is short which is different from the
return type in the super class(A) hence this option is
invalid.
in option E. static method cannot override the instance method hence
invalid


cheers!
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Amit is correct. Just in addition to his/her post.

D doesn't compile because in the end of return statement is expected a semicolon (
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lalit mishra:
Can any one help us really confuse
1. class A {
2. protected int method1(int a, int b) { return 0; }
3. }
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; }





B - is wrong because it is narrowing the scope of the method..
c - is also wrong because it is narrowing the scope of the method..and there is mismatch in the type of argument...
d - is having a different return type..

Therefore A and E are the correct answers.....
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Mithun,

Amit is right. The correct answers are A and C.

Not B because you are reducing the scope of the method.

C is right because you not overriding but overloading, so private is fine. The overloaded method canot be further overridden in any subclasses of subclass of A.

E is incorrect because you cannot make the overridding method static (You can only make it static if the parent class' method was also static but that's hiding and not overriding).

regards,
vijay.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is correct. It's not overridden, it's overloaded.
 
mithun gooty
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you vijay...
thats correct its overloading not overiding..........
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
Can any one help us really confuse
1. class A {
2. protected int method1(int a, int b) { return 0; }
3. }
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; }


As per my point of view the answer is only (A)

B and C are assigning weaker privelleges to the protected method.
D is having different return type. The compiler won't allow this.
E is having static refrence.( it overloads static base class methods )
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


B and C are assigning weaker privelleges to the protected method.



True for B but C isn't an override method but an overload and the access modifier private is possible
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the rules on overriding and overloading. This was taken from Sierra and Bates SCJP book.

Overriding:

The argument list must exactly matchthat of the overridden method.
? The return type must exactly matchthat of the overridden method.
? The accesslevelmustnotbemorerestrictivethanthatoftheoverriddenmethod.
? The access level can be less restrictivethan that of the overridden method.
? The overriding method must not throw new or broader checked exceptions
than those declared by the overridden method. For example, a method that
declaresaFileNotFoundExceptioncannotbeoverriddenbyamethod
thatdeclaresaSQLException, Exception, oranyothernon-runtime
exception unless it�s a subclass of FileNotFoundException.
? The overriding method can throw narrower or fewer exceptions. Just because
an overridden method �takes risks� doesn�t mean that the overriding subclass�
exception takes the same risks. Bottom line: An overriding method doesn�t
have to declare any exceptions that it will never throw, regardless of what
the overridden method declares.
? You cannot override a method markedfinal.
? If a method can�t be inherited, you cannot override it. For example, the
following code is not legal:


Overloading:

? Overloaded methods must change the argument list.
? Overloaded methods can change the return type.
? Overloaded methods can change the access modifier.
? Overloaded methods can declare new or broader checked exceptions.
? A methodcanbe overloaded in the same class or in a subclass.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic