• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Questions for you

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Questions for you (I made these up):

1) Which of the follow modifiers are legal for an interface method (circle all)?

A) public
B) private
C) synchronized
D) protected
E) none of the above

2) What is true about the following code?

A) It will not compile
B) The constructor will set x = 4
C) The overloaded method Doo will never execute with any call
D) The overridden method Doo will work normally
E) The overloaded method Doo will work normally
3)What is true about the following code?

A) It will not compile
B) Any object reference to a Sprinter can call the Race() method
C) Any object reference to a Slowpoke can call the Race() method
D) Any object reference to a TurboTurtle can call the Race() method
[ September 13, 2004: Message edited by: Tom Tolman ]
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer to question 1: only 'public' accessibility modifier is legal to method in interface.

answer to question 2: this code will fail to compile since void cannot be at constructor.

answer to question 3: this code will fail to compile since class TurboTurtle does not know which interface the Race() method it is referring to.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sura watthana:

answer to question 2: this code will fail to compile since void cannot be at constructor.

answer to question 3: this code will fail to compile since class TurboTurtle does not know which interface the Race() method it is referring to.



Let me take a try at it

A is wrong coz the code will compile.
B is wrong coz the only constructor that actually gets compiled is a default constructor which contains only public Doo() {super();} and knows nothing about any x.

D talks of an overridden method. There aren't any overridden methods either.

In order for a class to have an overridden method, it needs to have a superclass which contains a method with the same signature and return type as a method in the class. The only superclass to class Doo is Object. It has no method called void Doo(). There is only one method in the Doo class. Therefore it has no overridden methods.

C and E talk about overloaded methods, but there are no overloaded methods. In order to have an overloaded method, there needs to be a method in the class or in one of the superclasses that has a different signature. Object has no Doo method with any signature, and the Doo class has no additional Doo methods, therefore there are no overloaded methods.

My answer is there is no answer. As an aside this class will work normally but it has nothing to do with constructors, overloaded methods, and overridden methods.


Question 3: This will not compile. Too many closed } at the end.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom, for question 1 are you restricting the domain to only top-level interfaces? (Ahha! we are talking about the methods declared in the interface, so it doesn't matter if the interface is top-level or declared within a class)


The result of compiling and running question 3 is enlightening (after removing the extraneous "}" and adding a few extra statements to create the object references in the question)
[ September 15, 2004: Message edited by: Barry Gaunt ]
 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr. Louie van Bommel for telling me the correct answer.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi All,
I have few good inputs on the subject:

Q 1: only 'public' accessibility modifier is legal to method in interface.
Nav: AGREE

Q 2: this code will fail to compile since void cannot be at constructor.
Nav: NOT AGREE
DESC: if we put a return type in front of a contr..it becaome a method...and is no more a constr. In particular to our class 'Doo' "void Doo(){}" will now be consider as a method by the compiler. Also, it will implicitly insert a default constructor... now these two are an overloaded methos. dont u agree??? jus try the following steps... a)compile the class file, b) decompile it[u cn decompile it using >javap Doo]... so watch out.
[plz correct m if you thnk am wrong smwhr ]

Q 3: This will not compile. Too many closed } at the end.
Nav: AGREE

We as a programmer must dare to think like a compiler.. and thts the word for me.

Thanks
Navneet
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question - 3 will compile after removing the extra }. But it will not run. It will throw NoSuchMethodError.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nanda Kishore:
Question - 3 will compile after removing the extra }. But it will not run. It will throw NoSuchMethodError.



But give it a main method, create some instances of the class and call the method through the various reference types...
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
===========
But give it a main method, create some instances of the class and call the method through the various reference types...
===========

Tried it out and perfectly works fine! Answer for Q3 is thus B,C,D (considering the extra '}' is removed )

The code ...

====
interface Sprinter
{
public void Race();
}

interface Slowpoke
{
public void Race();
}

class TurboTurtle implements Sprinter, Slowpoke
{
public void Race()
{
System.out.println("Turbo charged");
}

public static void main(String args[])
{
Sprinter s1 = new TurboTurtle();
Slowpoke s2 = new TurboTurtle();
TurboTurtle t = new TurboTurtle();

s1.Race();
s2.Race();
t.Race();
}
}
===

The output ...
===
Turbo charged
Turbo charged
Turbo charged
===
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, finally, the answers are

1. A
2. E
3. BCD

Am I right???

Kaps
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kapil,
I feel it's
1> A
2> No Answer ( I agree with Louie's interpretation )
3> BCD
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not true that a class that implements two interfaces with methods with the same signature will not compile. The following compiles:



Another language's, such as C++ (which supports multiple inheritance), compiler would bark at a similar construct.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Louie.

methods and constructors both have signatures but they exist in separate namespaces so neither can overload or override the other.

There is no place in Java syntax where you can choose between using either a class name or a method name. This is good because the presence of a return type, the only way to distinguish constructors from methods, is not part of any signature. If two methods in a class have the same signature but different return types, that's a compile-time error. In this case, the Doo() method would have conflicted with the default Doo() constructor.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic