General

Chat

How to call a Constructor in another class?

Hey all,

I have a comp sci project due on Thursday night and I need help desperately. I'm wondering how you can call a Constructor from one class in Java in another class. The first class is an Address class with a Constructor with four String arguments. I want to call it in a Photoprocessing class that has only one string argument so that the combined Constructor has 5 string arguments. How do I do it? The Address class is also immutable so I can't have any set methods. Right now as I have it, it's combining the name with my default Constructor which is not what I want. I need it to display the information the user entered. I'll send along a picture soon.
Thanks!

November 2, 2015

2 Comments • Newest first

xdarkshynobi

I took coding in middle school & barely know anything about html beside what you can do with MySpace. Kinda forgot most of it newayszz

Reply November 3, 2015
Shimy

Sounds like you're dealing with inheritance. Read up on inheritance (google java inheritance) for more details.

In java you use the word extend to show inheritance. From my understanding you want the Photoprocessing class to have the same member variables as Address but also have a member variable of its own.

Lets start with the Photoprocessing class.

//Keyword extends, read up on it!
public class Photoprocessing extends Address
{
String something; this is our member variable for this class
public Photoprocessing(String somethingInput)
{
super(); // This calls on the super class, your other 4 member variables are now with this class
//to display it, you can write functions in this class that prints out the other 4 member variables
this.something = somethingInput; // Your new 5th member variable
}
}

And to call this on the main.

public static void main(Strings[] args)
{
Address addressOne = new Address(a,b,c,d); //initialize your first 4 member variables
Photoprocessing photoOne = new Photoprocessing(e); //initialize the 5th member variable.
}

photoOne should now have the 4 membervariables from the address, and also the one from its argument.

Reply November 2, 2015