It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Doubt in Java Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Java Interface" Watch "Doubt in Java Interface" New topic
Author

Doubt in Java Interface

vijay makam
Ranch Hand

Joined: Sep 06, 2008
Posts: 31
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???


Regards,
vijay
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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

Joined: Oct 13, 2005
Posts: 39053
    
  23
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.
 
GeeCON Prague 2014
 
subject: Doubt in Java Interface