08 class hierarchies - class hierarchies2
TRANSCRIPT
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
1/34
Class Hierarchies
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
2/34
Inheritance
• is the capability of a class to use the properties and methods ofanother class while adding its own functionality.
• An example of where this could be useful is with an employeerecords system.
• You could create a generic employee class with states and actions
that are common to all employees. Then more specific classescould be defined for salaried, commissioned and hourly employees.• The generic class is known as the parent or superclass or base
class! and the specific classes as children or subclasses orderi"ed classes!.
• The concept of inheritance greatly enhances the ability to reuse
code as well as making design a much simpler and cleaner process.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
3/34
#nheritance in $a"a
• $a"a uses the extends keyword to set the
relationship between a child class and a
parent class
• %or example Consider the following
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
4/34
&xample
• class Animal' (
private )tring type*
public Animal')tring aType! ( type + aType*
public )tring to)tring! ( return -This is a - type* class /og extends Animal' ( private )tring breed*
public /og)tring name!( super name!*
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
5/34
Consider
• 0e ha"e a less detailed abstract! class
Animal'
• And a more detailed subclassie.moreconcrete! /og
• /og extends that is pro"ides more detail!
• the superclass Animal'
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
6/34
Another &xample
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
7/34
Class 1
class 1 (
int x*
"oid set#t int n!
( x+n*"oid increase ! (
x+x'*
"oid triple ! (
x+x23**int return#t ! (
return x*
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
8/34
Class C
class C extends 1 (
• "oid triple ! (x+x3* 44 o"erride existing
method• "oid 5uadruple ! (x+x26* 44 new method
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
9/34
7sing these Classes
public class 8et9ich (
public static "oid main)tring:; args! (
1 b + new 1!*
b.set#t
b.increase!*
b.triple!*
)ystem.out.println b.return#t! !* 44 prints =
C c + new C!*
c.set#t
c.increase!*
c.triple!*)ystem.out.println c.return#t! !* 44 prints >
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
10/34
?ote about the class definition
• 0hen a class C extends class 1, C
automatically has all "ariables and
methods defined in class 1. think of it as
a internal copying mechanism!
• #f class C defines a "ariable or method
that has the same name in class 1, class
C@s definition o"errides 1@s.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
11/34
?otes
• #n the abo"e code, class C inherits all class 1@s "ariablesand methods. Class C o"errides the tripleB method, andadded a 5uadrupleB method.
• ther classes can extend Class C, as to inherit all
members and methods of class C which includes thosein 1!. #n this way, a tree hierarchy is formed.
• 0hen class C extends 1, we say that C is a subclassB of A, and A is the superclassB of C. This is calledinheritance, because C inherited from 1. Two or moreclasses can inherit from the same parent class. Howe"er,a class can only ha"e one parent. #n other words, in$a"a, you cannot subclass from multiple classes.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
12/34
Overloading, Overriding, and
Shadowing• "erloading, o"erriding, and shadowing are similar
concepts that can be easy to confuse. Although all threetechni5ues allow you to create members with the samename, there are some important differences.
• "erloaded members are used to pro"ide different"ersions of a property or method that ha"e the samename, but that accept different number of parameters, orparameters with different data types.
• "erridden properties and methods are used to replace
an inherited property or method that is not appropriate ina deri"ed class. "erridden members must accept thesame data type and number of arguments. /eri"edclasses inherit o"erridden members.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
13/34
)hadowing
• )hadowed members are used to locally
replace a member that has broader scope.
Any type can shadow any other type. %or
example, you can declare a property that
shadows an inherited method with the
same name. )hadowed members cannot
be inherited.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
14/34
)hadowing
• The main purpose of shadowing is to protect
the definition of your class members. The
base class might undergo a change that
creates an element with the same name asone you have already defined. If this
happens, the Shadows modifier forces
references through your class to be resolved
to the member you defined, instead of to thenew base class element.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
15/34
Abstract Classes
• #n class hierarchies, the superclass is more general thanits subclasses!.
• The superclass contains elements and propertiescommon to all of the subclasses.
• The pre"ious example was of a concrete superclassthat instance obDects can be created from.
• ften, the superclass will be set up as an abstract classwhich does not allow obDects of its prototype to be
created.• #n this case, only obDects of the subclass are used. To do
this the reser"ed word abstract is included in the classdefinition.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
16/34
&xample
• public abstract class Animal 44 class isabstract (
pri"ate )tring name*
public Animal)tring nm! 44 constructormethod ( name+nm*
public )tring get?ame! 44 regular method
( return name!* public abstract "oid speak!* 44 abstract
method E note no (
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
17/34
Abstract methods
• These are methods with no bodyspecification.
• )ubclasses must pro"ide the method
statements for their particular meaning.• #f the method was one pro"ided by the
superclass, it would re5uire o"erriding in
each subclass.• And if one forgot to o"erride, the applied
method statements may be inappropriate
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
18/34
Folymorphism
• Polymorphism is the capability of anaction or method to do different thingsbased on the obDect that it is acting upon.
This is the third basic principle of obDectoriented programming. "erloading ando"erriding are two types of polymorphism .?ow we will look at the third typeGdynamic method binding .
• Consider the following example
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
19/34
&xample
• public class Animal9eference• (• public static "oid main)tring args:;!• Animal ref 44 set up "ar for an Animal• Cow aCow + new Cow-1ossy-!* 44 makes specific obDects
• /og a/og + new /og-9o"er-!*• )nake a)nake + new )nake-&arnie-!*
• 44 now reference each as an Animal• ref + aCow*• ref.speak!*
• ref + a/og*• ref.speak!*• ref + a)nake*• ref.speak!*•
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
20/34
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
21/34
Class odifiers
• )o far we ha"e seen many examples of
such modifiers
• &.g.
• Fublic
• Abstract etc
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
22/34
&xercises
• 'G Create a )tudent Class
• ' b extend the student class to create a computerstudent class with a login and password. #ncludemethods to ask the user to input login and password and
see if they match.• < Create a 1ook class
•
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
23/34
• abstract class &mployee (
)tring name*
public abstract float calc#ncome!*
• class anager extends &mployee (
public "oid hire)tring who! (
)ystem.out.println who - hired by - name !*
public "oid fire)tring who! ()ystem.out.println who - fired by - name !*
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
24/34
ain program
public class anagerCheck (
public static "oid main)tring args:;!( anager me + new anager!*
me.hire-newbie-!*
me.fire-nobody-!*
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
25/34
Class Modifiers
• A class declaration may include class modifiers.• ClassModifiers: ClassModifier ClassModifiers
ClassModifier ClassModifier: one of
• public• protected• pri"ate• abstract
• static• final• strictfp
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
26/34
ield Modifiers
• FieldModifiers:• FieldModifier FieldModifiers• FieldModifier FieldModifier: one of
• public• protected• pri"ate• static
• final• transient• "olatile
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
27/34
strictfp Classes
• The effect of the strictfp modifier is to
make all float or double expressions within
the class declaration be explicitly %FEstrict
• This implies that all methods declared in
the class, and all nested types declared in
the class, are implicitly strictfp.
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
28/34
public
• Fublic access
• ost liberal kind of access
• Class or field is accessible e"erywhere
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
29/34
Fri"ate
• Fri"ate if its only "isible from inside the
class definition
• This is compromised somewhat by public
access methods
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
30/34
protected
• #f you create a protected data field or
method it can be used within its own class
or any subclass extended from it but not
from the outside world
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
31/34
static
• #f a field is declared static, there exists
exactly one incarnation of the field, no
matter how many instances possibly
Iero! of the class may e"entually becreated. A static field, sometimes called a
class variable, is incarnated when the
class is initialiIed
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
32/34
final ields
• A field can be declared final
• 1oth class and instance "ariables static
and nonEstatic fields! may be declared
final.
• &ffecti"ely final declares the "ariable to be
constant
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
33/34
Transient and "olatile
• 0e will go back to these
-
8/18/2019 08 Class Hierarchies - Class Hierarchies2
34/34