Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[May 17, 2024, 05:02:16 PM]

[May 17, 2024, 05:02:16 PM]

[May 17, 2024, 05:02:16 PM]

[May 17, 2024, 05:02:16 PM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » JAVA
 Java: Interfaces
Pages: [1]   Go Down
  Print  
Author Topic: Java: Interfaces  (Read 3347 times)
Khushi
Global Moderator
Adv. Member
*****



Karma: 4
Offline Offline

Posts: 329

Hello!!


View Profile
Java: Interfaces
« Posted: January 03, 2007, 12:01:25 AM »


The how and why of interfaces

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

Code:
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

Code:
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

Code:
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

Code:
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

Code:
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.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.078 seconds with 25 queries.