Skip to content

TatyCoder/Java-OOP-Review

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

title type duration creator
OOP Review
Homework
1:00
name
Charlie Drews

OOP Review

Requirements

Respond to the following questions based on what we've learned (and maybe what we haven't covered yet).


Questions

  1. 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.

  2. Does it make sense to write getter and setter methods for a public member variable? What about private variables? ####Private member variables are not accessible outside the class and cannot be accessed without getter/setter. Public variables do not require this.

  3. What are some benefits of making member variables private? ####Private member variables enable code encapsulation and access control.

  4. 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.

  5. 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.

  6. Consider the following code, where the Refrigerator class extends the Appliance class, and getTemperature() is a method in Refrigerator, 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? ####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().

  7. 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.

  8. What about an interface? Can you implement any methods in an interface? Can you declare methods in an interface? ####An interface has no implementations, just declarations.

  9. Can you create an instance of an abstract class? Also, look up the Java keyword final and see if you can explain why a class cannot be both abstract and final. ####An abstract class cannot be instantiated.

  10. 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.

  11. What's the relationship between List, LinkedList, and ArrayList? Why do we call a method polymorphic if it takes an input of type List rather than an input of type LinkedList or Arraylist? Why is that useful? ####List is the parent class of LinkedList and ArrayList.

Deliverable

This file, with your answers added.


Additional Resources

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).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published