• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

abstract class implementing interface method

 
Greenhorn
Posts: 5
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranch

I'm preparing for OCP-JP SE6 Exam, and I have a question regarding an abstract class that implements an interface method, which is mentioned in Q.22 of Practice Exam 3.

Given:
interface Horse { public void nicker(); }

Which will compile? (Choose all that apply.)
A. public class Eyra implements Horse { public void nicker() { } }
B. public class Eyra implements Horse { public void nicker(int x) { } }
C. public class Eyra implements Horse { public void nicker() { System.out.println("huhuhuhuh..."); } }
D. public abstract class Eyra implements Horse { public void nicker(int loud) { } }
E. public abstract class Eyra implements Horse { public void nicker(int loud); }

The answer is A, C and D.

I understand why A and C will compile and B and E won't, but I don't understand why D compiles. It looks the same as B except that D is an abstract class.
Isn't it an illegal overloading without implementing first? Or is it a new method which happens to have the same name as the interface method?

Thank you in advance!
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi May,

A abstract class that implements an interface, does not need to provide methods corresponding to methods defined in the interface.
The implementing class can leave the job of providing method implementations to it's first concrete descendent.

For example, this will compile:






Regards
Richard
 
May Kusakabe
Greenhorn
Posts: 5
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rechard,

Thank you very much for the quick response! You are exactly right, but I'm still confusing with the following situations...



Would anyone please explain why #2 compiles while #1 don't? #2 seems to have reduced the visibility AND overloaded without implementing MyInterface's doStuff().
 
Richard G Hayward
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

May Kusakabe wrote:

Would anyone please explain why #2 compiles while #1 don't? #2 seems to have reduced the visibility AND overloaded without implementing MyInterface's doStuff().



#2 doesn't reduce the visibility of MyInterface's doStuff.
Its a different method. The fact that it has an int parameter, which doStuff() in MyInterface did not, means that this is an overload.
Because MyAbstractClass2 is abstract, it's not required to provide an implementation of the no-parameter doStuff().


Richard
 
May Kusakabe
Greenhorn
Posts: 5
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard G Hayward wrote:
#2 doesn't reduce the visibility of MyInterface's doStuff.
Its a different method. The fact that it has an int parameter, which doStuff() in MyInterface did not, means that this is an overload.
Because MyAbstractClass2 is abstract, it's not required to provide an implementation of the no-parameter doStuff().
Richard




That totally makes sense:) Thank you very much for your help, Richard!

May

 
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

please tell me how the visibility is reduced here?
Thank you
 
May Kusakabe
Greenhorn
Posts: 5
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gajendra,

Visibility here refers to the methods' access levels: private < default < protected < public.
And it is not legal to reduce the visibility when overriding super class methods or providing implementations of interface methods.

Since interface methods are always "public" and "abstract" whether marked so or not, MyAbstractClass1's doStuff() must be marked public.
If not, the doStuff() is trying to have a default access level, which is narrower than public, which is illegal.

I hope this would help.

May
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

May Kusakabe wrote:
Visibility here refers to the methods' access levels: private < default < protected < public.



class A
{
protected void call()
{
System.out.println("This is from A");
}
}

class B extends A
{
public void call()
{
System.out.println("This is from B");
}
}

In the above example in class B we are overriding method call() with the access specifier which is less restrictive than protected so the overriding is accepted. Similarly if we give public as access specifier we have to give either public or the access specifier which is less restrictive than public.As there is no access specifier which is less restrictive than public we have to give public itself.

As the default access specifier in Interface is public,while overriding you have to give public explicitly or else it will take access specifier as default which is more restrictive than public.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand why D is correct and E not. Both look like right overloadking method. Thanks
 
Ranch Hand
Posts: 1738
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


D. public abstract class Eyra implements Horse { public void nicker(int loud) { } }
E. public abstract class Eyra implements Horse { public void nicker(int loud); }



The nicker is a method defined in D. This class provides a body to nicker method in Horse.
But the nicker does not have a body in E. Eyra defines an abstract method , but this method does not mark "abstract". That is why it does not compile.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!