0% found this document useful (0 votes)
235 views

Week6 PDF

The document discusses polymorphism in object-oriented programming. It explains that the protected access modifier allows subclasses to access protected fields and methods directly, while abstract classes define abstract methods but contain no implementation, requiring subclasses to implement them. Polymorphism is demonstrated through subclasses overriding inherited methods, while still allowing superclass reference variables to access the subclass's specific implementation through late binding.

Uploaded by

TeaShrew
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
235 views

Week6 PDF

The document discusses polymorphism in object-oriented programming. It explains that the protected access modifier allows subclasses to access protected fields and methods directly, while abstract classes define abstract methods but contain no implementation, requiring subclasses to implement them. Polymorphism is demonstrated through subclasses overriding inherited methods, while still allowing superclass reference variables to access the subclass's specific implementation through late binding.

Uploaded by

TeaShrew
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

protected keyword Polymorphism

casting

abstract classes & methods final classes & methods

CST8284

protected keyword

when applied to fields, permits subclasses access to fields directly


"access" means both read & write instead of using getter/setter careful: direct access will bypass "business rules" coded in getter/setter

can also apply to methods


instead of public or private e.g. permit only subclass(es) to use getter/setter consider BankExample: setBalance() is not exposed to end-user but subclasses can use/override it
CST8284

consider a superclass and a subclass


e.g. Employee and Manager var of type superclass can refer to instance of subclass
because a Manager "is an" Employee, called an "upcast"
Employee e2 = m; Employee e = new Employee(); Manager m = new Manager();

what about the reverse ?


runtime error because e refers to an Employee
CST8284

Manager m2 = (Manager) e2; // "downcast" Manager m3 = (Manager) e; // runtime error

use instanceof to check first

inherited and overridden methods


can be accessed by var of type superclass more specific behaviour method is overridden called "late binding" or "polymorphic method lookup"

if subclass has additional methods/fields


access requires variable of subclass type e.g. var of type Employee can access methods/fields defined in Manager such as getBonus() using a superclass var to refer to subclass instance

"upcasting" always permitted

"downcasting" use instanceof before cast


object must be instance of subclass to avoid runtime error

CST8284

consider Employee class

wish to create HourlyEmployee and SalariedEmployee each with specific getPay() method later, there may be more subclasses with different getPay() can we "postpone" definition of getPay() until later subclasses solution: declare method getPay() as an "abstract method"
using the abstract keyword public abstract double getPay(); but still ensure it gets coded ? but no getpay() in Employee

CST8284

abstract method

complete declaration, param signature, etc.


but no method body

abstract class

can not be private

a class with 1 or more abstract methods only used for inheritance can not be instantiated imposes coding requirement on subclass(es) if not coded, subclass is also abstract
CST8284

so a class that has at least one abstract method is called an abstract class
and must be defined with abstract keyword
public abstract class Employee { ... public abstract double getPay(); ... }

CST8284

abstract class can have mix of abstract and fully defined methods If subclass of abstract class does not define bodies for all of abstract methods

"concrete" class

then it is abstract also and use must use abstract in its defintion

has no abstract methods all methods are fully defined


(also called "concrete" methods

CST8284

Although we can not instantiate abstract class


it's still a class still has constuctors
coded by us or default from compiler

and subclasses must still use super()

can be used as a type for variables, upcasts, passed as args, etc

or compiler will call superclass no-arg constructor common use: var of type (abstract) superclass to refer to subclass
Employee e = new HourlyEmployee(); List myList = new ArrayList();
CST8284

have already seen final keyword with variables


once value is set, it can not be modified often use to create "constants"

final can also be used with definition of methods


such methods can not be overridden in subclass

final can also be used with definition of class


means class can be extended can be used as a superclass for a subclass effectively "stops" further inheritance

CST8284

You might also like