I C S E
Introduction to Computer Science for Engineers
L-3: Object-Oriented Design
Introduction to Computer Science for Engineers L-3: Object-Oriented - - PowerPoint PPT Presentation
I C S E Introduction to Computer Science for Engineers L-3: Object-Oriented Design ICSE Exercise Update Working during the weekend is not very comfortable :/ Hence, we change the rhythm of publishing/submitting exercises tasks
I C S E
L-3: Object-Oriented Design
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
2
Exercise Update
exercises
—> on monday evening or tuesday morning
corresponding exercise) at 2 pm!
Some Old Slides
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
4
Source Code: Welcome
/* A simple Java Program,which says Welcome to ICSE */ public class Welcome { public static int var; public static void say(String s) { System.out.print(“Welcome to”+s); } public static void main(String[] argv) { say(”ICSE!"); } } the class name (Save code as Welcome.java) a variable a method The main method (The entry point while executing the program) a comment
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
5
Source Code: Welcome
/* A simple Java Program,which says Welcome to ICSE */ public class Welcome { public static int var; public static void say(String s) { System.out.print(“Welcome to”+s); } public static void main(String[] argv) { say(”ICSE!"); } } the class name (Save code as Welcome.java) a variable a method The main method (The entry point while executing the program) a comment public static void main(String[] argv) {
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
6
Methods
Signature:
Example:
Notes:
int à double
double à int
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
7
Methods
Signature:
Example:
Notes:
int à double
double à int
double i1 = max (1,2); … int i2 = max ( (int) d2, i1);
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
8
Content
Object-Oriented Design
Object-Oriented Design: Goals, Principles and Design Patterns
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
10
Object-Oriented Design: Goals
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
11
Object-Oriented Design: Goals
Robustness:
explicitly defined for its application
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
12
Object-Oriented Design: Goals
Robustness:
explicitly defined for its application
Adaptability:
conditions in its environment
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
13
Object-Oriented Design: Goals
Robustness:
explicitly defined for its application
Adaptability:
conditions in its environment
different hardware and operation system platforms
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
14
Object-Oriented Design: Goals
Robustness:
defined for its application
Adaptability:
its environment
different hardware and operation system platforms
Reusability:
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
15
Object-Oriented Design: Principles
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
16
Object-Oriented Design: Principles
Abstraction:
explaining their functionality
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
17
Object-Oriented Design: Principles
Abstraction:
their functionality
stored, the operations supported on them, and the types of parameters of the operations
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
18
Object-Oriented Design: Principles
Encapsulation:
a software system
no internal details for outsiders
details
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
19
Object-Oriented Design: Principles
Encapsulation:
software system
internal details for outsiders
Modularity:
functional units to reduce complexity
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
20
Object-Oriented Design: Design Patterns
Design patterns:
different situations.
Elements of a design pattern:
Classes and Objects: Basic Concepts
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
22
Classes and Objects
Class: a template/blue print, defining the data which the object stores and the
methods for accessing and modifying that data Classes can contain any of the following variable types:
Methods represent operations that can act on the data associated
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
23
Classes and Objects
Class: a template/blue print, defining the data which the object stores and
the methods for accessing and modifying that data
public class Counter { public int count; // instance variable public Counter( ) { } // method (default constructor) public Counter(int a){ // method (constructor with one parameter) int count = a; // local variable } public void increment( ){count++;} // change the instance variable public void increment(int a){count+=a;} // change the instance variable public int getCount( ) {return count;} // an accessor method } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
24
Classes and Objects
Keyword this
with the same name
super(values);
public class Counter { public int count; // instance variable public Counter( ) { // method (default constructor) this(0); // invoke one-parameter constructor with value zero } public Counter(int a){ // method (constructor with one-parameter) this.count = a; // set the instance variable equal to parameter } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
25
Classes and Objects
Object: is an instance of a class which have a state and behavior
variables are initialized to standard default values
public class Example{ public static void main(String[ ] args) { Counter c; // declares a variable; Counter d = new Counter(3); // creates a new object; assigns reference c = new Counter(); d = c; // assigns d to reference the same object as c } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
26
Classes and Objects
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
27
Classes and Objects
The Dot Operator:
public class Example{ public static void main(String[ ] args) { Counter countVariable = new Counter(); // creates a new object countVariable.increment(); // call a method countVaraible.count = 3; // call a variable countVaariable.increment(2); // call a method } } ObjectReference = new Constructor(); //create an object ObjectReference.variableName; //call a variable ObjectReference.methodName(); //call a method
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
28
Classes and Objects
Overloading:
Counter c = new Counter( ); // creates a new object; assigns reference c.increment(); // increases its value by 1 c.increment(3) // increases its value by 3 public class Counter { public int count; // instance variable public Counter( ) { } // method (default constructor) public void increment( ){count++;} // change the instance variable public void increment(int a){count+=a;} // change the instance variable }
Classes and Objects: Access Modifier
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
30
Classes and Objects
Access Modifier:
defined aspect
defined aspect
public data type variable [ = value][, variable [= value] …]; public returnType methodeName (parameter list) {} private data type variable [ = value][, variable [= value] …]; private returnType methodeName (Parameter List) {} protected data type variable [ = value][, variable [= value] …]; protected returnType methodeName (Parameter List) {}
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
31
Classes and Objects
Access Modifier – public
public class Example { public static void main( String[] args) { Counter c = new Counter(); c.count = 42; // count is visible; dot notation can be used int temp = c.getCount(); // method is visible; temp will be 42 } public class Counter { // classes are allowed to construct new instances public int count; // anyone can access this instance variable public Counter(){} public int getCount() { return count; } // other can call to that method }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
32
Classes and Objects
Access Modifier – private
public class Example { public static void main( String[ ] args) { Counter c = new Counter(); c.count = 42; // count is not visible —> ERROR int temp = c.getCount(); // method is not visible —> ERROR } public class Counter { private int count; // only methods of the same class can access this public Counter(){} private int getCount() { return count; } //count is visible }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
33
Classes and Objects
Access Modifier – protected
that are designated as subclasses of the given glass
public class Example { public static void main( String[ ] args) { Counter c = new Counter(); c.count = 42; // count is not visible —> ERROR int temp = c.getCount(); // method is not visible —> ERROR } public class Counter { protected int count; // only methods of the same class can access this public Counter(){} protected int getCount() { return count; } //count is visible }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
34
Classes and Objects
Non Access Modifier:
instances created for the class.
access modifier static data type variable [ = value]; access modifier static returnType methodeName (parameter list) {} public static final int CONSTANT = 254;
Back to the Algorithm
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
36
Algorithm: Average
public class Average{ public static double getAverage(int[] data){ int counter = data.length; return (double)linearSum(data, counter)/ (double)counter; } public static int linearSum(int[] data, int n) { if (n == 0) return 0; else return linearSum(data, n-1)+data[n-1]; } public static void main(String[] args) { int[] data = {7,8,5,3,7,7,6,9}; System.out.println(getAverage(data)); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
37
Algorithm: Class Average
public class Average{ }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
38
Algorithm: Class Average
public class Average{ public int[] data; }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
39
Algorithm: Class Average
public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
40
Algorithm: Class Average
public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } public double getAverage() { int n = this.data.length; return (double)linearSum(this.data, n)/ (double)n; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
41
Algorithm: Class Average
public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } public double getAverage() { int n = this.data.length; return (double)linearSum(this.data, n)/ (double)n; } public static int linearSum(int[] data, int n) { if (n == 0) return 0; else return linearSum(data, n-1)+data[n-1]; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
42
Algorithm: Class Test
public class Test{ }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
43
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
44
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
45
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
46
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
47
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();
System.out.println("Average: " + object2.getAverage()); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
48
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();
System.out.println("Average: " + object2.getAverage()); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
49
Algorithm: Class Test
public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();
System.out.println("Average: " + object2.getAverage()); int sum = Average.linearSum(dataSet1, 4); } }
Object-Oriented Design: Inheritance
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
51
Object-Oriented Design: Inheritance
Definition:
properties (methods and fields) of another.
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
52
Object-Oriented Design: Inheritance
Superclass aka base class, parent class
Subclass aka derived class, child class
classes) of other.
superclass can be invoked from the subclass
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
53
Object-Oriented Design: Inheritance
Example: Cal (Superclass) Example: My_Cal (Subclass)
public class Cal{ int z; public int add(int x, int y){ return x+y; } } public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
54
Object-Oriented Design: Inheritance
Example: Cal (Superclass) Example: My_Cal (Subclass)
public class Cal{ int z; public int add(int x, int y){ return x+y; } } public class My_Cal extends Cal{ public int mult(int x, int y){ z=x*y; return z; } } mult() int z add()
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
55
Object-Oriented Design: Inheritance
Polymorphism:
reference is used to refer to a child class object
any direct or indirect subclass of that type
actual type of the referenced object (not the declared type). superclass object = new subclass();
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
56
Object-Oriented Design: Inheritance
Example: My_Cal (Subclass)
public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } } public class Test_My_Cal{ public static void main(String args[]){ int a=20, b=10; My_Cal object = new My_Cal(); //create an object of My_Cal int c = object.add(a,b); int d = object.mult(a,b); } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
57
Object-Oriented Design: Inheritance
Example: My_Cal (Subclass)
public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } } public class Test_Cal{ public static void main(String args[]){ int a=20, b=10; Cal object = new My_Cal(); //create an object of Cal int c = object.add(a,b); //call method add() int d = object.mult(a,b); //ERROR can not call method } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
58
Object-Oriented Design: Inheritance
Keyword super
subclass, if they have same names
super(values); super.variable super.method();
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
59
Object-Oriented Design: Inheritance
Example: Event (Superclass) Example: Lecture (Subclass) :
public class Event{ public int num; public Event(int num){ this.num=num; } public void getParticipantNum(){ System.out.println(num) } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //call constructor of the Superclass super.num--; //call variable of the Superclass super.getParticipantNum(); //call method of the Superclass } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
60
Object-Oriented Design: Inheritance
Access Modifier:
aspect
public data type variable [ = value][, variable [= value] …]; public returnType methodeName (parameter list) {} private data type variable [ = value][, variable [= value] …]; private returnType methodeName (Parameter List) {} protected data type variable [ = value][, variable [= value] …]; protected returnType methodeName (Parameter List) {}
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
61
Object-Oriented Design: Inheritance
Example: Event (Superclass) Example: Lecture (Subclass) :
public class Event{ private int num; private Event(int num){ this.num=num; //num is visible } private void getParticipantNum(){ System.out.println(num) //num is visible } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //Error not visible super.num--; //Error not visible super.getParticipantNum(); //Error not visible } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
62
Object-Oriented Design: Inheritance
Example: Event (Superclass) Example: Lecture (Subclass) :
public class Event{ protected int num; protected Event(int num){ this.num=num; //num is visible } protected void getParticipantNum(){ System.out.println(num) //num is visible } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //call constructor of the Superclass super.num--; //call variable of the Superclass super.getParticipantNum(); //call method of the Superclass } }
Object-Oriented Design: Types of Inheritance
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
64
Object-Oriented Design: Inheritance
Single Inheritance: Multi Level Inheritance:
public class A{ … } public class B extends A{ … } public class A{ … } public class B extends A{ … } public class C extends B{ … }
Class A Class A Class A Class B Class C
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
65
Object-Oriented Design: Inheritance
Hierarchical Inheritance: Multiple Inheritance:
public class A{ … } public class B extends A{ … } public class C extends A{ … }
Class A Class B Class C Class C Class A Class B
public class A{ … } public class B{ … } public class C extends A, B { … }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
66
Object-Oriented Design: Inheritance
Hierarchical Inheritance: Multiple Inheritance:
public class A{ … } public class B extends A{ … } public class C extends A{ … }
Class A Class B Class C Class C Class A Class B
public class A{ … } public class B{ … } public class C extends A, B { … }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
68
Object-Oriented Design: Overriding
Overriding:
the original overridden method in the superclass
method's access level
modifiers returnType methodName (parameter list) {}
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
69
Object-Oriented Design: Overriding
Method Overriding:
public class Figure { public float area () { return -1.0f; } } class Rectangle extends Figure { //extend class Figure private float w, h; Rectangle (float w, float h) { this.w = w; this.h = h; } public float area () { return w*h; } //overriding method area public float area (float w) { return w*h; } //overloading } class Circle extends Figure { //extend class Figure private float r; Circle ( float r) { this.r = r; } public float area () {return r * r * 3.1415;} //overriding }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
70
Object-Oriented Design: Overriding
Method Overriding:
class Rectangle extends Figure { public float area () { return w*h; } . . . class Circle extends Figure { public float area () { return r*r *3.14159265; } . . . Rectangle r = new Rectangle (2 ,3); Circle c = new Circle (1); float a = r.area(); // a = 6 float b = r.area(3); // b = 9 a = c.area(); // a = 3.14159265 Figure f = null; f = r; a = f.area(); // a = 6 f = c; a = f.area(); // a = 3.14159265
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
72
Object-Oriented Design: Interfaces
Interface:
interface
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
73
Keyword: interface Keyword: implements
public interface Lecture{ public string getName(); public int getID(); } public class ICSE implements Lecture{ private string name; private int id; private string descript; public ICSE(string n, int i, string desc){ name = n; id = i; descript = desc; } public string getName(){ return name; } public int getID () {return id;} public string getDescription() {return descript} }
Object-Oriented Design: Interfaces
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
74
Keyword: implements
Multiple Inheritance for Interfaces:
Interface C Interface A Interface B
public interface A{ … } public interface B{ … } public interface C extends A, B{ … }
Object-Oriented Design: Interfaces
class ICSE implements Lecture, Exercise, Exam { … }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
76
Abstract Class
an interface
implementation of those method - abstract method
provide implementations to the abstract methods in it
abstract methods
Object-Oriented Design: Abstract Classes
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
77
Keyword: abstract Keyword: extends
public abstract class Lecture { private string name; public Lecture(string name){this.name = name;} public int getName() {return this.name;} public abstract int getID(); } public class ICSE extends Lecture{ private int id; public ICSE (string name, int id) { super(name); this.id=id; } public int getID () {return id;} }
Object-Oriented Design: Abstract Classes
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
79
Object A: ObjectPairA a = new ObjectPairA(“ABC”, 2); Object B: ObjectPairB b = new ObjectPairB(“ABC”, 2.0);
public class ObjectPairA { String first; int second; public ObjectPair(String a, int b) { // constructor first = a; second = b; } }
Object-Oriented Design: Generics
public class ObjectPairB { String first; float second; public ObjectPair(String a, float b) { // constructor first = a; second = b; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
80
Generics
a variety of data types while often avoiding the need for explicit casts.
Generics Framework
specified at run time
Object-Oriented Design: Generics
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
81
Generics Methods
different types
angle brackets: < >
placeholders for the types of the arguments passed to the generic method, which are known as actual type parameters
reference types, not primitive types (like int, double and char) à wrapper classes: Integer, Double and Character
Object-Oriented Design: Generics
modifiers < A > methodName (parameter list) {}
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
82
Object-Oriented Design: Generics
Method: minFunction(int varA, int varB) Generics Method: minFunction(A varA, A varB)
public static < A > A minFunction(A varA, A varB) { if (varA > varB) return varB; else return varA; } public static int minFunction(int varA, int varB) { } public static void main (String args[]) { Integer a = 3; Integer b = 5; Integer c = minFunction(a, b) }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
83
Method: biggerThan(int varA, int varB, string varC) Generics Method: biggerThan(A varA, B varB)
Object-Oriented Design: Generics
public < K,L > void biggerThan(K varA, K varB, L varC) { if (varA > varB) System.out.println(varC); } public static void biggerThan(int varA, int varB, string varC){} public static void main (String args[]) { biggerThan(1, 2, “false”); biggerThan(2.5, 2.0, “true”); }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
84
Method: minFunction(int[] varA) Generics Method: minFunction(A[] varA)
Object-Oriented Design: Generics
public static < A > A minFunction(A[] varA) { if (varA[0] >= varA[1]) return varA[0]; else return varA[1]; } public static int minFunction(int[] varA) { } public static void main (String args[]) { Integer[] a = {3,5}; int c = minFunction(a) }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
85
Generic Class
declaration, except that the class name is followed by a type parameter section
Object-Oriented Design: Generics
modifiers class className < A > {} public class ObjectPair < D,A >{ private D first; private A second; public ObjectPair(D a, A b) { // constructor first = a; second = b; } public D Getfirst() { return first; } }
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
86
Summary
Object-Oriented Design
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
87
Recommended Reading
Chapter 2: Object-Oriented Design
Data Structures & Algorithms in Java
by Michael T. Goodrich, Roberto Tamassia and Michael H. Goldwasser
ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)
ICSE
88
Coming up…
Fundamental Algorithms