Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Today at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Today at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Yesterday at 5:34 PM
Pure VPN - Up to 27 Months
vgp
Updated:
Friday at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Jun 2, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
java help needed
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="sihina_lahiru" data-source="post: 7285050" data-attributes="member: 149577"><p><strong><span style="color: Red">3.</span></strong></p><p>Implementing an Interface</p><p> <p style="margin-left: 20px"> To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one. <strong>A Sample Interface, Relatable</strong></p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px">Consider an interface that defines how to compare the size of objects. <p style="margin-left: 20px">public interface Relatable {</p> <p style="margin-left: 20px"> </p> <p style="margin-left: 20px"> // this (object calling isLargerThan) and</p> <p style="margin-left: 20px"> // other must be instances of the same class</p> <p style="margin-left: 20px"> // returns 1, 0, -1 if this is greater</p> <p style="margin-left: 20px"> // than, equal to, or less than other</p> <p style="margin-left: 20px"> public int isLargerThan(Relatable other);</p> <p style="margin-left: 20px">}</p> <p style="margin-left: 20px"></p></p> <p style="margin-left: 20px">If you want to be able to compare the size of similar objects, no matter what they are, the class that instantiates them should implement Relatable. Any class can implement Relatable if there is some way to compare the relative "size" of objects instantiated from the class. For strings, it could be number of characters; for books, it could be number of pages; for students, it could be weight; and so forth. For planar geometric objects, area would be a good choice (see the RectanglePlus class that follows), while volume would work for three-dimensional geometric objects. All such classes can implement the isLargerThan() method. </p> <p style="margin-left: 20px">If you know that a class implements Relatable, then you know that you can compare the size of the objects instantiated from that class. </p> <p style="margin-left: 20px"><strong>Implementing the Relatable Interface</strong></p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px"> Here is the Rectangle class that was presented in the <a href="http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html" target="_blank">Creating Objects</a> section, rewritten to implement Relatable. <p style="margin-left: 20px">public class RectanglePlus implements Relatable {</p> <p style="margin-left: 20px"> public int width = 0;</p> <p style="margin-left: 20px"> public int height = 0;</p> <p style="margin-left: 20px"> public Point origin;</p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px"> // four constructors</p> <p style="margin-left: 20px"> public RectanglePlus() {</p> <p style="margin-left: 20px"> origin = new Point(0, 0);</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"> public RectanglePlus(Point p) {</p> <p style="margin-left: 20px"> origin = p;</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"> public RectanglePlus(int w, int h) {</p> <p style="margin-left: 20px"> origin = new Point(0, 0);</p> <p style="margin-left: 20px"> width = w;</p> <p style="margin-left: 20px"> height = h;</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"> public RectanglePlus(Point p, int w, int h) {</p> <p style="margin-left: 20px"> origin = p;</p> <p style="margin-left: 20px"> width = w;</p> <p style="margin-left: 20px"> height = h;</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px"> // a method for moving the rectangle</p> <p style="margin-left: 20px"> public void move(int x, int y) {</p> <p style="margin-left: 20px"> origin.x = x;</p> <p style="margin-left: 20px"> origin.y = y;</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px"> // a method for computing the area of the rectangle</p> <p style="margin-left: 20px"> public int getArea() {</p> <p style="margin-left: 20px"> return width * height;</p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px"> </p> <p style="margin-left: 20px"> // a method required to implement the Relatable interface</p> <p style="margin-left: 20px"> public int isLargerThan(Relatable other) {</p> <p style="margin-left: 20px"> <strong>RectanglePlus otherRect = (RectanglePlus)other;</strong></p> <p style="margin-left: 20px"> if (this.getArea() < otherRect.getArea())</p> <p style="margin-left: 20px"> return -1;</p> <p style="margin-left: 20px"> else if (this.getArea() > otherRect.getArea())</p> <p style="margin-left: 20px"> return 1;</p> <p style="margin-left: 20px"> else</p> <p style="margin-left: 20px"> return 0; </p> <p style="margin-left: 20px"> }</p> <p style="margin-left: 20px">}</p> <p style="margin-left: 20px"></p></p> <p style="margin-left: 20px">Because RectanglePlus implements Relatable, the size of any two RectanglePlus objects can be compared. <p style="margin-left: 20px"><strong>Note:</strong> The isLargerThan method, as defined in the Relatable interface, takes an object of type Relatable. The line of code, shown in bold in the previous example, casts other to a RectanglePlus instance. Type casting tells the compiler what the object really is. Invoking getArea directly on the other instance (other.getArea()) would fail to compile because the compiler does not understand that other is actually an instance of RectanglePlus. </p></p> <p style="margin-left: 20px"> </p></blockquote><p></p>
[QUOTE="sihina_lahiru, post: 7285050, member: 149577"] [B][COLOR=Red]3.[/COLOR][/B] Implementing an Interface [INDENT] To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one. [B]A Sample Interface, Relatable[/B] Consider an interface that defines how to compare the size of objects. [INDENT]public interface Relatable { // this (object calling isLargerThan) and // other must be instances of the same class // returns 1, 0, -1 if this is greater // than, equal to, or less than other public int isLargerThan(Relatable other); } [/INDENT] If you want to be able to compare the size of similar objects, no matter what they are, the class that instantiates them should implement Relatable. Any class can implement Relatable if there is some way to compare the relative "size" of objects instantiated from the class. For strings, it could be number of characters; for books, it could be number of pages; for students, it could be weight; and so forth. For planar geometric objects, area would be a good choice (see the RectanglePlus class that follows), while volume would work for three-dimensional geometric objects. All such classes can implement the isLargerThan() method. If you know that a class implements Relatable, then you know that you can compare the size of the objects instantiated from that class. [B]Implementing the Relatable Interface[/B] Here is the Rectangle class that was presented in the [URL="http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html"]Creating Objects[/URL] section, rewritten to implement Relatable. [INDENT]public class RectanglePlus implements Relatable { public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { origin = p; } public RectanglePlus(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public RectanglePlus(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int getArea() { return width * height; } // a method required to implement the Relatable interface public int isLargerThan(Relatable other) { [B]RectanglePlus otherRect = (RectanglePlus)other;[/B] if (this.getArea() < otherRect.getArea()) return -1; else if (this.getArea() > otherRect.getArea()) return 1; else return 0; } } [/INDENT] Because RectanglePlus implements Relatable, the size of any two RectanglePlus objects can be compared. [INDENT][B]Note:[/B] The isLargerThan method, as defined in the Relatable interface, takes an object of type Relatable. The line of code, shown in bold in the previous example, casts other to a RectanglePlus instance. Type casting tells the compiler what the object really is. Invoking getArea directly on the other instance (other.getArea()) would fail to compile because the compiler does not understand that other is actually an instance of RectanglePlus. [/INDENT] [/INDENT] [/QUOTE]
Insert quotes…
Verification
Hata thunen beduwama keeyada? (60 bedeema thuna)
Post reply
Top
Bottom