# Inheritance problem

Mandy Thompson
Greenhorn
Posts: 6
I am having some issues learning inheritance. I have stumbled across this example in my book but it has got me floundered. I am not expecting a resolution to this but maybe a little push in the right direction.

I need to write an inheritance hierarchy for classes quadrilateral, trapezoid, parallelogram, rectangle and square. Quadrilateral bieng the superclass of the heirarchy. Make the hierarchy as deep as possible. Specify the instance variables and methods for each class. The private instance variable of Quadrilateral should be the x-y coordinate pairs for the four ended points fo the Quadrilateral. I then need to write a program that instantiates objects of my classes and outputs each objects area. I am so lost right now so any lead into the right direction would be awesome. Thanks!!

Nigel Browne
Ranch Hand
Posts: 703
Post what you have done so far or try to ask a more specific question. What exactly are you stuck on?
Inheritance is the ability to reuse a superclass's variables and methods. The heirachy needs to reflect the 'is-a' relationship. For example a trapezoid 'is a' quadrilateral. A quadrilateral is a four-sided polygon and a trapezoid is a specialised quadrilateral because of its two parallel sides.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Start with the simplest thing you can think of. As an example, see if you can make this run:

Now we can see if you're stuck on creating classes, writing constructors, or something else before we even think about the inheritance tree.

BTW: You have some guidance in your assignment that is good for a school exercise but bad for real systems. We'll get to that before we're done, too!

Daniel .J.Hyslop
Ranch Hand
Posts: 55
hiya mandy,
I have a program here that may help to understand inheretence a little more .The property class is abstract and is the super class.notice the public void door() method ,it is declared in property but not in the two subclasses,this is becuase it is inhereted from property,so in a main method if you were to say
Shed s = new Shed();
s.doors();
This would work becuase method doors is inhereted from property and does not need to be specified in shed.
bungalow IS A property .shed IS A property. each method in bungalow and shed overrides the methods in property and implements them .notice that bumgalow implements interface brickwork you can use interfaces to implement uncommon methods between subclasses the same applys to shed and it`s wooden interface.I realise this is a million miles away from your problem but looking at it from a different perspective may help and I hope it does.Heres the program[code]
abstract class Property
{
public void doors()
{
input = JOptionPane.showInputDialog("Please enter doors in shed");
noOfDoors=input;
}

public abstract void windows();
public abstract void rooms();
}

class Bungalow extends Property implements BrickWork
{
private String noOfDoors;
private String noOfWindows;
private String noOfRooms;

private String input;
private int qty;
private int sqMeters;

public Bungalow()
{
this.input=input;
this.qty=qty;
this.sqMeters=sqMeters;
}

public void windows()
{
input = JOptionPane.showInputDialog("Please enter Windows in Bungalow");
noOfWindows=input;
}

public void rooms()
{
input = JOptionPane.showInputDialog("Please enter Rooms in bungalow");
noOfRooms=input;
}

public void noOfBricks(int sqMeters)
{
this.sqMeters=sqMeters;
qty=0;

if(sqMeters<=5)
{
qty=lowNoOfBricks;
}
else if(sqMeters<=10)
{
qty=medNoOfBricks;
}
else qty=highNoOfBricks;
}
}
class Shed extends Property implements Wooden
{
private String noOfDoors;
private String noOfWindows;
private String noOfRooms;

private String input;
private int qty;
private int sqMeters;

public Shed()
{
this.input=input;
this.qty=qty;
this.sqMeters=sqMeters;
}

public void windows()
{
input = JOptionPane.showInputDialog("Please enter windows in shed");
noOfWindows=input;
}

public void rooms()
{
input = JOptionPane.showInputDialog("Please enter rooms in shed");
noOfRooms=input;
}

public void woodQty(int sqMeters)
{
this.sqMeters=sqMeters;
qty=0;

if(sqMeters<=2)
{
qty=lowQtyWood;
}
else if(sqMeters<=6)
{
qty=medQtyWood;
}
else

qty=highQtyWood;
}
}

interface Wooden
{
static final int lowQtyWood=7;
static final int medQtyWood=20;
static final int highQtyWood=30;

public void woodQty(int sqMeters);
}

interface BrickWork
{
static final int lowNoOfBricks=1000;
static final int medNoOfBricks=2000;
static final int highNoOfBricks=3000;

public void noOfBricks(int sqMeters);
}[\code]
by the way for obvious reasons this code wont run as there is no main method .hope it helps though

Mandy Thompson
Greenhorn
Posts: 6
Dont you think it would be easier to develop the hierarchy tree, realizing the attributes and assigning the instance variables and methods and then start the coding once we can see a layout???

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Constructors that call showInputDialog? Member variables that are self-assigned in the constructor (Bungalow?) String variables used to hold numbers? A member variable (input) used and reused to hold temporary data? Constants with lower-case names?

Oy, my eyes!

Mandy, don't imitate that program! It's a laundry-list of The Wrong Things To Do!

Mandy Thompson
Greenhorn
Posts: 6
Thanks;-)

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791

Dont you think it would be easier to develop the hierarchy tree, realizing the attributes and assigning the instance variables and methods and then start the coding once we can see a layout???

Traditionally, a lot of people have been more comfortable doing a detailed design before they code, so I understand and respect your question.

Recently more and more people are more comfortable with running code, even if it's tiny and incomplete and we know it will change to some degree. (We like to think we are the cool ones.) This doesn't mean "zero design" but it does mean "just barely enough" design at any moment.

It's kind of a gamble ... do you get more benefit from detailed design before you ever try any of your ideas in code, or from experimenting in code and letting the design evolve? It definitely depends on your experience and skills and work practices.

Why not give the early, simple running code approach a try this time. We can work through the evolutionary part together.

Mandy Thompson
Greenhorn
Posts: 6
In this case, I have to develop a hierarchy and then base my code from that. Since I am new to inheritance, i feel that in this situation, seeing the design prior to starting the coding would benefit me more. I can then take that design and implement it a lot easier.