Avatar of rafamvc
rafamvc asked on

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
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

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/
twobitadder

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.
twobitadder

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
CEHJ

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
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.
ASKER
rafamvc

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
twobitadder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
rafamvc

tnks a lot!
twobitadder

glad I could help.
ASKER
rafamvc

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
rafamvc

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.
CEHJ

>>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.
     }
}
ASKER
rafamvc

tnks again!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

8-)