The major elements of the object-oriented model Abstraction Encapsulation Inheritance Modularity Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Reusing Classes Hierarchy 2 An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer. An abstraction captures the entire behavior of some object, no more and no less, and offers no surprises or side effects that go beyond the scope of the abstraction. Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation. [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] 3 4
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] Hierarchy is a ranking or ordering of abstractions. The two most important hierarchies in a complex system are its class structure (the is a hierarchy) and its object structure (the part of hierarchy). [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] 5 6 part of 7 8
Is this a part of relation? What about a person that can also think of two numbers? 9 10 is a Copy-paste is dangerous! 11 12
is a Access Specifiers 13 14 The super keyword 1 Initialization Order 2 Output: 15 16
Inheritance and Constructors Inheritance and Constructors Compilation Error!!! public PersonTwo() { super(); } 17 18 Extending...the Object Class 19 20
SubClass is a type of the SuperClass. PersonTwo is a type of Person. Output: 21 22 Identity (memory address) State (given by its attributes) An Object has... Interface (known by its user) Behavior (known by its implementer) Compilation Error!!! 23 24
25 26 Attributes Final Members Entities that cannot be changed Methods Classes 27 Polymorphism Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Polymorphism
USB + transferpctodevice(pc pc, Device device) : void + transferdevicetopc(pc pc, Device device) : void Device - information : String + Device() + Device(info : String) + store(info : String) : void + load() : String PC - memory : String - registry : String + store(information : String) : void + load() : String VideoDevice - producer : String + VideoDevice(info : String, prod : String) + recordmovie() : void PhotoDevice + PhotoDevice(info : String) + takepicture() : void 30 31 32
Overriding Methods 33 34 Overriding Methods A non static method in a subclass with the same signature (same name, same number and type of parameters) and return type as a non static method in the superclass overrides the method from the superclass. Compilation Error!!! 35 36
Specializing Methods Calls the method defined in the Superclass! Output: 37 38 Method-call Binding Polymorphism Connecting a method call to a method body is called binding. Early binding is done before the program is run. Late binding a.k.a. dynamic binding or runtime binding is done at run time, based on the type of the object. [B. Eckel. Thinking in Java. 4th edition. Prentice Hall. 2006] Type Inheritance + Late Binding Polymorphism allows us to use in an uniform manner different types of objects via Type Inheritance and also differently every object type, if necessarily via Late Binding. 39 40
Overloading versus Overriding Overloading versus Overriding public boolean equals(book o) overloading!!! 41 public boolean equals(book o) overloading!!! 42 Overloading versus Overriding Constructors and polymorphic methods supervalue = 0!!! public boolean equals(object o) overriding!!! 43 44
Polymorphic attributes??? static and overriding methods??? 45 Compilation Error!!! overriding method is static 46 static and overridden methods??? Compilation Error!!! overridden method is static 47 48
Open-Closed Principle Use Device!!! Software entities should be open for extension, but closed for modification. [B.Meyer. Object-Oriented Software Construction.1988] [R. Martin. 1996. http://www.objectmentor.com/resources/articles/ocp.pdf] 49 50 Move functionalities in subclasses and use them via polymorphic calls!!! Figure f = new Figure(); not useful!!! Abstract classes 51 52
Abstract classes Abstract methods Figure f = new Figure(); not possible!!! not useful!!! 53 54 Abstract classes and methods An abstract class is a class than cannot be instantiated. An abstract method is a method without implementation. A class can be abstract even if it does not contain any abstract method. If a class contains at least an abstract method, then it should be declared as being an abstract class. A subclass should implement all the abstract methods from its superclass, or should be declared as abstract. Interfaces Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Interfaces 55
Class versus Interface Interfaces!= interface java keyword!!! Type = Interface WHAT functionality an object provides Class = Implementation HOW is executed the source code FootballPlayer may be a class an abstract class or... an interface!!! 57 58 Interfaces All the members of an interface are public. All the attributes of an interface are static final. Compilation Error!!! protected not allowed here An interface can extends interfaces (more than one). An interface can be seen as a pure abstract class. Compilation Error!!! interface methods cannot have body 59 60
Compilation Error!!! attempting to assign weaker access privileges 61 62 63 64
Compilation Error!!! TennisFootballPlayer is not abstract and does not override abstract method playtennis() or!!! 65 66 Interfaces When a class implements an interface, it should implement all the methods from the interface or it should be declared as being abstract. An interface can have an undefined number of implementations. Two distinct classes can implement the same interface. A class can extend only one class and implement an undefined number of interfaces. 67 68
Conflicts Conflicts Compilation Error!!! reference to C is ambiguous Compilation Error!!! return type is not compatible with void 69 70 Abstract Classes versus Interfaces Abstract Classes versus Interfaces Should provide an implementation or become abstract!!! Overriding is not mandatory!!! 71 72
20 Prefer class hierarchies to tagged classes Effective Java Companion slides of Joshua Bloch, Effective Java (Second Edition) 74 Shortcomings of tagged classes... Readability is hampered because of multiple implementations. Memory is increased. Constructors are responsible for adding the flavor. Adding a new flavor implies the change of the class. Tagged classes are verbose, error-prone and inefficient!!! are imitations of a class hierarchy!!! 75 76