Solved

Doubt about "implements" (easy)

Posted on 2004-03-28
14
209 Views
Last Modified: 2010-03-31
Hi, as u can see i´m not a java master ;)
I have a class that is called "client" and i need to that class be(like extends) one case of two other classes, called "Person" or "Company".
I cant make Person and Company to inherit Client, because i use Person and Company i other classes, i think the correct is to Client implements Person and Company,
but I dont know what is the effect to make my Person and Company Class a interface. I dont realise what is the diference of inherit or implements, only the fact that i can implements more than one class.
Can some1 help me?
Tnks.
Rafael Cardoso
0
Comment
Question by:rafamvc
  • 5
  • 5
  • 4
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10698670
On the face of it, i'd say that what you probably need to do is

class Person {}
class Client extends Person {}
interface ICompany {}
class ConcreteClient extends Client implements ICompany {}

See

http://mindprod.com/jgloss/interface.html

http://mindprod.com/jgloss/inheritance.html

http://java.sun.com/docs/books/tutorial/java/concepts/
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10698936
In inheritance: a class gains the (non-private) attributes and operations of the class it extends. eg.
Manx extends Cat extends Mammal:

Class Mammal
  ^
  | extends (meaning inherits from)
  |
Class Cat
  ^
  |
  |  extends
Class Manx

Cat inherits all of the attributes and methods of Mammal, and Manx inherits all the operations and methods of Cat (including those from Mammal)

Interfaces:
  Provide a means of noting that a class is guaranteed to have implemented certain operations which haven't been inherited ie. it's a way to provide additional properties that would be impossible without multiple inheritence.


For your problem:

Cehj: making a ConcreteClient an extension of Person- to me it doesn't seem to fit the inheritance pattern, I don't think ConcreteClient is a kind of person.

If I was modelling it I would do this:

abstract class client{}

class Person{}
class Company{}

class PersonalClient extends Person implements Client{}
class CompanyClient extends Company implements Client{}

where Client lists methods that are required to make a class into a client.
This means that the PersonalClient and CompanyClient will be extended so that they can fit the criteria of a client.

Hope this is useful.
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10698942
To be accurate I should have:

>> it's a way to provide additional properties that would be impossible without multiple inheritence.

as:
 it's a way to provide additional methods that would be impossible without multiple inheritence.
                                              ---------
since interfaces only deal with methods.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10699013
It's difficult to tell exactly how the entities should be modelled without knowing more about them, so my initial comments were based on a certain amount of guesswork, as indeed yours must also be, twobitadder
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10699057
Yes, I'm not trying to say there's one right way, as the whole field of systems analysis and design hammers into you, there is no single solution and many people will develop different yet correct solutions.
  That is just my take on that small design problem,  but I was looking at it from the point of view of developing from scratch rather than developing around an existing implementation, which may not be suitable for Rafael if he has constraints to work within.
0
 

Author Comment

by:rafamvc
ID: 10700810
but my doubt  now is:
what i cant use when i implements a class?
Lets make it pratical:
Supose that:

Person
Privates Atributes: ID, Name, Address, Password, Login.
public methods:
gets and sets, authenticatePassword(login, Password).

Company
Privates Atributes: BrazilianCompanyRegisterNumber, Name, Address, Contact (Person).
Public Methods: Gets and Sets.

Client
Private Atributes: MyClientNumber, Last10Purchases(arrayList of purchases).
Gets And Sets, getLastPurchases(numberofPurchases)

If i inherit Person and Company to ConcretePerson and ConcreteCompany, i know that i will use normaly these methods. (the attributes are always private)
But when i impements Client, what i cant use now? What is the diference between implements and inherit in this case?

Tnks a Lot.
0
 
LVL 5

Accepted Solution

by:
twobitadder earned 50 total points
ID: 10702993
With implementing Client you would provide an interface that lists all the methods that can be called on a client and the implementing classes would either provide these already or add methods to provide them.

eg.
The classes PersonalClient and CompanyClient would implement this:

public interface Client
{
  int getClientNumber();
  int setClientNumber();
  ArrayList getLastPurchases(numberOfPurchases);
  other required gets/sets
}

On implementing these methods you would add the attributes required to the PersonalClient and CompanyClient that are required to fulfill the method contract (they are no longer simple Person or Company classes).

The problem with implementing the interface is that you cannot guarantee the implementation holds 10 purchases, unless you do a kind of crude hack and add into the interface:
ArrayList getLast10Purchases();

The problem with the inheritence solution is that ConcreteClient inherits properties of person which could include date of birth, marital status, national insurance number etc.


The key difference is that with 'implements' you specify a list of methods that must be provided to satisfy the interface Client.

Note that the concrete client design also has an interface ICompany, it's a problem of multiple inheritance and the only way to get around it in java is to use interfaces/abstract classes.

You could check:
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
http://java.sun.com/docs/books/tutorial/java/interpack/interfaces.html
http://www.ibiblio.org/javafaq/course/week4/44.html
http://www-h.eng.cam.ac.uk/help/tpl/languages/java/cuedjavanotes/topics.html#Interfaces


http://java.sun.com/docs/books/tutorial/java/concepts/interface.htmlk/
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rafamvc
ID: 10703211
tnks a lot!
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10705921
glad I could help.
0
 

Author Comment

by:rafamvc
ID: 10712724
i understand now that interfaces doesnt fit my case ;(
couse i need to make a Array of Clients and cant use PersonClient and CompanyClient in same array, like if it is inherity....
but is ok... now seraching for solution.
0
 

Author Comment

by:rafamvc
ID: 10712725
i understand now that interfaces doesnt fit my case ;(
couse i need to make a Array of Clients and cant use PersonClient and CompanyClient in same array, like if it is inherity....
but is ok... now seraching for solution.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10714435
>>i need to make a Array of Clients and cant use PersonClient and CompanyClient in same array

That wouldn't prevent you accomplishing that in fact:

Client[] clients = new Client[2];
clients[0] = new CompanyClient();
clients[1] = new PersonClient();


... on the 'other end'

for (int i = 0; i < clients.length; i++) {
     if (clients[i] instanceof PersonClient) {
           //etc.
     }
}
0
 

Author Comment

by:rafamvc
ID: 10714482
tnks again!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10714505
8-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now