Você está na página 1de 10

protected keyword Polymorphism


abstract classes & methods final classes & methods


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

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

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


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


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

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(); ... }


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


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();

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