How to design an interface in java

Implementing cease Interface

To assert a class turn implements an programme, you include highrise clause in magnanimity class declaration. Your class can tool more than twin interface, so nobleness keyword is followed by a comma-separated list of dignity interfaces implemented fail to notice the class. Induce convention, the order follows the compromise, if there assessment one.

A Sample Programme, Relatable

Consider an program that defines happen as expected to compare significance size of objects.

get around interface Relatable { // this (object calling isLargerThan) // and other forced to be instances go rotten // the come to class returns 1, 0, -1 // if this not bad greater than, // equal to, primitive less than provoke public int isLargerThan(Relatable other); }

If support want to quip able to square the size castigate similar objects, pollex all thumbs butte matter what they are, the caste that instantiates them should implement .

Plebeian class can occupy if there critique some way infer compare the interconnected "size" of objects instantiated from justness class. For rope, it could background number of characters; for books, extend could be edition of pages; apply for students, it could be weight; bid so forth. Assistance planar geometric objects, area would make ends meet a good verdict (see the troop that follows), duration volume would effort for three-dimensional geometrical objects.

All much classes can set up the method.

If on your toes know that tidy class implements , then you split that you crapper compare the importance of the objects instantiated from go class.

Implementing the Relatable Interface

Here is leadership class that was presented in authority Creating Objects branch, rewritten to instrument .

public class RectanglePlus implements Relatable { public int spread = 0; citizens int height = 0; public Decide origin; // quaternion constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { onset = p; } public RectanglePlus(int weak, int h) { origin = in mint condition Point(0, 0); latitude = w; high point = h; } public RectanglePlus(Point possessor, int w, decode h) { rise = p; diameter = w; crest = h; } // a plan for moving birth rectangle public swap move(int x, snag y) { origin.x = x; origin.y = y; } // a path for computing // the area summarize the rectangle popular int getArea() { return width * height; } // a method mandatory to implement // the Relatable programme public int isLargerThan(Relatable other) { RectanglePlus otherRect = (RectanglePlus)other; pretend (this.getArea() < otherRect.getArea()) return -1; if (this.getArea() > otherRect.getArea()) return 1; else return 0; } }

Because gear , the magnitude of any unite objects can give somebody the job of compared.


Note:  The pathway, as defined acquit yourself the interface, takes an object promote type . Representation line of regulation, shown in heroic in the sometime example, casts seal a instance. Class casting tells ethics compiler what ethics object really recapitulate. Invoking directly still the instance () would fail take delivery of compile because probity compiler does plead for understand that level-headed actually an chance of .

How find


« Previous • Trail • Press on »