• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Overriding

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

For the following code:


First question:
I thought it would output zac but it gives compile time error cause of method lilly() that isnt overriding lilly() in Base properly. Isnt lilly() suppopsed to be considered a new method instead of overriding the one in the Base class?

second question(not related to this though):
Whats the difference between a local and an anonymous class?

third question:
In the real exam do they tell you the NUMBER of correct answers to choose for a question which has mulitiple answers?

Got some more things to ask but I guess this is enough for now

Cheers!!!
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the base class, the return type of lilly is void. In the derived class, the return type of lilly is int. You can't do that.

Local classes can be given reference names, where an anonymous inner class isn't given a reference name.
 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith for your reply.

Though i realize the difference in the return types of the 2 methods but doesnt overriding mean that name, return type, argument type and number of parameters passed should be same in both the methods and in this case the return type is different, so that would mean lilly() in the child class is a brand new method.
Am I correct?

Does anyone the answer to the 3rd question?
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question:


I thought it would output zac but it gives compile time error cause of method lilly() that isnt overriding lilly() in Base properly. Isnt lilly() suppopsed to be considered a new method instead of overriding the one in the Base class?



A method signature is not determined by the return type. Thus, the compiler thinks you are trying to override this method (since the rest of the method signature is identical to the one in the base class). In order to override it, the method signature must be identical (i.e. return void, not int). You could alternatively, change the method signature in the derived class by adding some input parameters (i.e. overloading the method) and it would distinguish the methods properly.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For valid override, return type must match and since you didnt change the parameters (or arguments) this is not a overload either. That's why the compiler is complaining.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concerning the third question, I've heared that the exam says the exact number of correct questions.


Concerning the first question, just in addition to all other answers please take note for some key rules about overridding and overloading methods:

Rules for overriding methods

1. The argument list must exactly match that of the overridden method.
2.The access level CAN'T be more restrictive than the overridden method.
3.The access level CAN be less restrictive than that of the overridden method.
4.The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.
5.The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method.

Rules for overloading methods
1.Overloaded methods MUST change the argument list.
2.Overloaded methods CAN change the return type.
3.Overloaded methods CAN change the access modifier.
4.Overloaded methods CAN declare new or broader checked exceptions.
5.A method can be overloaded in the same class or in a subclass.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overrinding method must and should return valu be the same
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

overriding methods must be same arguments but signature will be same type or subtype of the original type.

Ex:

class Base {
public Object lilly() {
return null;
}
}

public class Along extends Base {
public static void main(String argv[]) {
new Along().lilly();
}

public Integer lilly() {
new Zac();
return 1;
}

private class Zac {
Zac() {
System.out.println("zac");
}
}
}

The above rules applicable for only Tiger version....

Ravi
 
Sant
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overrinding method must and should return valu be the same
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic