encapsulation, inheritance & polymorphism. oop properties encapsulation the process of creating...

14
Computer Science Encapsulation, Inheritance & Polymorphism

Upload: annice-harrison

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Computer Science

Encapsulation, Inheritance & Polymorphism

Page 2: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

OOP PropertiesEncapsulation

­ The­process­of­creating­programs­so­that­information­within­a­class­is­not­accessible­from­an­outside­class.

­ The­attributes­and­behaviours­of­that­class­cannot­be­changed­from­outside­of­the­class

Inheritance­ The­ability­to­create­new­classes­that­are­based­on­existing­ones

Polymorphism­ The­ability­to­create­methods­that­perform­a­general­function­,­which­automatically­adapts­itself­to­work­with­objects­of­different­classes

Page 3: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

EncapsulationEncapsulation­means­hiding­the­details­of­an­object­from­the­other­parts­of­a­program.­

The­object­can­be­used­only­through­its­access methods,­which­are­carefully­written­to­keep­the­object­consistent­and­secure.­

Example:­The­inner­working­of­a­TV­are­encapsulated­by­the­screen­and­plastic­frame.­­The­remote­control­or­user­interface­allows­you­to­access­the­TV.­

In­Java,­the­user­interface­is­controlled­by­the­access modifiers

Page 4: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Access Control ModifiersPrivate­Access­Modifier

­ A­member­in­the­class­can­only­by­the­methods­of­that­class.

Public­Access­Modifier­ Allows­a­member­in­the­class­to­be­accessed­by­code­outside­of­that­class

Page 5: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Encapsulation at WorkClass CheckingAccount { private String accountNumber; private String accountHolder; private int balance; private int useCount = 0; public CheckingAccount( String accNumber, String holder, int start ) { . . . . } private void incrementUse() { .. . . } public int getBalance() { . . . . } public void processDeposit( int amount ) { . . . . } public void processCheck( int amount ) { . . . . } }

Only accessed in CheckingAccount Class

Accessed Outside of the Class

Page 6: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

InheritanceInheritance­enables­you­to­define­a­new­class­based­upon­an­existing­class.­

The­new­class­is­similar­to­the­existing­class,­but­has­additional­member­variables­and­methods.­

This­makes­programming­easier­because­you­can­build­upon­an­existing­class­instead­of­starting­out­from­scratch.­

Page 7: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Parents and Children­The­class­that­defines­the­new­class­is­known­as­

­ The­parent­class­ The­superclass­ The­base­class

A­class­that­is­based­on­the­parent­class­is­known­as­ The­child class­ The­subclass­ The­derived­class.

The­child­classes­inherits­the­characteristics­of­the­parent­class

In­Java­there­is­one­parent­for­multiple­childs.

Page 8: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Inheritance between classes not objectsRemember­classes­are­like­blueprintsYou­can­have­many­objects­for­the­same­classWhat­this­means

­ Parent­and­Child­classes­can­have­several­objects­eachExample:

­ Parent­class­``Automobile``­has­one­object Joe`s­Car

­ Child­Class­``Ford``­has­two­objects Mary`s­Ford,­Bob`s­Ford

Note: A class may be the parent for a child class and may be a child of another class.­class childClass extends parentClass {...}

Page 9: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Using Inheritanceclass Video { String title; // name of the item int length; // number of minutes boolean avail; // availability} class Movie extends Video { String director; // name of the director String rating; // G, PG, R, or Xpublic Movie( String ttl, int lngth, String dir, String rtng ) { // constructor super( ttl, lngth ); // uses the super class's constructor director = dir; rating = rtng; // initialize what's new to Movie } }

member­ ­

title­ inherited­from­Video

length­ inherited­from­Video

avail­ inherited­from­Video

show()­ inherited­from­Video

director defined­in­Movie­

rating­ defined­in­Movie­

Note: use reserved super() class to ensure inheritance of parent class –always place first in constructor –anywhere in method

Page 10: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Polymorphism & the abstract class­Recall­that­polymorphism­is­ability­to­create­methods­that­perform­a­general­function­,­which­can­then­adapt­to­work­with­other­object­in­different­classes

The­abstract class­is­a­special­class­that­is­never­instantiated.­Its­purpose­is­to­be­a­parent­to­several­related­classes.­The­children­classes­inherit­from­the­abstract­parent­class.­­ abstract­class­ClassName

Page 11: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Abstract MethodsAbstract­Methods­can­be­included­in­the­abstract­classThe­purpose­of­the­abstract method is­to­ensure­that­all­types­objects­can­use­the­methods­of­the­parent­class.

Example­ Class­cards­–­has­properties­of­greeting­cards­ Various­types­of­Cards­–­Birthday,­Mother’s­Day­ The­greeting­for­a­birthday­card­will­be­different­from­the­greeting­of­the­Mother’s­day­card

­ Using­the­Abstract­method­ensures­the­correct­greeting­is­on­the­correct­object

An­abstract method­has­no­body.­(It­has­no­statements.)­ ­It­declares­an­access­modifier,­return­type,­and­method­signature­followed­by­a­semicolon.­

Page 12: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Example of the Abstractabstract class Card { String recipient; // name of who gets the card public abstract void

greeting(); // abstract greeting() method }

Page 13: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Polymorphism without the AbstractPolymorphism­means­"having­many­forms.“For­example:­a­single­variable­might­be­used­with­several­objects­of­related­classes­at­different­times­in­a­program.­

When­the­variable­is­used­to­invoke­a­method,­exactly­which­method­is­run­depends­on­the­object­that­the­variable­currently­refers­to.­

Page 14: Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation The process of creating programs so that information within a class is not accessible

Examplepublic class CardTester { public static void main ( String[] args ) { Card card = new Holiday( "Amy" ); card.greeting(); //Invoke a Holiday greeting() card = new Valentine( "Bob", 3 ); card.greeting(); //Invoke a Valentine greeting() card = new Birthday( "Cindy", 17 ); card.greeting(); //Invoke a Birthday greeting() } }