Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Introduction to CS301. 2 Agenda Syllabus Schedule Lecture: the management of complexity."— Presentation transcript:

1 1 Introduction to CS301

2 2 Agenda Syllabus Schedule Lecture: the management of complexity

3 3 Class Description Data structure: container to store, organize and manipulate data elements. Examples ? Standard data structures in Java. Implementing new data structures.

4 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.

5 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.

6 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.

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

8 8 Management of Complexity Composition –Begin with some primitive forms, and add rules for combining forms to create new forms. –“Has-a” relationship.

9 9 Management of Complexity Layers of specialization –“Is-a” relationship –Inheritance Multiple views –List (implementation, speed, fields, methods, …)

10 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.

11 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.

12 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); }

13 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.

14 14 Generalization Example:

15 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.

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

17 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.

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

19 19 Inheritance public 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.

20 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.

21 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

22 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.

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

24 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,...).

25 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.

26 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 }

27 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

28 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

29 29 Inheritance Rules Inheritance of class fields: –same rules as for instance fields (shadowing). Inheritance of class methods: –no overriding, but shadowing.

30 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 2 b.f(); // returns -2 B.g(); // returns B A a = (A) b;// upcasting a.i;// returns 1 a.f();// returns -2 !!! overriding != shadowing A.g();// returns A

31 31 Inheritance Rules Example: Create two subclasses of Account: –CheckingAccount (+field overdraft) –SavingsAccount (+field percent)


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

Similar presentations


Ads by Google