Hi Tanya
Let me help you with something…
The difference is in the way the Person is used, for example you
can define a Person like this
Person John = new Person();
And if you like this Person to Play FootBall, then you can
create a function like this:
void playFootball(FootballInterface x)
{
… /* Implement playing football */
}
Then you can call the function directly as: playFootball(John)
This is possible because John is a Person which implements the
FootballInterface
Or you can do the same with:
FootballInterface JohnyPlayer = new Person();
playFootball(JohnyPlayer);
CONCLUSION: If you declare an instance of Person, then it can be
“used” easily with functions that requires this kind of interface.
Hope it helps
Patricio
Thanks Keith. But I still don't really get it.
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:
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.
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
__________ Information from ESET Smart Security, version of virus signature
database 3894 (20090227) __________
The message was checked by ESET Smart Security.
http://www.eset.com
__________ Information from ESET Smart Security, version of virus signature
database 3894 (20090227) __________
The message was checked by ESET Smart Security.
http://www.eset.com