title | type | duration | creator | ||
---|---|---|---|---|---|
OOP Review |
Homework |
1:00 |
|
Respond to the following questions based on what we've learned (and maybe what we haven't covered yet).
-
What's the difference between member variables (also called instance variables) and class variables (with the
static
keyword)? Which can be accessed without creating an instance of the class? ####Intance variables belong to an instance of the class. Class variables belong to the class definition, so I cannot instantiate them. Class variables can be accessed without an instance. -
Does it make sense to write getter and setter methods for a
public
member variable? What aboutprivate
variables? ####Private member variables are not accessible outside the class and cannot be accessed without getter/setter. Public variables do not require this. -
What are some benefits of making member variables
private
? ####Private member variables enable code encapsulation and access control. -
If Class A extends Class B, which is the superclass and which is the subclass? Which would you call the parent, and which would you call the child? ####Class B is the parent/superclass. Class A is the child/subclass.
-
What does it mean for a class to inherit methods or variables (or both) from its parent class? ####Inheritance means a subclass has access to the methods and variables of the parent class.
-
Consider the following code, where the
Refrigerator
class extends theAppliance
class, andgetTemperature()
is a method inRefrigerator
, but not inAppliance
:Appliance myAppliance = new Refrigerator(); double temperature = myAppliance.getTemperature();
Why will this call to
getTemperature()
cause an error? How will casting help solve this issue? ####This will fail because myAppliance is a reference to an Appliance class which does not have getTemperature(). Casting myAppliance to Refrigerator will allow access to getTemperature(). -
In a normal class (also called a concrete class), do you need to implement all the methods, or can you simply declare some? What about in an
abstract
class? ####A normal class implements all methods. An abstract class will have some methods with no implementation. -
What about an
interface
? Can you implement any methods in aninterface
? Can you declare methods in aninterface
? ####An interface has no implementations, just declarations. -
Can you create an instance of an
abstract
class? Also, look up the Java keywordfinal
and see if you can explain why a class cannot be bothabstract
andfinal
. ####An abstract class cannot be instantiated. -
What happens when a method overrides another method? If parent and child classes have methods with the same name, when you call that method on an instance of the child class, which implementation of the method will be executed? ###The overridden method in the child class is executed.
-
What's the relationship between
List
,LinkedList
, andArrayList
? Why do we call a method polymorphic if it takes an input of typeList
rather than an input of typeLinkedList
orArraylist
? Why is that useful? ####List is the parent class of LinkedList and ArrayList.
This file, with your answers added.
Refer to the "Classes and Objects" lesson, the "Subclasses" lesson, and the "Abstract Classes and Interfaces" lesson.
Feel free to do a Google search for these concepts as well. There are plenty of Java tutorial websites and Stack Overflow posts that can help you. But be sure to write up your answers in your own words — copying and pasting some text does not help you actually learn (and is, in fact, cheating).