Top Banner
1 Introduction to CS301
31

1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

1

Introduction to CS301

Page 2: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

2

Agenda

• Syllabus

• Schedule

• Lecture: the management of complexity

Page 3: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

3

Class Description

• Data structure: container to store, organize and manipulate data elements.

• Examples ?• Standard data structures in

Java. • Implementing new data

structures.

Page 4: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

4

Class Description        Object-oriented programming in Java.        Abstract data types.        Algorithms analysis.        Execution time analysis.        Vectors.        Lists.        Stacks and queues.        Deques.        Sets.        Trees.        Matrices.        Maps.

Page 5: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

5

Management of Complexity

• Control of complexity– Large programs development

requires organization, method, discipline, management skills.

– Programming in the small ( small team, design algorithms) / programming in the large ( large team, communication).

– Java as an object-oriented programming language.

Page 6: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

6

Management of Complexity

• Abstraction, information hiding, layering– Abstraction is the purposeful

suppression, or hiding, of some details of a process or an artifact in order to bring more clearly other aspects or details.

– Information hiding is the purposeful omission of details in the development of an abstract representation.

– Layering is the imposition of structure over a model: layers of specialization, division into parts.

Page 7: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

7

Management of Complexity

• Division into parts– Encapsulation and

interchangeability– Interface (what) and

implementation (how)– The service view: what service

does each component provide ?– Repetition and recursion

(mathematical induction)

Page 8: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

8

Management of Complexity

• Composition– Begin with some primitive forms,

and add rules for combining forms to create new forms.

– “Has-a” relationship.

Page 9: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

9

Management of Complexity

• Layers of specialization– “Is-a” relationship – Inheritance

• Multiple views– List (implementation, speed,

fields, methods, …)

Page 10: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

10

Management of Complexity

• Patterns– A pattern is a software

component described in a way that permits it to reuse it for similar problems.

– Example: Adapter is a service that takes information from a Requester and transforms it o be understood by a Service provider.

Page 11: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

11

Composition

• Composition: composition reuses a class by creating a reference to it in another class.

• This is how we have been reusing classes until now.

Page 12: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

12

Composition• Example:

• The field ‘c’ reuses the class Customer by composition.

• The local variable ‘name’ reuses the class String by composition.

class Bank { Customer c; public static void main (String[] args) { String name = “Smith”; c = new Customer(name); }}

Page 13: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

13

Generalization

• The generalization relation is the relation between a superclass and a subclass where all instances of the subclass are instances of the superclass: ‘subclass’ is-a ‘superclass’.

• When a class G is more general than a class S, the set of all instances of G is a superset of the set of all instances of S.

• The inverse relation is the specialization.

Page 14: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

14

Generalization• Example: LivingThing

HumanBeingAnimal

Plant

Mammal Fish

specialization

Page 15: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

15

Abstraction• A related relation is the abstraction

relation.• A class G is more abstract than a

subclass S if it is described by less description elements, or features, among those describing S.

• If a class is more abstract, it is also more general, so there is an inclusion between these two concepts, although they are not identical.

Page 16: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

16

AbstractionBldg

heightwidthdepthheatType

AptBldg

heightwidthdepthheatTypenumAptsapts

less abstract

Bldg is more abstract than AptBldg because it has less description elements.

Page 17: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

17

Inheritance• Inheritance is the mechanism by

which a class knows the features of its superclass(es).

• Permits to regroup features in the superclass, and to have subclasses reuse the superclass description.

• Is at the basis of code reuse, components-based development.

Page 18: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

18

InheritanceBldg

heightwidthdepthheatType

AptBldg

heightwidthdepthheatTypenumAptsapts

less abstract

Bldg

heightwidthdepthheatType

AptBldg inherits Bldg features: height, width, depth, heatType

Bldg

heightwidthdepthheatType

AptBldg

numAptsapts

is-a

Bldg

heightwidthdepthheatType

Page 19: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

19

Inheritancepublic class Bldg { int height, width, depth; String heatType; public Bldg() {heatType = “electricity”;} public String getHeatType() {return heatType; }}public class AptBldg extends Bldg { int numApts; Apt[] apts;}

AptBldg inherits all public and protected fields and methods from Bldg. Private fields and methods are not inherited.

Page 20: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

20

Inheritance Rules

• Inheritance of constructors:– constructors are not inherited.

However, a class can access the constructors of its superclass by using super(), or super(10), … as the first statement in a constructor.

– whenever an instance is created, its superclass constructor is always called. If this is not done explicitly, the compiler inserts a super(); as the first statement.

Page 21: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

21

Inheritance Rules

public class A { public A(int g) {}}public class B extends A { public B() { super(5); }}…

B b1 = new B(9);

error

Page 22: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

22

Inheritance Rules• Inheritance of constructors:

– if a class declaration does not contain any constructor declaration, Java provides by default a constructor. This constructor is the following: public MyClass() {super();}.Attention: the empty constructor of the superclass must exist.

– if a class declaration contains a constructor declaration, Java does not provide the empty constructor.

Page 23: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

23

Inheritance Rules

public class A { public A(int g) {}}public class B extends A {// class definition does not provide any constructor}

error

Page 24: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

24

Inheritance Rules

• Single inheritance: a class can have only one direct superclass (but a class can reuse any number of interfaces).

• Inheritance permits to reuse definition of inherited class, while allowing for customization (adding new fields/methods, overriding inherited methods, ...).

Page 25: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

25

Inheritance Rules

• Methods overriding:– a method in an inherited class is overriden

in a subclass if a method in the inheriting class has the same name, number of arguments and types of arguments as the inherited method.

– if the overriden method returns void, the overriding method must also return void. The overriding method must return the same return type as the overriden method.

– Dynamic method look-up: methods are looked up at runtime.

Page 26: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

26

Inheritance Rules// in Bldg public void display() {System.out.println(“” + width +” “ + depth + “ “ + height); }// in AptBldg// display() in AptBldg overrides display() in Bldg public void display() { super.display(); System.out.println(“” + numApts); } …

{ AptBldg a = new AptBldg(); … a.display(); // displays width / depth / height / numApts ((Bldg) a).display(); // displays width / depth / height / numApts}

Page 27: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

27

Inheritance Rules

• Inheritance of fields:– fields with the same

name in a subclass and a

superclass are shadowed by the corresponding fields in

the subclass– example: firstName

Employee

firstNamelastNamesalaryemployeeIDpasswordhireDate

display()raiseSalary()

Manager

secretaryfirstName

display()raiseSalary()

Page 28: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

28

Inheritance Rules

• Inheritance of fields:– to access the fields in the

superclass (Employee):

– if there are several levels of hierarchy: A is superclass of B, and B is superclass of C

Manager m = new Manager(“John”, “Smith”);this.firstName; // returns “John”super.firstName; // returns null((Employee) this).firstName; // returns null

super.super.firstName; // ILLEGAL((A)this).firstName; // works

Page 29: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

29

Inheritance Rules

• Inheritance of class fields:– same rules as for instance fields

(shadowing).

• Inheritance of class methods:– no overriding, but shadowing.

Page 30: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

30

Inheritance Rules• Difference between overriding and shadowing

class A { int i = 1; int f() { return i;} static char g() { return ‘A’;}} class B extends A{ int i = 2; int f() { return -i;} static char g() { return ‘B’;}} …B b = new B();b.i; // returns 2b.f(); // returns -2B.g(); // returns BA a = (A) b; // upcastinga.i; // returns 1a.f(); // returns -2 !!! overriding != shadowingA.g(); // returns A

Page 31: 1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity.

31

Inheritance Rules• Example:

Create two subclasses of Account:– CheckingAccount (+field overdraft)– SavingsAccount (+field percent)