We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Doubt about "implements" (easy)

rafamvc
rafamvc asked
on
Medium Priority
262 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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
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/
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.
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.
CERTIFIED EXPERT
Top Expert 2016

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

Author

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.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
tnks a lot!
glad I could help.

Author

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.

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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.
     }
}

Author

Commented:
tnks again!
CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.