JAV_INT - Interfaces & Abstract Classes (Student-fillable) Complete
JAV_INT - Interfaces & Abstract Classes (Student-fillable) Complete
start 1245
Interfaces & Abstract Classes time:
It's common that classes have similar methods or data, but different internal behavior (both
cars and bikes accelerate, but they do it differently.) Java allows us to group classes together
and treat them as the same type in certain ways. In this activity, you will look at two ways to
group classes with similar functionality or data: interfaces and abstract classes.
Learning Objectives (for content & process)
After completing this activity, learners should be able to:
● Use supertype variable to hold objects of subclass types.
● Identify which methods will be available to variables of supertype or subclass types.
● Determine the requirements for a class implementing an interface.
● Determine the requirements for a class extending an abstract class.
Before you start, complete the form below to assign a role to each member.
If you have 3 people, combine Speaker & Reflector.
Team Date
8/27/24
Team Roles Team Member
Recorder: records all answers & questions,
and provides copies to team & facilitator. Garrett
Speaker: talks to facilitator and other teams.
Manager: keeps track of time and makes sure
everyone contributes appropriately. Aidan
Reflector: considers how the team could
work and learn more effectively. Armaan
Reminders:
1. Note the time whenever your team starts a new section or question.
2. Write legibly & neatly so that everyone can read & understand your responses.
Interfaces & Abstract Classes page 2 of 10
start 1246
(15 min) A. Related Classes time:
Circle Rectangle
3. (2 min) Describe the behavior of the area method for each class.
circle area takes one input, the radius, while the rectangle area takes two inputs, width and
hiegth.
4. (2 min) How does your team think that Java knows which version of area to call in the
example above?
based on the class it calls it calls a different version of the method
Interfaces & Abstract Classes page 3 of 10
5. (1 min) Give an example of a method call that would be allowed using the variable circ but
would not be allowed using the variable rect.
getradius() works with circ but not with rect
In Java, all classes are inherited from the type Object. In other words, Object is a
superclass (or parent class) of all other class types. A variable of the superclass' type can
hold objects of the subclass (or child class) type.
6. (2 min) Why to you think the compiler would allow circ.circumference() as the next
line of code in the example above, but would not allow obj.circumference().
because object is a superclass and obj.circumference doesn't specificly call the child class in
the object class
7. (2 min) Explain why the following code would produce compiler errors where the area
method is called.
Object[] arr = new Object[2];
there is no area method to call because the array is an array of Objects and so the call of
the .area() does not exist in that context
Interfaces & Abstract Classes page 4 of 10
start 105
(16 min) B. Interfaces time:
One solution to this problem of accessing behavior that two or more classes share in Java is to
use an interface. Interfaces work like a contract, to say that any class that implements the
interface will define methods that fulfill the signatures given in the interface. It does not
require that those methods have the same implementation.
8. (2 min) If both Circle and Rectangle implement the Shape interface, then the compiler
will allow Shape variables to be used to call the area method.
Write code for printing the area of all shapes in the array defined below.
Shape[] shapes = new Shape[10];
shapes[0] = new Circle(35.0);
shapes[1] = new Rectangle(10.0, 10.0);
...
for(int i =0; i<2;i++){
system.out.println(shapes[i].area());
}
9. (1 min) Explain why the compiler still would not allow your code to call
shapes[0].circumference() from the Shape array above.
because shapes does not have a circumference method, it would have to specificy shapes
[0].circ.circumference()
Look at the Circle and Rectangle classes and the Shape interface in the handout at the end
of the document.
Because Circle and Rectangle implement Shape, Shape is the supertype (not a
superclass, because it's not a class.) Circle and Rectangle are subclasses. Remember that
any class that implements an interface is required to have methods that match the
signature of the interface's methods.
10. (3 min) Predict the approximate output of the code above. (You can run the code in a main
method in either Rectangle or Circle to confirm your prediction.)
it would print the coresponding area of the shapes
11. (1 min) In the example above, how does the compiler know that area is allowed to be called
on shape?
yes because the circle and rectangle classes implement the shape interface
12. (1 min) Notice that a different version of the area method is called each time. How does
Java select which version of area should be called when the code runs?
by the subclass with the Shape supertype
13. (2 min) Considering the answers above, how does Java determine if code that implements
an interface can be compiled, and what does Java decide at execution time (as the
program runs) to select which class method to execute?
It would check the subclasses contained within the shape supertype
14. (2 min) Explain why the code for the Triangle class below would not compile. (Notice
that Triangle is supposed to implement Shape.)
public class Triangle implements Shape{
private double[] sides;
it has no area method in the class so it can not implement shape since it need the area
method
Interfaces & Abstract Classes page 6 of 10
15. (2 min) What prevents Java from ever creating a Shape object, even though we can declare
Shape variables?
because shape is declared as an interface and not a class or object
16. (2 min) Explain why it makes sense as a design decision not to have a Shape class that can
be used to create actual Shape objects.
because shape is too general, it will always need a subclass in order to function and be of
use
17. (3 min) Design a static method called printArea that takes a shape as a parameter and
prints the area of the shape given to it. Write the method code here:
start 124
(14 min) C. Abstract Classes time:
Another way to guarantee behavior for a group of classes is to extend an abstract class. Like
interfaces, they can't be instantiated to create objects of that type, and they can be
superclasses for other classes.
18. (2 min) What does the abstract class Person provide for its subclasses that an interface
wouldn't?
it has methods that can be left blank for subclasses to add on to and customize
19. (2 min) The printInfo method has no body (like an interface's methods) because it is
abstract. What does this mean for classes that extend Person?
they can have their own custom printInfo classes they do unquie things
20. (1 min) List all methods that could be used by any subclass of Person?
getName()
setName()
printInfo()
21. (1 min) The name field is private, meaning it can't be directly accessed from outside the
class. How will subclasses be able to make use of this field?
through get and set functions
Interfaces & Abstract Classes page 8 of 10
22. (3 min) Give an example of how you might implement printInfo for the Employee class
below to print all data for an Employee.
...
}
you create a printInfo method in this class that prints the Employee's name and salary
23. (3 min) Describe what would be needed to create a new subclass of Person named Client
that has a accountBalance field representing the dollar amount of their current account
balance.
it would need a double to store the accountBalance and to set the name in the Person class
on creation
24. (2 min) Describe in general what the benefits are of having an abstract class like Person
(as opposed to just an interface.)
it has methods that can be left blank for subclasses to add on to and customize alowing for
more diverse uses for the abstract class
Interfaces & Abstract Classes page 9 of 10