java for oop
play

Java for OOP Objects de-mystified Christoph Angerer Java Virtual - PowerPoint PPT Presentation

Java for OOP Objects de-mystified Christoph Angerer Java Virtual Machine VM Heap Program Memory (Program Code (Classes, JIT Memory) etc.) Reads Internal use Virtual Machine Slide 2 Runtime Model public class BankAccount {


  1. Java for OOP Objects de-mystified Christoph Angerer

  2. Java Virtual Machine VM Heap Program Memory (Program Code (Classes, JIT Memory) etc.) Reads Internal use Virtual Machine Slide 2

  3. Runtime Model public class BankAccount { private int balance; super null public int getBalance() { “Object” name return balance; } methodTable public void setBalance(int balance) { Class Object bject this.balance = balance; } public void deposit(int amount) { hashCode {code} /* some code */ } toString {code} public int withdraw(int amount) { ... ... /* some code */ super } } “BankAccount” name methodTable Class BankAccount nkAccount class getBalance {code} balance 42 setBalance {code} deposit {code} Object a3249 3249 withdraw {code} (*) the bank account example is taken from the Java in Depth class from Carlo A. Furia Slide 3

  4. How To: Method Call super null “Object” name BankAccount myAccount = ...; methodTable int myBalance = myAccount.getBalance(); Class Object bject hashCode {code} toString {code} (1) ... ... super “BankAccount” name (2) methodTable Class BankAccount nkAccount class (3) getBalance {code} balance 42 setBalance {code} (4) this-pointer deposit {code} Object a3249 3249 withdraw {code} Slide 4

  5. How To: Method Call 2 super null “Object” name BankAccount myAccount = ...; methodTable String myDescr = myAccount.toString(); (5) Class Object bject hashCode {code} (4) toString {code} (1) ... ... super “BankAccount” name (2) methodTable Class BankAccount nkAccount class (3) getBalance {code} balance 42 setBalance {code} (6) this-pointer deposit {code} Object a3249 3249 withdraw {code} Slide 5

  6. Fundamental Concepts • Method Call/Dispatching: starting from the object, follow its class and superclass pointers until a method is found in a method table Slide 6

  7. How To: Overriding super null “Object” name public class BankAccount { /*...*/ methodTable public String toString() { return “I am a bank account”; Class Object bject } } hashCode {code} String myDescr = myAccount.toString(); toString {code} ... ... (1) super “BankAccount” name overridden (2) methodTable Class BankAccount nkAccount class (3) getBalance {code} balance 42 setBalance {code} toString {code} Object a3249 3249 deposit {code} Slide 7

  8. Fundamental Concepts • Method Call/Dispatching: starting from the object, follow its class and superclass pointers until a method is found in a method table • Overriding: a subclass defines a method with the same name as a superclass so it is found first Slide 8

  9. How To: Overloading super null public class BankAccount { “Object” name /*...*/ public void deposit(int amount) { methodTable /* some code */ Class Object bject } public void deposit(float amount) { /* some code */ hashCode {code} } } toString {code} ... ... myAccount.deposit(42.56); super (1) “BankAccount” name (3) ? (2) methodTable Class BankAccount nkAccount class deposit {code} balance 42 We need more deposit {code} Object a3249 3249 information to ... ... resolve this Slide 9

  10. How To: Overloading 2 super null public class BankAccount { “Object” name /*...*/ public void deposit(int amount) { methodTable /* some code */ Class Object bject } public void deposit(float amount) { /* some code */ hashCode {code} } } toString {code} ... ... myAccount.deposit(42.56); super (1) “BankAccount” name (2) methodTable Class BankAccount nkAccount class (3) deposit(int) {code} balance 42 deposit(float) {code} Object a3249 3249 ... ... Slide 10

  11. Fundamental Concepts • Method Call/Dispatching: starting from the object, follow its class and superclass pointers until a method is found in a method table • Overriding: a subclass defines a method with the same name as a superclass so it is found first • Overloading: Java allows us to use the same name for different methods as long as the parameter types are different (overloading is essentially syntactic sugar!) Slide 11

  12. How To: call to this super null public class BankAccount { “Object” name /*...*/ public String toString() { methodTable return “I am a bank account with ” + this.getBalance() Class Object bject + “ CHF”; } hashCode {code} } toString {code} String myDescr = myAccount.toString(); ... ... (1) super “BankAccount” name (2) (6) methodTable (5) Class BankAccount nkAccount class (3) getBalance {code} balance 42 setBalance {code} toString {code} Object a3249 3249 (4) this-pointer deposit {code} Slide 12

  13. How To: call to super super null public class BankAccount { “Object” name /*...*/ public String toString() { methodTable return (5) super.toString() Class Object bject + “ (I am a bank account)”; } hashCode {code} } toString {code} String myDescr = myAccount.toString(); ... ... (1) super “BankAccount” name (2) (4) methodTable Class BankAccount nkAccount class (3) getBalance {code} balance 42 setBalance {code} this-pointer toString {code} Object a3249 3249 deposit {code} Slide 13

  14. Fundamental Concepts • Method Call/Dispatching: starting from the object, follow its class and superclass pointers until a method is found in a method table • Overriding: a subclass defines a method with the same name as a superclass so it is found first • Overloading: Java allows us to use the same name for different methods as long as the parameter types are different (overloading is essentially syntactic sugar!) • Super-call: similar to a method call to ‘this’, but method resolution starts at the super class Slide 14

  15. Classes re-visited • A class is a collection of: • fields • methods, including signatures and bodies • one class can extend another (“inherit from”): • sets the ‘super’ pointer to the other class • An object “is an instance of a class”: • Its ‘class’ pointer points to the class struct • the fields defined in the class define the size and layout of the object struct Slide 15

  16. Multiple Inheritance • What if: the single ‘super’ super name ... pointer were a list of pointers? methodTable ... Class A • How to resolve methods now? What order? super super name ... name ... • Different solutions, no “right” methodTable ... methodTable ... Class B Class C strategy, weird corner cases • Java’s Solution: don’t allow it super name ... • ⇒ Single inheritance + methodTable ... Class D Interfaces Slide 16

  17. Java Interfaces • “classes with empty method bodies” • An interface defines method signatures that classes later implement • Because an interface does not include method bodies, multiple inheritance is not a problem • An Interface can extend other interfaces • Interfaces are interesting for structuring your code and modeling • But don’t play a big role at runtime during execution Slide 17

  18. Abstract Classes • Mix between interfaces and classes (“incomplete classes”) • A class can leave methods unimplemented and only define the signature if it is declared to be abstract • Children of this class must either implement all abstract methods or be declared abstract itself • Abstract classes can contain method implementations: • We still have problems with multiple inheritance • Therefore: A class can only extend exactly one other class or abstract class • Again: more interesting for designing than at runtime Slide 18

  19. public abstract class BankAccount { /*code as before, plus:*/ � public abstract float getInterestRate(); } interface Freezable { void freeze(); Interface Example void unfreeze(); } interface Updatable { void dailyUpdate(Date d); } public class PrivatePersonAccount � � � � � � extends BankAccount �� � implements Freezable, Updatable { boolean isFrozen = false; void freeze() { this.isFrozen = true; } void unfreeze() { this.isFrozen = false; } void dailyUpdate(Date d) { if(! this.isFrozen) { /* compute interest */ } } void getInterestRate() { return DB.getPrivatePersonInterestRate(); } Slide 19 }

  20. Code Management • Java provides many features for managing code: • Packages • Visibility modifiers (public, protected, package- local, private) • Those features are not terribly important for the program execution, but in Java you still have to understand them (somewhat) Slide 20

  21. Packages • In big projects, one has to deal with naming conflicts (what class “Person” do you mean?) • Java avoids this through packages (a.k.a. “really long names”) • by convention in reverse URL notation: com.mydomain.myproject.some.package • but that’s not necessary and has nothing to do with internet URLs • In a file, you can either write this really long name everywhere • or import a class or a whole package in the beginning of the file • as long as you don’t get a naming conflict; then you have to use some fully qualified names again • For defining your own packages, consult the internet Slide 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend