cannot understand interface in java -


i looking @ interface chapter provided on java website
using interface type
understanding whole point of interface is class it's not possible form objects it, page says how use interface data type. line relatable obj1 = (relatable)object1; seems create object of type relatable interface. although must new keyword has not been used here, not creating reference object of type relatable. cause line not creating object of type relatable?

again, further says

if make point of implementing relatable in wide variety of classes, objects instantiated of classes can compared findlargest() method—provided both objects of same class.

what mean? mean implements relatable can call findlargest()? if it's so, why provided both objects of same class?

----- edit -----
previous chapters of tutorial:
definition of relatable:

public interface relatable {      // (object calling islargerthan)     // , other must instances of      // same class returns 1, 0, -1      // if greater // than, equal      // to, or less other     public int islargerthan(relatable other); }   

using relatable type:

public object findlargest(object object1, object object2) {    relatable obj1 = (relatable)object1;    relatable obj2 = (relatable)object2;    if ((obj1).islargerthan(obj2) > 0)       return object1;    else        return object2; } 

----- edit 2 -----
in chapter on anonymous classes, this:

public class helloworldanonymousclasses {      interface helloworld {         public void greet();         public void greetsomeone(string someone);     } . . .  helloworld englishgreeting = new englishgreeting();          helloworld frenchgreeting = new helloworld() {             string name = "tout le monde";             public void greet() {                 greetsomeone("tout le monde");             }             public void greetsomeone(string someone) {                 name = someone;                 system.out.println("salut " + name);             }         }; 

so how work?

the line relatable obj1 = (relatable)object1; seems create object of type relatable

no. line creates reference (obj1) of type relatable , assigns object1. in order work, object1 has cast (interface) type relatable.
no new objects being created here.

does mean implements relatable can call findlargest()?

yes.

if it's so, why provided both objects of same class?

it has implementation of islargerthan(). since class implementing relatable interface can't know other classes implementing it, can't meaningful comparisons other classes. therefore, in order work, both objects need of same class.

response edit 2

so how work?

instead of first defining class , creating instance of it, in case englishgreeting, frenchgreeting created on fly. happens under cover new class implementing helloworld created, in english case, time anonymous (you never give name). convenience shortcut times when need one-time implementation of interface.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -