encapsulation, inheritance, composition. class methods can be either void or return can have...

13
Encapsulation, Inheritance, Composition

Upload: stephen-warren

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Encapsulation, Inheritance, Composition

Page 2: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Class MethodsCan be either void or returnCan have parameters or notMust be staticShould be publicKnow how to invoke (call)Be able to evaluate

Page 3: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Object MethodsCan be either void or returnCan have parameters or notMay be public or private (helper)Know how to invoke (call)Be able to evaluate

Page 4: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

ParametersActual vs FormalFormal

In the method definitionMust have data types

ActualIn the method callCannot have data typesMust match the formal parameters in type,

quantity and sequence

Page 5: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Void methodsCan have parameters or notHave the keyword void in the heading

Page 6: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Return methodsCan have parameters or notDo not have the keyword void in the headingHave a return statement in the body of the

method

Page 7: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

InheritanceDefinitionExamplesIs-ASuperclassSubclassExtendsConstructor of superclass called first

Page 8: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

superUsed in constructor to invoke the constructor

of the super classMust be used to pass information to the

constructor of the super classMust be used if the super class has no default

constructorMust be first in the constructorUsed in other methods to invoke methods of

the super class

Page 9: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Inheritance Examplepublic class superClass{ private int var1; public superClass(int v1) { var1 = v1; }}public class subClass extends superClass{

private int var2; public subClass(int v1, int v2) { super(v1); var2 = v2; }}

Page 10: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Multi-Level InheritanceKnow which constructor is called firstKnow how to pass information to the super

constructor and the super constructors of the classes above that.

Page 11: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Watch for tricky questionsWatch for tricky assignmentsKnow which constructor is called firstKnow which method is called – first it looks in

the sub class for the method, then the super class.

Page 12: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

DefinitionExamplesHas-AConstructor of container class called firstConstructor of contained class called last

Composition

Page 13: Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how

Composition Examplepublic class Part{ private int var1; public Part(int v1) { var1 = v1; }}public class Container {

private int var2; private Part var3; public subClass(int v1, int v2) { var2 = v2; var3 = new Part(v1); }}