Skip to main content
Logo image

Section 4.65 Picture Lab A5: Picture Classes and Inheritance

Subsection 4.65.1 Picture Classes: UML and Inheritance

The Picture class inherits attributes and methods from the SimplePicture class and the SimplePicture class implements the DigitalPicture interface as shown in the Unified Modeling Language (UML) class diagram in the figure below.
Figure 4.65.1. A UML Class diagram
A UML class diagram shows classes and the relationships between the classes. Each class is shown in a box with the class name at the top. The middle area shows attributes (instance or class variables) and the bottom area shows methods. The open triangle points to the class that the connected class inherits from. The straight line links show associations between classes. Association is also called a β€œhas-a” relationship. The numbers at the end of the association links give the number of objects associated with an object at the other end.
For example, it shows that one Pixel object has one Color object associated with it and that a Color object can have zero to many Pixel objects associated with it. You may notice that the UML class diagram doesn’t look exactly like Java code. UML isn’t language specific.
The following questions require some knowledge about inheritance which is covered in the next unit, Unit 9. You may want to come back to do these questions after Unit 9.
Click on this Picture Lab project link
 1 
https://replit.com/@BerylHoffman/Picture-Lab
and click on Show files to answer the following questions.

Activity 4.65.1.

Click on the Picture Lab project link
 2 
https://replit.com/@BerylHoffman/Picture-Lab
and click on Show files. Open Picture.java and look for the method getPixels2D. Is it there?
  • The Picture.java class does not have the getPixels2D() method defined in it but it inherits it from the class SimplePicture.
  • No, but it is inherited
  • Correct, this class inherits that method from the class SimplePicture.

Activity 4.65.2.

Open SimplePicture.java and look for the method getPixels2D. Is it there?
  • Yes, the SimplePicture class contains the method getPixels2D.
  • The SimplePicture class contains the method getPixels2D.

Activity 4.65.3.

This question is about interfaces which are not covered in the AP exam. Interfaces are like abstract templates of a class that specify the method headers but not the definitions. Does the following code compile? Try it in the main method if you do not know.
DigitalPicture p = new DigitalPicture();
  • We cannot create an object from an interface because it is abstract.
  • Correct! We cannot create an object from an interface because it is abstract.

Activity 4.65.4.

This question is about interfaces which are not covered in the AP exam. Assuming that a no-argument constructor exists for SimplePicture, would the following code compile?
DigitalPicture p = new SimplePicture();
  • Yes. The SimplePicture class implements the interface DigitalPicture which means it is a type of Digital Picture.
  • The SimplePicture class implements the interface DigitalPicture which means it is a type of Digital Picture.

Activity 4.65.5.

This question is about interfaces which are not covered in the AP exam. Assuming that a no-argument constructor exists for Picture, would the following code compile?
DigitalPicture p = new Picture();
  • Yes, because Picture extends SimplePicture which implements the interface DigitalPicture.
  • Picture extends SimplePicture which implements the interface DigitalPicture.

Activity 4.65.6.

Assuming that a no-argument constructor exists for Picture, does the following code compile?
SimplePicture p = new Picture();
  • Yes, because Picture extends SimplePicture which implements the interface DigitalPicture.
  • Picture extends SimplePicture which implements the interface DigitalPicture.

Activity 4.65.7.

Assuming that a no-argument constructor exists for SimplePicture, does the following code compile?
Picture p = new SimplePicture();
  • Picture inherits from SimplePicture, but not the other way around.
  • Picture inherits from SimplePicture, but not the other way around.
Because DigitalPicture declares a getPixels2D method that returns a two-dimensional array of Pixel objects, SimplePicture implements that interface, and Picture inherits from SimplePicture, you can use the getPixels2D method on a Picture object. You can loop through all the Pixel objects in the two-dimensional array to modify the picture. You can get and set the red, green, and/or blue value for a Pixel object. You can also get and/or set the Color value for a Pixel object. For example,
Picture pict = new Picture("beach.jpg");
Pixel[][] pixels = pict.getPixels2D();
Pixel p = pixels[0][0]; // get the first pixel
int blue = p.getBlue(); // get its blue value
System.out.println("Pixel (0,0) has a blue value of " + blue );
p.setBlue(255);  // set its blue value to 255
pict.show();
You have attempted of activities on this page.