Module 7 (CSC 222)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is an "abstract class" in object-oriented programming?

A class that guides the design of subclasses but cannot itself be instantiated as an object. Ex: An abstract Shape class might also specify that any subclass must define a computeArea() method.

What is a "UML diagram"?

A diagram that indicates class inheritance relationships

What does a "class" do in object-oriented programming?

Encapsulates data and behavior to create objects.

True or false: Any class can serve as a Base Class (AKA Superclass)

True

What is a "Derived Class"?

A Derived Class is a class that is derived from another class, called its Superclass. The derived class inherits the properties of the base class, a concept called inheritance. This means that the Derived Class can call on methods from its Superclass.

How is a Derived Class declared?

A Derived Class is declared by placing the keyword "extends" after the Derived Class name, followed by the Superclass name. Ex: class DerivedClass extends BaseClass { ... (code) ... }

What is "polymorphism" in Java?

When you have multiple methods with the same names, and the program needs to figure out which method to use.

What is a "Superclass"? *What is its other name?

A Superclass (AKA a "Base Class") is a class from which a Derived Class is derived; the Superclass passes on all of its public members to its Derived Class.

What's the difference between a "behavioral" and "structural" diagram? Which one is a UML class diagram?

A structural diagram visualizes static elements of software, such as the variables and methods used in the program. A behavioral diagram visualizes dynamic behavior of software, such as the flow of an algorithm. A UML class diagram is a structural diagram that can be used to visually model the classes of a computer program, including member variables and methods.

What is an Object Class? Does it have a Superclass?

The built-in Object class serves as the base class for all other classes and does not have a base class. All classes, including user-defined classes, are derived from Object and implement Object's methods.

What two common methods are defined within the Object class, and what do they do?

toString() -- returns a String representation of the Object. By default, toString() returns a String containing the object's class name followed by the object's hash code in hexadecimal form. Ex: java.lang.Object@372f7a8d. equals(otherObject) -- compares an Object to otherObject and returns true if both variables reference the same object. Otherwise, equals() returns false. By default, equals() tests the equality of the two Object references, not the equality of the Objects' contents.

Which provides an API that must be implemented and no other code? A.) Interface B.) Abstract class

A.) Interface An interface does not restrict future inheritance, so is the best choice if no other code is provided.

Which provides only static final fields? A.) Interface B.) Abstract class

A.) Interface Interfaces can declare public static final fields and don't restrict a class' inheritance.

Mug / Cup Is-a Has-a

A.) Is-a

Pear / Fruit A.) Is-a B.) Has-a

A.) Is-a

What is "inheritance" in object-oriented programming?

Allows one class (a subclass) to be based on another class (a base class or superclass). Ex: A Shape class may encapsulate data and behavior for geometric shapes, like setting/getting the Shape's name and color, while a Circle class may be a subclass of a Shape, with additional features like setting/getting the center point and radius.

What is an Override Annotation and what does it do?

An Override Annotation is an optional command beginning with the "@" symbol that can provide the compiler with information that helps the compiler detect errors better. The @Override annotation causes the compiler to produce an error when a programmer mistakenly specifies parameters that are different from the parameters of the method that should be overridden or misnames the overriding method. Good practice is to always include an @Override annotation with a method that is meant to override a base class method.

What does an "interface" do, and how do programmers create one?

An interface can specify a set of abstract methods that an implementing class must override and define. In an interface, an abstract method does not need the abstract keyword in front of the method signature. To create an interface, a programmer uses the keyword interface.

Which provides variables/fields? A.) Interface B.) Abstract class

B.) Abstract class Only abstract classes can provide variables/fields to the subclasses.

How do you indicate class inheritance relationships in a UML diagram? A.) Draw a closed, filled arrowhead from the subclass to the superclass B.) Draw a closed, unfilled arrowhead from the subclass to the superclass C.) Draw a closed, filled arrowhead from the superclass to the subclass D.) Draw a closed, unfilled arrowhead from the superclass to the subclass

B.) Draw a closed, unfilled arrowhead from the subclass to the superclass *And the subclass should only show members and methods unique to it, and not in the superclass

House / Door A.) Is-a B.) Has-a

B.) Has-a

Dog / Owner A.) Is-a B.) Has-a

B.) Has-an

True or false: A Superclass can call on methods from its Derived Class?

False Although a Derived Class can call on its Superclass' methods, the "Inheritance" does not go both ways. A Superclass CANNOT call on methods from its Derived Class.

True or false: The Object class's toString( ) method returns a String containing only the Object instance's type.

False Object's default toString( ) implementation returns the Object instance's type followed by an unsigned hexadecimal representation of the object's hash code.

True or false: User-defined classes are not derived from the Object class.

False The Object class is the base class for all classes including built-in classes (e.g., Integer, Double) and user-defined classes (e.g., Business).

(T/F) Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, and location. This program will benefit from an abstract class to represent the trees.

False - No information exists that is specific to each species of tree. So each tree object can simply possess the species type, age, and location information.

(T/F) Consider a program that maintains a grocery list. Each item, like eggs, has an associated price and weight. Each item belongs to a category like produce, meat, or cereal, where each category has additional features, such as meat having a "sell by" date. This program will benefit from an abstract class.

False - Normal inheritance is sufficient. A superclass like Item might implement price and weight. Then subclasses like MeatItem might add behavior like a "sell by" date. But no behavior was mentioned that must be implemented by all subclasses.

(T/F) A UML class diagram describes everything that is needed to implement a class.

False - The class diagram does not specify how methods are implemented, or even what the intended functionality for a class is.

(T/F) Any subclass that inherits from a superclass must implement all of the methods of its superclass.

False - The subclass does inherit all of the methods from its superclass, but it doesn't necessarily have to IMPLEMENT them.

What's the difference between "members" and "methods" in a class?

Members are simply variables that hold data and define the state of the object initialized via the class Methods are blocks of code that can be used on that object to retrieve members and such.

Assume that an ArrayList of type ArrayList<Object> called myList contains only three elements of type Double. Is this statement valid?: myList.get(0).doubleValue(); Note that the method doubleValue() is defined in the Double class but not the Object class.

No The ArrayList is declared to contain elements of type Object. Thus, the methods a program may invoke on the elements must be defined by the Object class.

What's the difference between "inheritance" and "composition"?

The concept of inheritance is commonly confused with the idea of composition. Composition is the idea that one object may be made up of other objects, such as a MotherInfo class being made up of objects like firstName (which may be a String object), childrenData (which may be an ArrayList of ChildInfo objects), etc. Defining that MotherInfo class does not involve inheritance, but rather just composing the sub-objects in the class.

What does it mean for a Derived Class to "override" its Superclass? Bonus: What /should/ you do when an override occurs?

This is when a Derived Class defines a method with the same name and parameters as its Superclass. Bonus: It is usually good to annotate with: "@Override" right above the overriding method.

True or false: A Derived Class can call on methods from its Superclass?

True A Derived Class inherits the properties of its Superclass, and can call on the methods as such.

True or false: All classes can access Object's public and protected methods like toString() and equals(), even if the methods are not explicitly overridden.

True A derived class inherits the methods defined by the class's base class. Thus, user-defined classes always inherit Object's methods.

(T/F) Creating a derived class is generally less work than creating an independent class.

True A derived class only needs to implement members that add additional functionality.

True or false: The built-in Integer class overrides the toString() method in order to return a String representing an Integer's value.

True The Integer class provides a toString() implementation that is more appropriate for the class's intended use. Instead of returning the Integer instance's type and memory address, toString() returns the Integer's value.

True or false: An item of any class type may be added to an ArrayList of type ArrayList<Object>.

True The Object class is the base class for all classes. Thus, an item of any class type is implicitly casted to the Object type and added to the ArrayList.

True or false: Creating an independent class that has the same members as an existing class creates duplicate code.

True The independent class will duplicate the shared data members and functions from the existing class. Creating duplicate code should generally be avoided.

(T/F) Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, location, and estimated size based on age. Each species uses a different formula to estimate size based on age. This program will benefit from an abstract class.

True - A superclass Tree might store the age and location, and specify an estimateSize() behavior. Subclasses for each species like OakTree then implements the species particular growth rate formula.

What is "compile-time polymorphism"?

When a superclass and a subclass have two of the same methods, but each method has different arguments/parameters. This way, when the method is called, the program judges which method to use "at compile-time" based on the parameters/arguments it receives. *Think: Overload*

What is "runtime polymorphism"?

When multiple superclasses/subclasses have methods with the same names AND parameters. When the method is called, the program will determine which to use based on which of the classes was initialized. For example: Imagine you have class Animal (superclass), class Cat extends Animal (subclass), and class Dog extends Animal (subclass). They all have the 'public void makeSound( )' methods, but each method outputs something different ("animal noise", "meow", and "woof", respectively). If animal1 is initialized as class Dog and animal2 is initialized as class Cat, then animal1.makeSound( ) will output "woof" and animal2.makeSound( ) will output "meow".

Can a Derived Class also serve as a Superclass?

Yes You can totally make a class, make a Derived Class from that class, and then make yet another Derived Class from the original Derived Class.


Ensembles d'études connexes

CYU Chapters 27, 28, 29, 31 & 33

View Set

Tema 6 - Los Reyes Católicos II

View Set

history nationalism + imperialism quiz october 11 - alex sinins

View Set

Ch. 12: T-Tests for Dependent Samples

View Set

Pharmacology Exam 2 Chapter 9-11 PrepU Questions

View Set