Chapter 4 - Inheritance for Code Reuse

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

Obj. of derived class -> has type of: i) derived class + ii) every ancestor class it has hence, can be used in place of any obj. of ancestor type (NOT Vice-versa) ..... (flip card)

; Ancestor type X be replaced with one of its derived types.

Define Inheritance.

A technique in OOP that enables code re-usage. i) Create super/base class ii) Create sub class/derived class + add instance var.s + methods

How do sub-classes work?

By inheriting: i) all public methods ii) all public + pvt. instance var.s iii) all public + pvt. static var.s (all of the above from base class)

How does a sub class initialize all the data inherited from the base class?

Call super as 1st action in constructor definition.

What does "this" do?

Can be used as a name to invoke another constructor in the same class, within the def. of a constructor of a class.

What is Protected Access?

Means that method/instance var. can be accessed by: i) name inside own class def. ii) name inside any class derived from it. iii) name in the def. of any class in the same package.

Overloading a method results in:

Method in the sub class having a different signature to the method in the super class.

Can the getClass( ) method be overriden?

No because the method is marked "final".

Can equals method be overloaded?

No, it should be overriden.

What if a sub class doesn't call super?

No-arg. constructor of the base class will automatically be invoked -> causing potential error if base class has not defined a no-arg. constructor.

Can an instance var. be used an arg. to super?

No.

Can the return type be changed when overriding a method?

No.

If a final modifier is placed before the definition of a method in the super class, then can that method be redefined in a derived class?

No.

If a final modifier is placed before the definition of a class then can that class be used to derive/ create sub classes?

No; inheritance is not possible

How many times can we invoke super?

Only once; to invoke method directly from parent class

Issue w/ Protected Access?

Protected modifier provides very weak protection compared to the private modifier: It allows direct access to any programmer who defines a suitable derived class.

How does getClass( ) work?

Returns rep. of only the class that was used to create the new obj.

instanceof():

Returns true when: i) obj. = member of class being tested. ii) obj. = descendant of class being tested.

Overriding a method results in:

The new method definition in the sub class having the exact same i) no. of parameters ii) types of parameters as in the super class.

When the sub class overloads the original method, it still inherits the OG method from the super class. True or False?

True

Define package access.

When an instance var./method def. is not preceded with a modifier, they are said to have package/ (default/friendly) access. -> Enables package access by name inside the definition of any class in the same package.

Can the return type be changed to a sub-class type?

Yes. -> called *Covariant Return Type* (only allowed in Java 5.0 & onwards)

Describe the Object Class:

a) In java every class = descendant of object class b) parameter of obj. type can be replaced by obj. of any class c) has methods that every class inherits; toString, equals

Issue with pvt. var.:

a) Pvt. instance var. in a base class is inheritable ***however*** b) cannot directly be accessed by name in a method def. of derived class. Note: They can only be accessed by public accessor + mutator methods defined in class.

Sub-Classes can:

i) add more instance + static var.s/methods ii) change/override inherited methods if necessary

If we need to call both, super + this:

~ 1st call this. ~ 2nd call super.


Ensembles d'études connexes

3.4 Alternative Mechanisms of Carbon Fixation

View Set

Practice Test 2 : Questions Answered Incorrectly or Guessed

View Set

Lesson 77: Types of Trade Barriers

View Set

Section 11: Texas Defaults and Foreclosures

View Set

P&C Chapter 24 GA state laws and regulations

View Set

Vitamins and Minerals (Task C Term 1)

View Set

Positioning 2nd Year-Femur and Hip

View Set

Unit 3- Clinical decision making

View Set

mcb chpt. 23 (w/ answer options)

View Set

***Explain Aristotle's "function argument" (Nicomachean Ethics, Book 1, chapter 7) and the role it plays in his ethical theory.

View Set

English Test 2/ Examen de Ingles 2

View Set

Chapter 12 aggregate demand and supply

View Set

Smartbook 2.0 - Student Orientation Quiz

View Set