0% found this document useful (0 votes)
18 views

JAV_INT - Interfaces & Abstract Classes (Student-fillable) Complete

The document explains the concepts of interfaces and abstract classes in Java, highlighting their roles in grouping classes with similar functionality. It outlines learning objectives related to supertype variables, method availability, and implementation requirements for interfaces and abstract classes. Additionally, it provides examples and exercises to illustrate these concepts, including the implementation of a Shape interface and an abstract Person class.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

JAV_INT - Interfaces & Abstract Classes (Student-fillable) Complete

The document explains the concepts of interfaces and abstract classes in Java, highlighting their roles in grouping classes with similar functionality. It outlines learning objectives related to supertype variables, method availability, and implementation requirements for interfaces and abstract classes. Additionally, it provides examples and exercises to illustrate these concepts, including the implementation of a Shape interface and an abstract Person class.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Interfaces & Abstract Classes page 1 of 10

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

-radius: double -width: double


-height: double
+Circle(radius:double)
+getRadius(): double +Rectangle(width:double,height:double
+setRadius(radius:double )
) +getWidth(): double
+area(): double +setWidth(width:double)
+circumference(): double +getHeight(): double
+setHeight(height:double)
+area(): double

1. (2 min) What do the Circle and Rectangle classes have in common?


they both have a area

2. (1 min) What is different between the Circle and Rectangle classes?


circle has a radius and circumference while rectangle has width and hieght

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.

Rectangle rect = new Rectangle(10.0, 10.0);


Circle circ = new Circle(35.0);
System.out.println( circ.area() );
System.out.println( rect.area() );

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

Rectangle rect = new Rectangle(10.0, 10.0);


Circle circ = new Circle(35.0);
Object obj;

obj = rect; // OBJECT VARIABLE CAN HOLD ANY OBJECT TYPE


obj = "some text";
obj = circ;

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];

arr[0] = new Circle(35.0);


arr[1] = new Rectangle(10.0, 10.0);

for(int i=0; i < arr.length; i++)


System.out.println( arr[i].area() );

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:

public interface Shape{


public double area();
}

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.

Shape shape = new Circle( 1.0 );


System.out.println( shape.area() );

shape = new Rectangle( 30.0, 30.0 );


System.out.println( shape.area() );
Interfaces & Abstract Classes page 5 of 10

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;

public Triangle(double side1, double side2, double side3){


sides = new double[3];
sides[0] = side1;
sides[1] = side2;
sides[2] = side3;
}
}

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:

public staic void printArea(Shape shape){


system.out.println(shape.area());
}
Interfaces & Abstract Classes page 7 of 10

start 124
(14 min) C. Abstract Classes time:

public abstract class Person{


private String name;

public String getName(){


return name;
}

public void setName( String name ){


this.name = name;
}

public abstract void printInfo();


}

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.

public class Employee extends Person{


private int salary;

public Employee( String name, int salary ){


setName(name);
this.salary = salary;
}

public int getSalary(){


return salary;
}

public void setSalary( int salary ){


this.salary = salary;
}

...
}

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

Handout: Shape, Rectangle, & Circle

public interface Shape{


public double area();
}

public class Rectangle implements Shape{


private double width;
private double height;

public Rectangle( double width, double height ){


this.width = width;
this.height = height;
}

public double getWidth(){


return width;
}

public double getHeight(){


return height;
}

public void setWidth( double width ){


this.width = width;
}

public void setHeight( double height ){


this.height = height;
}

public double area(){


return width * height;
}
}
Interfaces & Abstract Classes page 10 of 10

public class Circle implements Shape{


private double radius;

public Circle( double radius ){


this.radius = radius;
}

public double getRadius(){


return radius;
}

public void setRadius( double radius ){


this.radius = radius;
}

public double area(){


return Math.PI * Math.pow( radius, 2 );
}

public double circumference(){


return 2 * Math.PI * radius;
}
}

You might also like