![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
Java: Interfaces
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
An interface may seem like a useless invention, it simply defines what methods are included in a class, but that's not all it does, and not entirley acurate. An interface includes methods that are similar between different objects, perhaps you have a Person object and a Dog object, to display a field within them you would have to typecast it to the specific class, this causes problems when many classes are involved, suppose you added a Cat object now you have 3 seperate cases, just to -for example- print the name. Interfaces allow you to treat objects the same based on similar information. The interface will also force you to include all methods defined within it in every class that implements it. I have written the example below including a seperate class for testing, as to prove that it does not affect the interface ability to deicern which object it is dealing with. CODE Example: main.java public class main{ public static void main(String args[]){ main m = new main(); Person p = new Person("William",21); Cat c = new Cat("Mr. Whiskers", 4); Dog d = new Dog("Max",18); //show all objects created properly System.out.println(p + "\n" + c + "\n" + d + "\n"); //show all objects are treated the same as defined as lfi m.printName(p); m.printName(c); m.printName(d); } public void printName(LivingThingInterface lfi){ System.out.println(lfi.getName()); } } Cat.java public class Cat implements LivingThingInterface{ String name; int age; Cat(String n, int a){ name = n; age = a; } public String getName() { return name; } public String toString(){ return name + "," + age; } public boolean hasFur() { return true; } } Dog.java public class Dog implements LivingThingInterface{ String name; int age; Dog(String n, int a){ name = n; age = a; } public String getName() { return name; } public String toString(){ return name + "," + age; } public boolean hasFur() { return true; } } Person.java public class Person implements LivingThingInterface{ String name; int age; Person(String n, int a){ name = n; age = a; } public String getName() { return name; } public String toString(){ return name + "," + age; } public boolean hasJob() { return false; } } LivingThingInterface.java public interface LivingThingInterface{ public String getName(); public String toString(); } *NOTE that methods not listed in the interface can be written and used in any of the objects, but they must accessed by that class only, and are not universal. |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java JDBC Tutorials - Topics include Java, Database, JDBC, Driver, ODBC and more! | Anilrgowda | Graphic tutorials | 0 | 04-Sep-2007 01:02 AM |
| From PSD to HTML - How to turn your Photoshop designs into web interfaces | Anilrgowda | Graphic tutorials | 0 | 23-Jul-2007 10:13 AM |
| The Javalog.txt file is created in the Windows\Java folder when Java logging is enabl | Anilrgowda | Microsoft windows vista error | 0 | 29-Jan-2007 10:09 AM |
| Interfaces in Java | Anilrgowda | Programming tutorials | 0 | 03-Jan-2007 10:29 PM |
| Microsoft presents draft of security interfaces | Anilrgowda | Microsoft windows vista error | 0 | 21-Dec-2006 08:05 AM |