Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Doubt about "implements" (easy)

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
rafamvc
Asked:
rafamvc
  • 5
  • 5
  • 4
1 Solution
 
CEHJCommented:
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
 
twobitadderCommented:
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
 
twobitadderCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
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
 
twobitadderCommented:
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
 
rafamvcAuthor Commented:
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
 
twobitadderCommented:
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
 
rafamvcAuthor Commented:
tnks a lot!
0
 
twobitadderCommented:
glad I could help.
0
 
rafamvcAuthor Commented:
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
 
rafamvcAuthor Commented:
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
 
CEHJCommented:
>>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
 
rafamvcAuthor Commented:
tnks again!
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now