Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Similar documents
Informatik II (D-ITET) Tutorial 6

Informatik II Tutorial 6. Subho Shankar Basu

Informatik II (D-ITET) Tutorial 6. TA: Anwar Hithnawi, Distributed Systems Group, ETH Zürich

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Object-Oriented Programming (Java)

Week 5-1: ADT Design

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

What is Inheritance?

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Java Object Oriented Design. CSC207 Fall 2014

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CIS 110: Introduction to computer programming

Lecture 10. Overriding & Casting About

Create a Java project named week10

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Super-Classes and sub-classes

CS-202 Introduction to Object Oriented Programming

Software and Programming 1

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Inheritance and Polymorphism

Java: introduction to object-oriented features

Informatik II Tutorial 7. Subho Shankar Basu

type conversion polymorphism (intro only) Class class

Inheritance and Polymorphism. CS180 Fall 2007

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Principles of Object Oriented Programming. Lecture 4

Java Classes, Inheritance, and Interfaces

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Classes and Inheritance Extending Classes, Chapter 5.2

Inheritance. Transitivity

Java Fundamentals (II)

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Java Magistère BFA

25. Generic Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Chapter 5 Object-Oriented Programming

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Basic Object-Oriented Concepts. 5-Oct-17

Abstract class & Interface

OVERRIDING. 7/11/2015 Budditha Hettige 82

Distributed Systems Recitation 1. Tamim Jabban

Programming II (CS300)

ITI Introduction to Computing II

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Introduction to Inheritance

C08: Inheritance and Polymorphism

Distributed Systems Recitation 1. Tamim Jabban

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CISC370: Inheritance

CS 251 Intermediate Programming Inheritance

Introduction to Programming Using Java (98-388)

Practice for Chapter 11

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

ITI Introduction to Computing II

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

More about inheritance

Inheritance and Polymorphism

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

CMSC 132: Object-Oriented Programming II

Programming Exercise 14: Inheritance and Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Exam Duration: 2hrs and 30min Software Design

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Example: Count of Points

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

More On inheritance. What you can do in subclass regarding methods:

CLASS DESIGN. Objectives MODULE 4

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Object Oriented Programming. I. Object Based Programming II. Object Oriented Programming

Chapter 6 Introduction to Defining Classes

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CMSC 341. Nilanjan Banerjee

First IS-A Relationship: Inheritance

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Polymorphism. return a.doublevalue() + b.doublevalue();

Inheritance, Polymorphism, and Interfaces

1 Shyam sir JAVA Notes

Lecture 2: Java & Javadoc

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Rules and syntax for inheritance. The boring stuff

OBJECT ORİENTATİON ENCAPSULATİON

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Transcription:

Informatik II Tutorial 6 Mihai Bâce mihai.bace@inf.ethz.ch 05.04.2017 Mihai Bâce April 5, 2017 1

Overview Debriefing Exercise 5 Briefing Exercise 6 Mihai Bâce April 5, 2017 2

U05 Some Hints Variables & Methods beginwithlowercase, areverydescriptiveand uppercaseseparated amethodwhichhasaverylongname() Classes with capital letters: class MyClass{ } C++ notation: attributes start with m (mvalue, mnext), not the case with temporary and passed parameters (int tmp) Mihai Bâce April 5, 2017 3

U05.01 Linked List Basic operations Mihai Bâce April 5, 2017 4

U05.02 More Linked List operations Mihai Bâce April 5, 2017 5

U05.03 Sorting a Linked List Mihai Bâce April 5, 2017 6

Object Oriented Programming Mihai Bâce April 5, 2017 7

Class Person Person Name Age Address PhoneNumber tostring getname getage getaddress getphonenumber setaddress(newaddress) setphonenumber(newphonenumber) Attributes Accessors Mutators Getter and Setter Methods Mihai Bâce April 5, 2017 8

How do we implement class Person? 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; }... Mihai Bâce April 5, 2017 9

What about students? Student Name Age Address PhoneNumber Legi tostring() getname() getage() getaddress() getphonenumber() getlegi() setaddress(newaddress) setphonenumber(newphonenumber) Person Name Age Address PhoneNumber tostring() getname() getage() getaddress() getphonenumber() setaddress(newaddress) setphonenumber(newphonenumber) Mihai Bâce April 5, 2017 10

Student class Student defines a constructor calls the basis class constructor through the usage of super 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; } Mihai Bâce April 5, 2017 11

Inheritance Student extends Person Student can: Add new fields: legi Add new methods: getlegi() Override existing methods: tostring() Student cannot: Remove fields Remove methods Mihai Bâce April 5, 2017 12

Why inheritance? Better design Code reuse Code «maintenance» Abstraction of the real world Mihai Bâce April 5, 2017 13

Inheritance Mihai Bâce April 5, 2017 14

Upcasting Cat c = new Cat(); System.out.println(c); Mammal m = c; // upcasting System.out.println(m); /* This printed: Cat@a90653 Cat@a90653 */ Cat is still exactly the same Cat after upcasting, it didn't change to a Mammal, it's just being labeled Mammal right now. This is allowed, because Cat is a Mammal. Upcasting is done automatically, no need to do it manually Mihai Bâce April 5, 2017 15

Mihai Bâce April 5, 2017 16

Downcasting Cat c1 = new Cat(); Animal a = c1; //automatic upcasting to Animal Cat c2 = (Cat) a; //manual downcasting back to a Cat Downcasting must be done manually! Why? Multiple child classes Mihai Bâce April 5, 2017 17

Downcasting Mihai Bâce April 5, 2017 18

Static & Dynamic Casting Person Student Employee 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 ok Compile error Compile error Ok (casting from base class to derived class) Runtime error (ps points to object of class Student) Mihai Bâce April 5, 2017 19

Static & Dynamic Casting Person Student Employee Person p = new Person(...); Student s = new Student(...); Employee e = new Employee(...); p instanceof Person p instanceof Student s instanceof Person s instanceof Student True False True True Mihai Bâce April 5, 2017 20

Object class in Java Is a superclass for all other classes defined in Java's class libraries, as well as for user-defined Java classes. This does not include primitive types (char, int, float, etc.): they are not classes! When a class is defined in Java, the inheritance from the Object class is implicit, therefore: public class MyClass {... } is equivalent to: public class MyClass extends Object {... } Mihai Bâce April 5, 2017 21

Object class in Java Object Person Student Employee Quelle: sun.com Mihai Bâce April 5, 2017 22

Visibility rules private members Private members in the base class are not accessible to the derived class, and also not to anyone else protected members Protected members are visible to methods in a derived class and also methods in classes in the same package, but not to anyone outside public members Everyone Mihai Bâce April 5, 2017 23

Final methods and classes A derived class Can accept the base class methods Or can override the base class methods A method declared as final in the base class cannot be overridden by any derived class A final class cannot be extended! E.g. Integer, Character,... Mihai Bâce April 5, 2017 24

Abstract classes Abstract method Is a method that all derived classes must implement Abstract class A class that has at least one abstract method If a class derived from an abstract class fails to override an abstract method, the compiler will detect an error Eclipse provides help! Mihai Bâce April 5, 2017 25

Interfaces The interface in Java is the ultimate abstract class. A class can implement many interfaces. A class implements an interface if it provides definitions for all the methods declared in the interface. So, both abstract classes and interface provide a specification of what subclasses must do. But... Mihai Bâce April 5, 2017 26

Abstract class vs. interface Mihai Bâce April 5, 2017 27

Abstract class vs. Interface Abstract class Interface An abstract class can provide complete code, default code, and/or just stubs that have to be overridden May declare methods as protected abstract A class may extend only one abstract class An interface cannot provide any code Not true since Java 8 (default code) All methods declared are implicitly public abstract A class may implement several interfaces http://java.sun.com/docs/books/tutorial/java/iandi/index.html Mihai Bâce April 5, 2017 28

Example interface 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) {... }... } Mihai Bâce April 5, 2017 29

Example Abstract class 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; }... Mihai Bâce April 5, 2017 30

Overview Debriefing Exercise 5 Briefing Exercise 6 Mihai Bâce April 5, 2017 31

U06 Q1: Classes, Interfaces and Casts Q2: Interfaces and their implementation Q3: Polymorphism Q4: ChunkedStack: optional submission Mihai Bâce April 5, 2017 32

Hints A keyword represents an interface Notation for the dependance of the instantiation of the interface. Thermal sensor instantiates the isensor interface Keyword Property Section with attributes (detailed representation) Private client as a specialized Person Section with operations (detailed representation http://de.wikipedia.org/wiki/klassendiagramm Mihai Bâce April 5, 2017 33

U06.A02 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. Mihai Bâce April 5, 2017 34

ScriptS.GE Mihai Bâce April 5, 2017 35

U06.A03 a and c Methods are not static anymore! Ex5.Q1: tostring, add, size Can be easily passed Interface Comparable public interface Comparable { boolean smallerthan(comparable rhs); } You can cast as Comparable without checking its type Mihai Bâce April 5, 2017 36

U06.A04 Stacks again! Optional, so not trivial Self-test: Whoever can do it will have no problem during the exam. Combines the efficiency of arrays to the effortless growth of lists Implement according to the interface Performance analysis Mihai Bâce April 5, 2017 37

Have Fun! Image Mihai Bâce April 5, 2017 38