Solved

Doubt about "implements" (easy)

Posted on 2004-03-28
14
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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