Nocturne
Nocturne

Reputation: 31

Clarification on creating Objects

This question might be a very simple question, but I am still kind of new to programming so please bear with me. If I have for example a class name picture and another class name simplePicture. Can I create a new object as: Picture a = new simplePicture(); So can I create an object using the constructor of another object? And if possible, how is that possible?

Upvotes: 0

Views: 36

Answers (2)

HJK
HJK

Reputation: 1382

Basically it is only possible when there is a hierarchy.

 class SimplePicture extends Picture{

 }

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79807

You can do that if SimplePicture has been defined with the words extends Picture after the name. That would make SimplePicture a subclass of Picture, which means that any SimplePicture is also a Picture, and that therefore all objects of class SimplePicture can be referred to with a variable of type Picture.

public class SimplePicture extends Picture {
    // ... put things here that are specific to SimplePicture
}

Upvotes: 1

Related Questions