Suppose I have a class Person which implements several interfaces. My person class is a Dad and a doctor and likes
to play football on weekends.
class Person implements DadInterface, DoctorProfessionalInterface, FootballInterface {
}
In my program I set up 3 objects like these:
FootballInterface person2 = new Person();
DadInterface dad1 = new Person();
Person person3 = new Person();
What is the difference between person2, dad1, and person3? Don't they have all the same properties and
methods as any Person()?
Feb 26, 2009 10:49:55 PM, [email protected] wrote:
Tanya,An interface is a way to abstract. Interface is more a design than an implementation. You can use Interfaces to handle different typed objects. In the case below it is true that type declaring as a class works but is not recommened. I think this is because the class Person implements the PersonInterface. For someone to look at and maintain the code they might think that "pc" was an abstract class and not an interface. So simply declaring an object "pi" of type PersonInterface and object class Person makes sense.It makes more sense once you implment more than one interface or multiple interfaces. So think of Person implementing multiple interfaces and the type PersonInterface declaration makes more sense. In general it seems to me that if there are multiple interfaces for a class to inherit, that is when we use interface and not just class. I also read somewhere that if you create an abstract class with all the methods abstract, that is really an interface.Keith
From: Tanya Dina Ruttenberg <[email protected]>
To: [email protected]
Sent: Thursday, February 26, 2009 2:48:29 PM
Subject: [java programming] Interfaces lesson - question
In http://www.javapassion.com/javase/javainterface.pdf page 21 it says
Interfaces and classes are both types
– This means that an interface can be used in places
where a class can be used
– For example:
// Recommended practice <<----
PersonInterface pi = new Person(); <<----
// Not recommended practice <<----
Person pc = new Person(); <<----
Why would it be the recommended practice to type a new object by its interface name?
Tanya
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---
