תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

Similar documents
תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 ומחלקות פנימיות

תוכנה 1 סמסטר א' תשע"א

מבוא למדעי המחשב השפעת השינוי על סטודנט הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות אם ברצוננו ששכר הלימוד לא יעלה על 2500.

הנכות 1 םוכיס לוגרת 14 1

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

Object-Oriented Programming Concepts

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

תוכנה 1 * לא בהכרח בסדר הזה

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 תרגול 2: מערכים ומבני בקרה

הנכות 1 םוכיס לוגרת 13 1


CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

What is Inheritance?

מבוא למדעי המחשב הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות

הקלחמ ה תמרב ת ונ וכ ת (static members ) יליזרב דהוא Java תפשב ם דקת מ תונכת ביבא ל ת תטיסרבינוא

תוכנה 1 * לא בהכרח בסדר הזה

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

- מבחן. - 4 עבודות ב- JAVA. הגשה בזוגות דרך ה- System Submission

Java: introduction to object-oriented features

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

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

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

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

מבוא לתכנות ב- JAVA תרגול 7

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

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Binghamton University. CS-140 Fall Dynamic Types

תכנות מתקדם בשפת C משתנים

Example: Count of Points

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

Tutorial 10. Introduction to C++ שימו

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

JAVA. Lab-9 : Inheritance

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

EKT472: Object Oriented Programming. Overloading and Overriding Method

CS-202 Introduction to Object Oriented Programming

Rules and syntax for inheritance. The boring stuff

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

Inheritance -- Introduction

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13

Chapter 5. Inheritance

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Java Fundamentals (II)

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

CMSC132 Summer 2018 Midterm 1

Avner Algom CEO IGT

Polymorphism. Agenda

1 Shyam sir JAVA Notes

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Overview of Java s Support for Polymorphism

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א'

CS 251 Intermediate Programming Inheritance

Instance Members and Static Members

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Simple Web Service. namespace MyService { public class Service1 : System.Web.Services.WebService {

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

ECE 122. Engineering Problem Solving with Java

Polymorphism CSCI 201 Principles of Software Development

Practice for Chapter 11

Inheritance. Transitivity

Inheritance. Chapter 7. Chapter 7 1

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

תרשים המחלקות ותרשים העצמים

Fundamentals of Object Oriented Programming

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Introductory Programming Inheritance, sections

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

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

ממשק משתמש גרפי בעזרת SWT

Implements vs. Extends When Defining a Class

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

תוכנה 1 בשפת Java תרגול מספר 9: הורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Method Overriding in Java

Unit 4 - Inheritance, Packages & Interfaces

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Polymorphism Part 1 1

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Nir Adar

תכנות מונחה עצמים משחקים תשע"ו

VIRTUAL FUNCTIONS Chapter 10

Handout 9 OO Inheritance.

ENUMERATED TYPES טיפוסי מנייה שחר מעוז תוכנה 1 בשפת ג'אווה אוניברסיטת תל אביב

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Transcription:

תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

class Outer { static class NestedButNotInner {... class Inner {... מחלקות מקוננות NESTED CLASSES 2

מחלקה מקוננת Class) )Nested מחלקה מקוננת היא מחלקה המוגדרת בתוך מחלקה אחרת. סוגים: סטטית 1. )static member( לא סטטית 2. )non-static member( אנונימית 3. )anonymous( מקומית 4. )local( מחלקות פנימיות )inner( 3

בשביל מה זה טוב? קיבוץ לוגי אם משתמשים בטיפוס מסוים רק בהקשר של טיפוס אחר, נטמיע את הטיפוס כדי לשמר את הקשר הלוגי. הכמסה מוגברת על ידי הטמעת טיפוס אחד באחר אנו חושפים את המידע הפרטי רק לטיפוס המוטמע ולא לכולם. קריאות מיקום הגדרת טיפוס בסמוך למקום השימוש בו. 4

מחלקות מקוננות - תכונות משותפות למחלקה מקוננת יש גישה לשדות הפרטיים של המחלקה העוטפת ולהיפך הנראות של המחלקה היא עבור אלו הן מחלקות )כמעט( יכולות להיות אבסטרקטיות, ממחלקות אחרות וכדומה "צד שלישי" רגילות לכל דבר ועניין לממש מנשקים, לרשת 5

Static Member Class מחלקה רגילה ש"במקרה" מוגדרת בתוך מחלקה אחרת החוקים החלים על איברים סטטיים אחרים חלים גם על מחלקות סטטיות גישה לשדות / פונקציות סטטיים בלבד גישה לאיברים לא סטטיים רק בעזרת הפניה לאובייקט גישה לטיפוס בעזרת שם המחלקה העוטפת OuterClass.StaticNestedClass יצירת אובייקט OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass(); 6

Non-static Member Class כל מופע של המחלקה הפנימית משויך למופע של המחלקה החיצונית מופע של המחלקה הפנימית מופע של המחלקה החיצונית 7 השיוך מבוצע בזמן יצירת האובייקט ואינו ניתן לשינוי באובייקט הפנימי קיימת הפניה לאובייקט החיצוני )qualified this(

House Example public class House { private String address; public class Room { // implicit reference to a House private double width; private double height; public String tostring(){ return "Room inside: " + address; גישה למשתנה פרטי לא סטטי 8

Inner Classes public class House { private String address; Height of House private double height; public class Room { // implicit reference to a House private double height; Height of Room public String tostring(){ Same as this.height return "Room height: " + height + " House height: " + House.this.height; 9

Inner Classes public class House { private String address; private List<Room> rooms; public House(String add){ address = add; rooms = new ArrayList<Room>(); Create new Room public void addroom(double width, double height){ Room room = new Room(width,height); rooms.add(room); { public Room getroom(int i){ return rooms.get(i); { 10

Inner Classes public static void main(string [] args) { House house = new House("Hashlom 6"); house.addroom(1.5,3.8); Room r = house.getroom(0); Compilation error { Room room = new Room(1.5,3.8); Room room1 = new House("Hashalom 7").new Room(1.5,3.8); 11

Inner Classes: static vs non-static public class Parent { { { public static class Nested { public Nested() { System.out.println("Nested constructed"); { { public class Inner { public Inner() { System.out.println("Inner constructed"); { { public static void main(string[] args) { Nested nested = new Nested(); Inner inner = new Parent().new Inner(); Construct nested static class Construct nested class 12

STATIC VS. DYNAMIC BINDING 14

Static versus Dynamic Binding public class Account { public String getname(){...; public void deposit(int amount) {...; public class SavingsAccount extends Account { public void deposit(int amount) {...; Account obj = new Account(); obj.getname(); obj.deposit( ); Account obj = new SavingsAccount(); obj.getname(); obj.deposit( ); Which version is called? 15

Binding in Java Binding is the process by which references are bound to specific classes. Used to resolve which methods and variables are used at run time. There are two kind of bindings: static binding and dynamic binding. 16

Binding in Java Static Binding (Early Binding) The compiler can resolve the binding at compile time. (As in the previous example) Dynamic Binding (Late Binding) The compiler is not able to resolve the call and the binding is done at runtime only. Dynamic dispatch 17

Static binding (or early binding) Static binding: bind at compilation time Performed if the compiler can resolve the binding at compile time Applied for Static methods Private methods Final methods Fields 18

Static binding example Static methods public class A { public static void m() { System.out.println ( A ); public class B extends A { public static void m() { System.out.println ( B ); public class StaticBindingTest { public static void main(string args[]) { A.m(); B.m(); Output: A B A A A a = new A(); A b = new B(); a.m(); b.m(); 19

Static binding example - Fields public class A { public String somestring = member of A ; public class B extends A { public String somestring = member of B ; public class StaticBindingTest { public static void main(string args[]) { A a = new A(); A b = new B(); B c = new B(); System.out.println( a.somestring ); System.out.println( b.somestring ); System.out.println( c.somestring ); Output: member of A member of A member of B 20

Dynamic Binding void func (Account obj) { obj.deposit(); What should the compiler do here? The compiler doesn t know which concrete object type is referenced by obj The method to be called can only be known at run time (because of polymorphism and method overriding) Run-time binding 21

Dynamic Binding public class DynamicBindingTest { public static void main(string args[]) { Vehicle vehicle = new Car(); vehicle.start(); class Vehicle { public void start() { //The reference type is Vehicle but run-time object will be Car //Car's start called because start() is overridden method System.out.println("Inside start method of Vehicle"); class Car extends Vehicle { @Override public void start() { System.out.println("Inside start method of Car"); Output: Inside start method of Car 22

difference between static and dynamic binding Static binding happens at compile-time while dynamic binding happens at runtime. Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. Binding of overridden methods happen at runtime. Java uses static binding for overloaded methods and dynamic binding for overridden methods. 23

הסוף...