Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Java Interface

 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface Adobarable{
void method1();
}

abstract class Myclass {
void method1();
}

class MyclassDemo extends Myclass implements Adorable{

public void method1(){
System.out.println("Which method's implementation is this???");
}
}
Which method's(interface's or abstract class's) implementation is provided in the MyclassDemo. If I am assigning a weaker access specifier for the mehtod compiler blurts out. That means the method implementation is for the Interface.

But in the below code how it works
interface Adorable{
void method1();
}

abstract class Myclass {
public void method1(){}
}

class MyclassDemo extends Myclass implements Adorable{

}
what can we say for this???
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure , but the above code may works as ,

when you extends a base class , you are inheriting its methods ,right , so when you extends MyClass into MyclassDemo , the complier sees something like this ,


and this method is perfectly implements your interface 'Adorable' ..

So my inputs - its implements interface method !

If I`m wrong , Ranchers please correct me !
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijay makam:
Which method's(interface's or abstract class's) implementation is provided in the MyclassDemo.

As far as the compiler is concerned that is a meaningless question. As long as there is an implementation which matches the interface it is happy.
When I was young people would buy 3-wheeled cars because they could drive them a year younger than 4-wheeled cars. They could pass a driving test on a motorbike, which (in Britain) entitled one to drive a three-wheeled car, again younger than a 4-wheeled car. Later we could sit a driving test in a 4-wheeled car, which also allowed one to drive a 3-wheeled car.
Year later, you buy a 3-wheeled car; are you allowed to drive it because you passed the test on a motorbike, or because you passed the test in a 4-wheeled car?

If I am assigning a weaker access specifier for the mehtod compiler blurts out. That means the method implementation is for the Interface.
An implementation of a method, or overriding, is a "refinement;" that means that whenever the superclass method works, the overridden method works too. If one of them is public, then you cannot override it as private or protected or default access. The interface methods are implicitly public, so they must be implemented as public.
Don't call it "weaker;" it is actually more restrictive, a "strong" restriction.

Otherwise that's like passing a driving test and being told you cannot drive on motorways.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic