| |
Mihai Bâce
mihai.bace@inf.ethz.ch
1-Nov-19 1
Informatik II
Tutorial 6
Mihai Bâce
Informatik II Tutorial 6 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation
Informatik II Tutorial 6 Mihai Bce mihai.bace@inf.ethz.ch Mihai Bce | | 1-Nov-19 1 Overview Debriefing Exercise 5 Briefing Exercise 6 Mihai Bce | | 1-Nov-19 2 U05 Some Hints Variables & Methods beginWithLowerCase,
| |
mihai.bace@inf.ethz.ch
1-Nov-19 1
Mihai Bâce
| | 1-Nov-19 Mihai Bâce 2
| | 1-Nov-19 Mihai Bâce 3
| | 1-Nov-19 Mihai Bâce 4
| | 1-Nov-19 Mihai Bâce 5
| | 1-Nov-19 Mihai Bâce 6
| | 1-Nov-19 Mihai Bâce 7
| | 1-Nov-19 Mihai Bâce 8
Person Name Age Address PhoneNumber toString getName getAge getAddress getPhoneNumber setAddress(newAddress) setPhoneNumber(newPhoneNumber)
| | 1-Nov-19 Mihai Bâce 9
public class Person { private String name; private int age; private String address; private String phone; public Person(String name, int age, String address, String phone) { this.name = name; this.age = age; this.address = address; this.phone = phone; } public String toString() { return getName() + " is " + getAge() + "old and lives in " + getAddress(); } public String getName() { return name; } public int getAge() { return age; } public String getAddress() { return address; } public String getPhoneNumber() { return phone; } .....
| | 1-Nov-19 Mihai Bâce 10
Student Name Age Address PhoneNumber
toString() getName() getAge() getAddress() getPhoneNumber()
setAddress(newAddress) setPhoneNumber(newPhoneNumber) Person Name Age Address PhoneNumber toString() getName() getAge() getAddress() getPhoneNumber() setAddress(newAddress) setPhoneNumber(newPhoneNumber)
| | 1-Nov-19 Mihai Bâce 11
Student
public class Student extends Person { private String legi; public Student(String name, int age, String address, String phone, String legi){ super(name, age, address, phone); this.legi = legi; } public String toString() { return getName() + " is " + getAge() + "old, lives in " + getAddress() + " and has legi-nr.: " + getLegi(); } public String getLegi() { return legi; } }
| | 1-Nov-19 Mihai Bâce 12
| | 1-Nov-19 Mihai Bâce 13
| | 1-Nov-19 Mihai Bâce 14
| | 1-Nov-19 Mihai Bâce 15
Cat c = new Cat(); System.out.println(c); Mammal m = c; // upcasting System.out.println(m); /* This printed: Cat@a90653 Cat@a90653 */
Mammal, it's just being labeled Mammal right now. This is allowed, because Cat is a Mammal.
| | 1-Nov-19 Mihai Bâce 16
| | 1-Nov-19 Mihai Bâce 17
Cat c1 = new Cat(); Animal a = c1; //automatic upcasting to Animal Cat c2 = (Cat) a; //manual downcasting back to a Cat
| | 1-Nov-19 Mihai Bâce 18
| | 1-Nov-19 Mihai Bâce 19
Person p = new Person(...); Student s = new Student(...); Employee e = new Employee(...);
Person ps = s Person pe = e Student sp = p Student sps = ps Student dsps = (Student) ps Employee deps = (Employee) ps
Ok (casting from base class to derived class)
Compile error Compile error Runtime error (ps points to object of class Student)
| | 1-Nov-19 Mihai Bâce 20
p instanceof Person p instanceof Student s instanceof Person s instanceof Student
Person p = new Person(...); Student s = new Student(...); Employee e = new Employee(...);
True True True False
| | 1-Nov-19 Mihai Bâce 21
public class MyClass { ...... }
public class MyClass extends Object { ...... }
| | 1-Nov-19 Mihai Bâce 22
Quelle: sun.com
| | 1-Nov-19 Mihai Bâce 23
| | 1-Nov-19 Mihai Bâce 24
| | 1-Nov-19 Mihai Bâce 25
| | 1-Nov-19 Mihai Bâce 26
| | 1-Nov-19 Mihai Bâce 27
| | 1-Nov-19 Mihai Bâce 28
Interface
code
code)
public abstract
interfaces
Abstract class
complete code, default code, and/or just stubs that have to be overridden
abstract
abstract class
| | 1-Nov-19 Mihai Bâce 29
public interface IStack { int size(); void push(Object obj); Object pop(); Object peek(); boolean empty(); } public class MyStack implements IStack { private int size; public int size() { return size; } public void push(Object obj) { ... } ... }
| | 1-Nov-19 Mihai Bâce 30
public abstract class BaseStack implements IStack { public abstract int size(); public abstract void push(Object obj); public abstract Object pop(); public Object peek() { Object top = pop(); push(top); return top; } public boolean empty() { return size() == 0; } } public class MyStack extends BaseStack { private GenericList first; public Object peek() { return first.value; } ... }
| | 1-Nov-19 Mihai Bâce 31
| | 1-Nov-19 Mihai Bâce 32
| | 1-Nov-19 Mihai Bâce 33
http://de.wikipedia.org/wiki/Klassendiagramm
A keyword represents an interface Notation for the dependance of the instantiation of the
instantiates the iSensor interface Keyword Property Section with attributes (detailed representation) Section with
representation Private client as a specialized Person
| | 1-Nov-19 Mihai Bâce 34
A factory method builds an object which implements a certain interface, but the inner functionality of the object is hidden. à Programmer 1 implements different lists which implement the IList interface. à Programmer 2 uses lists but doesn’t want to know about the functionality. When Programmer 1 writes a new implementation, Programmer2 has to rewrite all lines of new ListA() as new ListB(). à Programmer 1 puts a factory method at disposal and Programmer 2 can always call for example Factory.giveMeNewList() and gets an object from the newest implementation of the IList interface.
| | 1-Nov-19 Mihai Bâce 35
ScriptS.GE
| | 1-Nov-19 Mihai Bâce 36
public interface Comparable { boolean smallerThan(Comparable rhs); }
| | 1-Nov-19 Mihai Bâce 37
| | 1-Nov-19 Mihai Bâce 38
Image