Jump to content

Thinking inheritance

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
public class ShoeMakers{

	Customer[] cust = new Customer[2];


	/*[SIZE="3"]Necessary constructor and method calls[/SIZE],

	 *[SIZE="3"]cust[0..x] are initialised with values[/SIZE]

	 *[SIZE="3"]Here we can make calls like:[/SIZE]

	 *id = cust[i].getID;*/

}  

public class Customers extends ShoeMakers{

	private int id;

	String name;

	

	Customer(int i, String nam){

		id = i;

		name = nam;

		}   	

	/*[SIZE="3"]Necessary accessor and mutator methods e.g. getID and changeID;*/  	

		

	/*Im thinking since the cust[0.. x] has valid data, and Customer 

	 *inherets the data members from ShoeMaker, can i access the fields

         *id and name through cust[x].getWhatever from this class?[/SIZE] */     

}

If the answer to that question is no. Can I access the fields of cust with:
cust[0..x].getWhatever
from a class with a slightly different relationship to Customer, say,

public class Collect extends ShoeMakers{

}

without transfering the cust array to the collect class?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
I think your model could do with some modification.

A shoemaker has customers as you have correctly done with a customer array in ShoeMaker. But, a customer isnt't a shoemaker is he? Thats what your saying by extending ShoeMaker in Customer. Remove the extension.

With this new model a shoemaker will know all about its customers, but a customer won't know anything about its shoemaker. Which is what you want. It's called loose coupling, classes should know the least amount possible about eachother.

Back to the point, basically, what your saying in your post will work, but it isn't a very good idea.

To make your ShoeMake navigable through its Customers (and hence gain its visible methods) don't extend ShoeMaker, do something like this instead:



public class ShoeMaker{


 private Customer[] myCustomers = new Customer[2];


 public ShoeMaker(/*blah blah*/){

   //more blah...

 }


 public Customer getCustomer(int c){

    return myCustomer2[c];

 }

}

public class Customer{


 private ShoeMaker myShoeMaker = null;


 public Customer(/**blah blah*/{

    //blah

 }


 public someMethod(){

  int x = 1;

  //initialise myShoeMaker (possibly from constructor?)

  myShoeMaker.getCustomer(x);


  //do whatever you want to do

 }

}


If you don't understand any part let me know.

HTH

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Customer is out of line. I thought of that while i was typing .Bad example, that should be something like. Fish and goldfish.
In any case, the question i wanted answered(we ignore the poor choice of words), if the array of customers are initialised in shoemaker can customer have direct access through inheritance, to the array, as seen in my 'slightly confused' model
I did something similar for an assignment(cant post code), i had direct access as in cust[x].getwhatever from the customer class but the
data values were all null. It was like i had a whole new instance of the array with uninitialised values.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
JerrySpock

JerrySpock

    Newbie

  • Members
  • Pip
  • 9 posts
If there was a method inside Customer called getWhatever, I think what you're asking should work. In your example Customers (different than Customer?) extends ShoeMakers so it will inherit all the members of that class. When you call the constructor for Customers it will implicitly called the constructor for ShoeMakers. So Customers is a ShoeMaker in your example, and will be able to access the Customer[] object inside ShoeMaker.

#5
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts

fread said:

I did something similar for an assignment(cant post code), i had direct access as in cust[x].getwhatever from the customer class but the
data values were all null. It was like i had a whole new instance of the array with uninitialised values.

Stupid question here, you did already assign values of the array in this instance, right?

extending ShoeMaker doesnt give you a reference to an already created ShoeMaker object. It gives you a whole new ShoeMaker object.

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Stu_328 said:

Stupid question here, you did already assign values of the array in this instance, right?

extending ShoeMaker doesnt give you a reference to an already created ShoeMaker object. It gives you a whole new ShoeMaker object.

That stupid question stu_328 was the source of my confusion. Much thanks, i think i get the idea now.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused: