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: 7285054" data-attributes="member: 149577"><p><strong><span style="color: Red">4.</span></strong></p><p>Using an Interface as a Type</p><p> <p style="margin-left: 20px"> When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it <em>must</em> be an instance of a class that implements the interface. As an example, here is a method for finding the largest object in a pair of objects, for <em>any</em> objects that are instantiated from a class that implements Relatable: </p><p style="margin-left: 20px">public Object findLargest(Object object1, Object object2) {</p> <p style="margin-left: 20px"> Relatable obj1 = (Relatable)object1;</p> <p style="margin-left: 20px"> Relatable obj2 = (Relatable)object2;</p> <p style="margin-left: 20px"> if ( (obj1).isLargerThan(obj2) > 0)</p> <p style="margin-left: 20px"> return object1;</p> <p style="margin-left: 20px"> else </p> <p style="margin-left: 20px"> return object2;</p> <p style="margin-left: 20px">}</p> <p style="margin-left: 20px"></p></p> <p style="margin-left: 20px">By casting object1 to a Relatable type, it can invoke the isLargerThan method. If you make a point of implementing Relatable in a wide variety of classes, the objects instantiated from <em>any</em> of those classes can be compared with the findLargest() method—provided that both objects are of the same class. Similarly, they can all be compared with the following methods: </p><p style="margin-left: 20px">public Object findSmallest(Object object1, Object object2) {</p> <p style="margin-left: 20px"> Relatable obj1 = (Relatable)object1;</p> <p style="margin-left: 20px"> Relatable obj2 = (Relatable)object2;</p> <p style="margin-left: 20px"> if ( (obj1).isLargerThan(obj2) < 0)</p> <p style="margin-left: 20px"> return object1;</p> <p style="margin-left: 20px"> else </p> <p style="margin-left: 20px"> return object2;</p> <p style="margin-left: 20px">}</p> <p style="margin-left: 20px"></p> <p style="margin-left: 20px">public boolean isEqual(Object object1, Object object2) {</p> <p style="margin-left: 20px"> Relatable obj1 = (Relatable)object1;</p> <p style="margin-left: 20px"> Relatable obj2 = (Relatable)object2;</p> <p style="margin-left: 20px"> if ( (obj1).isLargerThan(obj2) == 0)</p> <p style="margin-left: 20px"> return true;</p> <p style="margin-left: 20px"> else </p> <p style="margin-left: 20px"> return false;</p> <p style="margin-left: 20px">}</p> <p style="margin-left: 20px"></p></p> <p style="margin-left: 20px">These methods work for any "relatable" objects, no matter what their class inheritance is. When they implement Relatable, they can be of both their own class (or superclass) type and a Relatable type. This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface. </p></blockquote><p></p>
[QUOTE="sihina_lahiru, post: 7285054, member: 149577"] [B][COLOR=Red]4.[/COLOR][/B] Using an Interface as a Type [INDENT] When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it [I]must[/I] be an instance of a class that implements the interface. As an example, here is a method for finding the largest object in a pair of objects, for [I]any[/I] objects that are instantiated from a class that implements Relatable: [INDENT]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; } [/INDENT] By casting object1 to a Relatable type, it can invoke the isLargerThan method. If you make a point of implementing Relatable in a wide variety of classes, the objects instantiated from [I]any[/I] of those classes can be compared with the findLargest() method—provided that both objects are of the same class. Similarly, they can all be compared with the following methods: [INDENT]public Object findSmallest(Object object1, Object object2) { Relatable obj1 = (Relatable)object1; Relatable obj2 = (Relatable)object2; if ( (obj1).isLargerThan(obj2) < 0) return object1; else return object2; } public boolean isEqual(Object object1, Object object2) { Relatable obj1 = (Relatable)object1; Relatable obj2 = (Relatable)object2; if ( (obj1).isLargerThan(obj2) == 0) return true; else return false; } [/INDENT] These methods work for any "relatable" objects, no matter what their class inheritance is. When they implement Relatable, they can be of both their own class (or superclass) type and a Relatable type. This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface. [/INDENT] [/QUOTE]
Insert quotes…
Verification
Winadiyakata thappara keeyak tibeda?
Post reply
Top
Bottom