Chapter 9 Notes

Ace your homework & exams now with Quizwiz!

When reserved word super is followed by

Parenthesis - call to superclass constructor

To protect against bad casts, you can use

instanceof operator - tests whether object belongs to particular type.

subclass constructor calls

superclass constructor with no arguments

Use the reserved word to call a

superclass method

ChoiceQuestion object differs from a question object in three ways.

-objects store various choices for the answer. -There is a method for adding answer choices -display method of the choicequestion class shows these choices that the respondent can choose one of them.

Examples of Unifrom Hiearchies

-savings account that earns interest -checking account that has no interest

how to write a toString method

Form a string consisting of the class name and the names and values of the instance variables.

In addition to toString method,

Object class provides equals method, purpose is to check whether two objects have the same contents.

Use cast to convert the type example

Question q = (Question) obj;

Assuming that class Soccer is a subclass of Sport, and game is an instance of Soccer, which of the following is invalid?

Soccer mySport = new Sport();

a subclass inherits the methods of the superclass

Vehcile class has a drive method Subclass - inherits the method

You cannot construct an object of an

abstract class, but you still can have an object reference whose type is an abstract class.

You should implement method doTheTask in the Question class, override it in ChoiceQuestion

q.doTheTask();

ChoiceQuestion objects is subset of all

question objects.

With inheritance, we have to present a question to userto not know exact type of

question.

Hashcode

random code used to tell objects apart-different objects are likely to have different hashcodes.

subclass reference can be used when a superclass

reference is expected.

super is not a

reference to an object. There is no separate superclass object.

Inheritance

relationship between a more general and a more specific class. -More specific class inherits behavior from the more general class.

equals method checks whether two objects have

same contents.

To call superclass constructor, use the super reserved word in the first

statement of the subclass constructor

if choices.size() is 2, then answer is set to

string "2"

Override the toString method to yield

string that describes the object's state

Override the toString method to yield a

string that describes the object's state.

specialzied class

subclass

you must call super.toString to get the instance variables of the superclass-

subclass can't access them directly.

overriding

subclass method provides an implementation of a method whose parameter variables have the same types.

equals method checks

two objects have the same contents.

To call a superclass constructor

use reserved word in the first statement of the subclass constructor

ChoiceQuestion methods cannot directly access the instance

variable - answer

instanceof operator tests

whether an object belongs to a particular type

hashCode

yields numerical code for storing the object in a set.

toString

yields string describing object.

Subclass inherits data and behavior from a

superclass

Unless specified otherwise the subclass constructor calls the

superclass constructor with no arguments

subclass override a

superclass method by providing a new implementation

subclass method replaces the functionality of a

superclass method, implementing different behavior

You can use a subclass object in place of a

superclass object

You only declare instance variables that are not apart of

superclass objects

Subclass reference can be used

superclass reference is expected.

You can always use a subclass object in a place of superclass

Object

Class where you cannot create objects is called

Abstract class

A subclass inherits

data and behavior from a superclass

Because Car is a subclass of Vehicle, you can call that method with a Car object:

Car myCar = new Car(. . .); processVehicle(myCar);

write a equals method

Cast the otherObject parameter variable to type of your class, and the compare instance variables of implicit parameter and explciti parameter.

addChoice method goes with

ChoiceQuestion class.

Protected data in object accessed by methods of object's class from subclasses

ChoiceQuestion inherits from Question, methods can access protected instance variables of the Question superclass

Because of dynamic method lookup,

ChoiceQuestion versions of the display and checkAnswer methods are called automatically.

Which of the following statements to initialize variable birthday is illegal?

Date birthday = new Object(5,7, 1990);

Example of superclass and subclass

Like how vehcile class is the supefclass,a nd the car class is the subclass

Cast the parameter variable to the class Stamp

Stamp other = (Stamp) otherObject;

When you have a reference type string, it must contain...

String object, never an object of a subclass.

If you compare an object of type ChoiceQuestion with an object of type Question, you find that

The reserved word extends suggests that the ChoiceQuestion object is an extended version of a Question. •The ChoiceQuestion object is larger; it has an added instance variable, choices. •The ChoiceQuestion object is more capable; it has an addChoice method.

Substitution principle

You can always use a subclass object when a superclass object is expected

Substitution

algorithm expects an object from the superclass (EX.Vehicle, supply a subclass, object, principle. You can always use substitute a a subclass object when a superclass object is expected.

subclass inherits

all methods that it does not override.

Question superlcass has methods for displaying

and checking.

Polymorphism

asking multiple objects to carry out a task, and each object does so in its own way.

Some programmers use specific type tests in order to implement

behavior that varies with each class.

Use the reserved word super to

call a superclass method

When you implement equals in subclass, you first

call equals in superclass to check whether the superclass instance variables match.

Object belongs to a given class, use a

cast to convert the type.

If you know that an object belongs to a given class, use

cast to convert type

addchoice Method

choiceQuestion.addChoice("Canada", true);

You can call inherited methods on a subclass object:

choiceQuestion.setAnswer("2");

extends reserved word

class inherits from a superclass

Extends reserved word

class inherits from superclass

Concrete class

class where you can create objects

Equals

compares objects with each other

Once the Account class has abstract method

compiler will flag attempt to create new Account() as error

equals method checks whether two objects have same

contents.

every class that is declared without explicit extends clause

extends class object.

Polymorphism makes programs easily

extensible.

overriding method can extend or replace

functionality of the superclass method

overriding method can extend or replace the

functionality of the superclass method

In object-oriented design, inheritance is a relationship betweenn

general class called superclass

Polymorphism

having multiple forms allows us to manipulate objects that share a set of tasks, even though tasks are executed in different ways.

Abstract method has no

implementation -Forces implementors of subclasses to specify concrete implementations of this method.

When you declare a method as abstract you force programmers to provide

implementations in subclasses

if you invoke method on implicit parameter, you don't have to specify

implicit parameter, and write method na,e

Private instance variables of superclass are

inaccessible. only the superclass has access to them.

Virtual machine calls instance method

locates method of implicit parameter's class. Dynamic method lookup.

super is a reserved word that forces execution of superclass

method

When the virtual machine calls an instance method, it locates

method of the implicit parameters class - Dynamic method lookup.

superclass

more general class is called "super" = not a value judgment, theory.

subclass override a superclass method by providing

new implementation

Java language specification demands equals method return false when otherObject is..

null

Polymorphism allows us to manipulate

objects that share a set of tasks even though taks are executed in different ways.

Purpose of inheritance is a model

objects with different behavior

Subclass inherits all methods it does not

overrride

the instance operator tests whether an object belongs to a

particular type

constructor of a subclass

pass arguments to a superclass constructor using reserved word super.

Constructor of a subclass can

pass arguments to a superclass constructor using the reserved word super.

Do not use the instanceof operator to bypass

polymorphism

Inherited hiearchy

process objects of different classes in uniform way.

superclass declare instance variable as protected:

public class Question { protected String text; ... }

Final reserved word

public final class String{. . .}

You can call display method of superclass by using reserved word super:

public void display() { // Display the question text super.display(); // OK // Display the answer choices . . . }


Related study sets

Renal & Urinary - Adult Health - NCLEX

View Set

Introduction to Business: Chapter 9

View Set

LAB EXERCISE 13.2 Why Some Streams Meander but Others Are Straight

View Set

ATI Fundamentals Practice A, B, & Final

View Set

NURS 225 Test 1 Book Questions, Ch. 13 - Legal & Ethical Issues, Ch. 16 - Data Analysis: Descriptive & Inferential Statistics, Ch. 15 - Reliability & Validity, Ch 12 - Sampling, Ch 6 - Qualitative Approaches to Research, Ch 5 - Introduction to Qualit...

View Set

FLS Director's Quiz (Fire and Non-Fire Questions)

View Set