This book is now obsolete Please use CSAwesome instead.
11.22. Object-Oriented Summary¶
In this chapter you learned about object oriented programming concepts. In an object-oriented program you write classes that define what objects of each class know (fields) and can do (methods). One class can inherit object fields and methods from another, which makes the amount of code that you have to write smaller and makes the classes easier to test and extend.
11.22.1. Concept Summary¶
object - Objects do the action in an object-oriented program. An object can have things it knows (fields) and things it can do (methods). An object is created by a class and keeps a reference to the class that created it.
class - A class defines what all objects of that class know (fields) and can do (methods). You can also have data and behavior in the object that represents the class (class fields and methods). All objects of a class have access to class fields and class methods, but these can also be accessed using
inheritance - One class can inherit object fields and methods from another. This makes it easy to reuse another class by extending it (inheriting from it). This is called specialization. You can also pull out common fields and/or methods from several related classes and put those in a common parent class. This is called generalization.
polymorphism - The runtime type of an object can be that type or any subclass of the declared type. All method calls are resolved starting with the class that created the object. If the method isn’t found in the class that created the object, then it will look in the parent class and keep looking up the inheritance tree until it finds the method. The method must exist, or the code would not have complied.
parent class - One class can inherit from another and the class that it is inheriting from is called the parent class. The parent class is specified in the class declaration using the
extendskeyword followed by the parent class name.
child class - The class that is doing the inheriting is called the child class. It inherits access to the object fields and methods in the parent class.
subclass - A child class is also called a subclass.
superclass - A parent class is also called a superclass.
declared type - The type that was used in the declaration.
List aList = new ArrayList()has a declared type of
List. This is used at compile time to check that the object has the methods that are being used in the code.
run-time type - The type of the class that created the object.
List aList = new ArrayList()has a run-time type of
ArrayList. This is used at run-time to find the method to execute.
overrides - A child class can have the same method signature (method name and parameter list) as a parent class. Since methods are resolved starting with the class that created the object, that method will be called instead of the inherited parent method, so the child method overrides the parent method.
overload - At least two methods with the same name but different parameter lists. The parameter lists can differ by the number of parameters and/or the types.
getter - A method that returns the value of a field in an object.
setter - A method that sets the value of a field in an object.
accessor - Another name for a getter method - one that returns the value of a field.
mutator - Another name for a setter method - one that changes the value of a field.
abstract class - A class that is used as a common parent for several child classes, that typically has at least one abstract method, but can also have fields and non-abstract methods. You can not create an object of an abstract class, but must create an object of a concrete (not-abstract) child class.
interface - A special type of abstract class that can only have public class constants and public abstract methods. These are useful for separating what a class is from what it does.
11.22.2. Java Keyword Summary¶
extends - Used to specify the parent class to inherit from. It is followed by the name of the parent class, like this:
public class ChildName extends ParentName. If no
extendskeyword is used in the class declaration, then the class will automatically inherit from the
static - Keyword used to indicate that a field or method is part of the class and not part of each object created by the class.
super - Keyword used to call a method in a parent class. This is useful if a child class overrides an inherited method, but still wants to call it.
10-20-1: Drag the item from the left and drop it on its corresponding answer on the right. Click the "Check Me" button to see if you are correct.
Review the summaries above.
- A class that extends another class
- child class
- A class that is being extended
- parent class
- Using the run-time type of an object to determine which method to call
- Providing a method in a child class with the same declaration as a parent method
10-20-2: Drag the description from the left and drop it on the correct code on the right. Click the "Check Me" button to see if you are correct.
Review the summaries above.
- What does the actual work in an object-oriented program
- Defines what all objects of the class know and can do
- Returns the value of a field
- Sets the value of a field
10-20-3: Drag the description from the left and drop it on the correct code on the right. Click the "Check Me" button to see if you are correct.
Review the summaries above.
- A class that inherits from the specified class
- Two methods with the same method name in a class, but with different parameters
- The type the object was declared as
- declared type
- The class that created the object
- actual type