Results 1 to 5 of 5
  1. #1
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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.

  2. #2
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default 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;}
    }

  3. #3
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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() {
        }
    }
    How would i go about setting the grade and resistance? Resistance can be either YES or NO, not a set result, it is determind based on user input. And also for grade, if can be 3 value of 1,2,3.

    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.

  4. #4
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default 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.

  5. #5
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Abstract Classes and ArrayLists.
    By Hooky75 in forum New To Java
    Replies: 9
    Last Post: 05-14-2011, 10:38 AM
  2. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 03:30 PM
  3. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 09:31 AM
  4. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 09:22 PM
  5. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •