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

  • Context: Java 
  • Thread starter Thread starter courtrigrad
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on writing a subclass in Java named ManMade that extends the superclass Thing. The user seeks assistance with constructors and instance variables, specifically mentioning issues with the name variable initialization and constructor calls. Key points include the need for proper syntax in variable assignment and constructor invocation, as well as the requirement for a main method for compilation. The discussion highlights the importance of correctly using the super keyword and ensuring all variables are properly initialized.

PREREQUISITES
  • Understanding of Java class inheritance and the extends keyword
  • Familiarity with Java constructors and instance variables
  • Knowledge of Java syntax for variable assignment and method calls
  • Basic understanding of Java compilation requirements, including the main method
NEXT STEPS
  • Review Java class inheritance and the super keyword
  • Learn about Java constructors and their overloading
  • Study Java syntax for variable initialization and method invocation
  • Explore Java compilation processes and the role of the main method
USEFUL FOR

Java developers, software engineers, and students learning object-oriented programming who are looking to deepen their understanding of class inheritance and constructor usage in Java.

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?
 

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K