Results 1 to 5 of 5
Thread: Abstract classes
- 11-21-2011, 08:23 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Abstract classes
Am needing to use abstract methods for my project.
I have a pipe class and then 5 pipes. so 'public class Type1 extends Pipe' so on so forth. However i am unsure of the structure of the main Pipe class and also how to extend the class. Google hasnt seemed to have helped.
Java Code:public abstract class Pipe { boolean InnerInsulation, OuterReinforcement, ChemicalResistance; int Colour, Grade; public abstract void setReinforcement(); public abstract void setInsulation(); public abstract void setResistance(); public abstract void setColour(); public void choosePipe (boolean InnerInsulation, boolean OuterReinforcement, boolean ChemicalResistance, int Colour, double Length, double Radius) { } }
Java Code:public class Type1 extends Pipe { }
This is my code so far. My variables are the things that change dependant on the pipe type. Public void is to select the correct pipe type based on the variables.
If someone could please show how to write the initial abstract class and how to implement, would be much appreciated.
Thanks.Last edited by FadedAura; 11-21-2011 at 08:50 PM.
- 11-21-2011, 09:07 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Re: Abstract classes
I am not sure I understand it well, but I will try to answer your question by a bit of theory about java. You normally use an abstract class to be sure that you only use the methods of this abstract class. Thanks to polymorphisme you can extends this abstract class and override the method you need. If you want that all the methods has to been overrided, you do an interface (instead of an abstract class) and you implements this interface (instead of extends the abstract class).
I think your abstract class isn't complete at the moment. First normally you put arguments to the set-methods. For example setColour(int colour). In the class Type1 you override this method with this code:
Java Code:public class Type1 extends Pipe{ int colour; public void setColour(int colour){ this.colour=colour;} }
- 11-21-2011, 09:14 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Abstract classes
How comes the colour variable is set as a parameter?
This is my code so far:
Java Code:public class Type1 extends Pipe { boolean InnerInsulation, OuterReinforcement, ChemicalResistance; int Colour, Grade; @Override public boolean setReinforcement() { OuterReinforcement = false; return OuterReinforcement; } @Override public boolean setInsulation() { InnerInsulation = false; return InnerInsulation; } @Override public boolean setResistance() { } @Override public int setColour() { Colour = 0; return Colour; } @Override public int setGrade() { } }
Colour is 0,1,2 as that is the number of colours the user can have on the pipe.
Thanks.Last edited by FadedAura; 11-21-2011 at 09:24 PM.
- 11-21-2011, 10:51 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Re: Abstract classes
How would you put OuterReinforcement to true if you don't have a parameter in the method? In you code you only can put OuterReinforcement to false. So if you take a method setReinforcement(boolean OuterReinforcement) you can call the method to put it to true or to false.
- 11-21-2011, 11:16 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Abstract Classes and ArrayLists.
By Hooky75 in forum New To JavaReplies: 9Last Post: 05-14-2011, 10:38 AM -
Abstract Classes.
By maknib in forum New To JavaReplies: 3Last Post: 05-12-2011, 03:30 PM -
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 09:31 AM -
interface vs abstract classes
By rosh72851 in forum New To JavaReplies: 7Last Post: 11-16-2008, 09:22 PM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 10:53 AM
Bookmarks