COSI 12b Lecture Notes - Lecture 6: Downcasting

88 views2 pages
Polymorphism
To add a variable to a subclass’s constructor it should always be after the call to the superclass’s
constructor, never before. Private fields in a parent class can only be accessed in a subclass via
accessor methods. The protected keyword allows subclasses to see the information, but outside
classes cannot (protected is rarely used); it is an intermediate level of privacy between public and
private. The super keyword is only necessary in a subclass when the method being called has
been overridden.
The Object Class
Every class implicitly extends Object. Any object can be stored in a variable of type Object.
Polymorphism The same code can be used for several objects. A variable of type T can hold an
object of any subclass of T.
Ex: Employee employee1 = new Lawyer();
The employee1 object can only use Employee methods, not Lawyer methods. However, it will
use any overridden methods from Lawyer. A Lawyer or Secretary class can be passed as a
parameter for a method that takes in an Employee type. Arrays of superclass types can be used to
store subclasses.
Ex: Employee [] e = {new Lawyer(), new Secretary()};
Object Casting
When an object is cast as another type it only applies to the line in which it was cast (the type
does not stay changed afterwards). Casting never changes the object itself. employee1 is a lawyer
object of the type Employee. Upcasting is implicit/automatic in Java. Unlike upcasting,
downcasting can only go down one level. Ex: Employee employee1 cannot be cast as a
LegalSecretary (2 levels or more down will not compile). Casting doesn’t work sideways, only
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

To add a variable to a subclass"s constructor it should always be after the call to the superclass"s constructor, never before. Private fields in a parent class can only be accessed in a subclass via accessor methods. The protected keyword allows subclasses to see the information, but outside classes cannot (protected is rarely used); it is an intermediate level of privacy between public and private. The super keyword is only necessary in a subclass when the method being called has been overridden. Any object can be stored in a variable of type object. Polymorphism the same code can be used for several objects. A variable of type t can hold an object of any subclass of t. The employee1 object can only use employee methods, not lawyer methods. However, it will use any overridden methods from lawyer. A lawyer or secretary class can be passed as a parameter for a method that takes in an employee type.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions