Java Need Help with Writing a Subclass in Java: Am I Doing It Right?

  • Thread starter Thread starter courtrigrad
  • Start date Start date
AI Thread Summary
The discussion revolves around the creation of a subclass called ManMade that extends a superclass named Thing. The user is attempting to implement two constructors and three instance variables for the ManMade class. Key points include the need to properly initialize the instance variable 'name' in the no-argument constructor, as it currently lacks an assignment, which would lead to a compilation error. Additionally, there is confusion regarding the superclass constructor that accepts two arguments, which is not defined in the Thing class. The user is encouraged to clarify their objectives and ensure that the constructors and instance variables are correctly implemented and initialized.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

I need help in writing a subclass (ManMade) that has 2 constructors and 3 isntance variables. Here is my code so far:

Code:
public class Thing
{
   
    
        
        private double length;        // instance variables (fields)
        private double height;
        private double width;
        private boolean isPurpleFox;
        private double age;
        private static boolean exists;
    
    //*******************************//
    
        public Thing()
        {
            length = 7;
            height = 8;
            isPurpleFox = false;
            age = 0;
            exists = true;
            width = 10;
        }
        //======================================
        public double getlength()
        {
            return length;
        }
        
        public void setAge(double a)
        {
            age = a;
        }
    
        public double getage()
        {
            return age;
        }
        public boolean getexists()
        {
            return exists;
        }
        
        public void setexists(boolean b)
        {
            exists = b;
            
        
        }
        public void Triple(int a)
        {
            a = a*3;
            System.out.println(a);
        }
    
    
        public Thing (double l, double w, double h )
        {
            length = l;
            width =  w;
            height = h;
            
       }
    }
    
   


// Subclass of Superclass
// Thing

public class ManMade extends Thing {  // inherits from Thing
    private string name;
    private double volume;
    private double surfarea;
    
    // no argument constructor
    public ManMade()
    {
        super(0, 0); // call the superclass contructor
        name = ;
    }
    
    // Constructor
    public ManMade( double l, double, w, double h, char s )
    {
        super( l, w, h); // call the superclass constructor
        setName( "Bob" )
        setVolume( v );
        setSurarea( S );

I am not sure whether i am doing this right.

any help is appreciated
 
Technology news on Phys.org
When you post code, can you please say what you want to do. What is it that you aren't sure of?? You make a subclass by using the extends keyword, like you've done, so what's the problem? All I can see a problem is with the line

Code:
name = ;

You need to write name = " "; cos otherwise the code won't compile (obviously you won't be able to compile that code on its own, as there's no main(), but you know..).
 
Last edited:
courtrigrad said:
Hello all

Code:
     public ManMade()
    {
        super(0, 0); // call the superclass contructor
        name = ;
    }
Where is the
Code:
Thing
constructor with two arguments?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top