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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
CS 211 Inheritance AAA.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter -6 Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
NESTED CLASSES REFLECTION PROXIES.
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Advanced Programming in Java
Overriding Method.
Inheritance-Basics.
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
ATS Application Programming: Java Programming
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Chapter 9 Inheritance and Polymorphism
CMSC 202 Polymorphism.
CS18000: Problem Solving and Object-Oriented Programming
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Sampath Kumar S Assistant Professor, SECE
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism CT1513.
Java Inheritance.
Polymorphism.
Lecture 18: Polymorphism (Part II)
Index Understanding static Final Inheritance Super
Chapter 9 Carrano Chapter 10 Small Java
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Advanced Programming in Java
Presentation transcript:

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

Nested classes מחלקות מקוננות class Outer { static class NestedButNotInner { ... } class Inner { מחלקות מקוננות Nested classes

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

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

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

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

Non-static Member Class כל מופע של המחלקה הפנימית משויך למופע של המחלקה החיצונית השיוך מבוצע בזמן יצירת האובייקט ואינו ניתן לשינוי באובייקט הפנימי קיימת הפניה לאובייקט החיצוני (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; } גישה למשתנה פרטי לא סטטי

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

Inner Classes Create new Room … public class House { private String address; private List<Room> rooms; public House(String add){ address = add; rooms = new ArrayList<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); … Create new Room

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

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

Static vs. Dynamic Binding

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

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.

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

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 Compiler knows that all such

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

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

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

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

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.

הסוף...