Java class cascading

Posted: May 2, 2014 in web

public abstract class Parent
-unit
-fee

public class ChildOne extends Parent
-deposit
-startDate

public class ChildTwo extend Parent
-title
-name

ChildOne childOne = new ChildOne();
childone.setUnit(2);
childone.setFee(2);
childone.setDeposit(2);

To access Parents properties on parent on ChildTwo.

Option 1.

public someMethod(ChildOne childOne){
	Parent parent = (Parent)childOne;
	ChildTwo childTwo = (ChildTwo)parent;
	
	childTwo.unit // outputs 2

}

Option 2.Still send ChildOne to someMethod but change the parameter to accept Parent instead of childOne.

public someMethod(Parent parent){
	ChildTwo childTwo = (ChildTwo)parent;
	childTwo.unit // outputs 2

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s