title | type | duration | creator | ||||
---|---|---|---|---|---|---|---|
Subclassing and Interfaces |
Homework |
1:00 |
|
Note: You can discuss with classmates, but everyone must submit their own answers
- Fork this repo, then add your answers direcly to this readme.md file
- After forking, you can clone, edit the readme in the text editor of your choice, and push back to Github...
- Or, you can edit the readme right from the browser
- After adding your answers, submit a pull request
- What is the difference between member variables (also called instance variables) and class variables (w/ keyword
static
)? Which can be accessed without creating an instance of the class?
The difference between them is the static keyword which defines the class level. Static determines whether the method can be called and accessed within the Class or without creating a new instance. ex. Class.method(); vs. Class method = new Class();
- Does it make sense to write getter and setter methods for a
public
member variable? What aboutprivate
variables?
No because the public access modifiers allows programmers to change and modify the member variable, if it was private the getters and setters are needed because it cannot be modified outside the Class.
- What are some benefits of making member variables
private
?
A beneift of making private member variables is that it can not be changed nor accessed by anyone since the access modifier is private and not public.
- If class A extends class B, which is the super class and which is the sub class? Which would you call parent, and which would you call child?
Classs B is the super class and Class A is the subclass. Class B is the parent and Class A is the child
- What does it mean for a class to inherit methods and/or variables from its parent class?
It means that all methods and member variable under the parent can be called and defined under the child class.
- Consider the following code, where class Refrigerator extends class Appliance, and
getTemperature()
is a method in Refrigertor, but NOT in Appliance:
Appliance myAppliance = new Refrigerator();
double temperature = myAppliance.getTemperature();
Why will this call to getTemperature()
cause an error? How will casting help solve this issue?
It will cause an error because the method is not inside the parent class which is being called from Appliance. If it was called from the Refrigerator class it would solve this problem.
- In a normal class (also called a concrete class), do you need to implement all of the methods, or can your simply declare some? What about in an
abstract
class?
In a Normal class it can simply be declared and defined from the class that it is impleting which is the abstract class.
- What about an
interface
? Can you implement any methods in an interface? Can you declare methods in an interface?
Yes, can implement methods and those methods are declared int a concrete class.
- 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
.
final keyword means that the method should not be change and are usually in capital letters
- What happens when a method overrides another method? If a parent and child class 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?
It gets the method from the abstract clasee even if it hasn't define and can be defined in the child class.
- What is 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
, and why is that useful?
This file, with your answers added
Refer to the Classes lesson, the Subclassing lesson, and the Interfaces & Abstract Classes lesson.
Feel free to google these concepts as well. There are plenty of Java tutorial websites and StackOverflow 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!
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].