Successfully reported this slideshow.

Jedi slides 2.1 object-oriented concepts


Published on


Published in: Technology
  • Be the first to comment

Jedi slides 2.1 object-oriented concepts

  1. 1. Object-oriented Concepts TOPIC ONE Object-oriented Software Engineering
  2. 2. Object-oriented Software Engineering <ul><li>Object-oriented Software Engineering is the use of object technologies in building software. </li></ul>
  3. 3. <ul><li>Pressman, 1997 </li></ul><ul><ul><li>Object technologies is often used to encompass all aspects of an object-oriented view and includes analysis, design, and testing methods; programming languages; tools; databases; and applications that are created using object-oriented approach. </li></ul></ul><ul><li>Taylor, 1997, Object Technology </li></ul><ul><ul><li>Object technology is a set of principles guiding software construction together with languages, databases, and other tools that support those principles. </li></ul></ul>Object Technology
  4. 4. <ul><li>It leads to reuse, and reuse (of program components) leads to faster software development and higher-quality programs. </li></ul><ul><li>It leads to higher maintainability of software modules because its structure is inherently decoupled. </li></ul><ul><li>It leads to object-oriented system that are easier to adapt and easier to scale, ie, large systems are created by assembling reusable subsystems. </li></ul>Benefits of Object Technology
  5. 5. <ul><li>It is a representation of an entity either physical, conceptual, or software. </li></ul><ul><li>It allows software developers to represent real-world concepts in their software design. </li></ul>Object
  6. 6. <ul><li>It is an entity with a well-defined boundary and identity that encapsulates state and behavior. </li></ul>Object
  7. 7. <ul><li>It is one of the possible conditions that an object may exists in. </li></ul><ul><li>It is implemented by a set of properties called attributes , along with its values and the links it may have on other objects. </li></ul>Object's State
  8. 8. <ul><li>It determines how an object acts and reacts. </li></ul><ul><li>It is represented by the operations that the object can perform. </li></ul>Object's Behavior
  9. 9. <ul><li>Although two objects may share the same state (attributes and relationships), they are separate, independent objects with their own unique identity. </li></ul>Object's Identity
  10. 10. <ul><li>Abstraction </li></ul><ul><li>Encapsulation </li></ul><ul><li>Modularity </li></ul><ul><li>Hierarchy </li></ul>Four Basic Principles of Object-orientation
  11. 11. <ul><li>Abstraction is a kind of representation that includes only the things that are important or interesting from a particular point of view. </li></ul><ul><li>It is the process of emphasizing the commonalities while removing distinctions. </li></ul><ul><li>It allows us to manage complexity systems by concentrating on the essential characteristics that distinguish it from all other kinds of systems. </li></ul><ul><li>It is domain and perspective dependent. </li></ul>Abstraction
  12. 12. Sample Abstraction <ul><li>An applicant submits a club membership application to the club staff. </li></ul><ul><li>A club staff schedules an applicant for the mock try-outs. </li></ul><ul><li>A coach assigns an athlete to a squad. </li></ul><ul><li>A squad can be a training or competing squad. </li></ul><ul><li>Teams are formed from a squad. </li></ul>
  13. 13. <ul><li>Encapsulation localizes features of an entity into a single blackbox abstraction, and hides the implementation of these features behind a single interface. </li></ul><ul><li>It is also known as information-hiding ; it allows users to use the object without knowing how the implementation fulfils the interface. </li></ul><ul><li>It offers two kinds of protection: it protects the object's state from being corrupted and client code from changes in the object's implementation. </li></ul>Encapsulation
  14. 14. Encapsulation Illustrated <ul><li>Joel Santos is assigned to the Training Squad. </li></ul><ul><li>The key is in the message interface . </li></ul>updateSquad(“Training”)
  15. 15. <ul><li>Modularity is the physical and logical decomposition of large and complex things into smaller and manageable components that achieve the software engineering goals. </li></ul><ul><li>It is about breaking up a large chunk of a system into small and manageable subsystems. The subsystems can be independently developed as long as their interactions are well understood. </li></ul>Modularity
  16. 16. Modularity Illustrated Ang Bulilit Liga Squad and Team System Club Membership Maintenance System Coach Information Maintenance System Squad and Team Maintenance System
  17. 17. <ul><li>Any ranking or ordering of abstractions into a tree-like structure. </li></ul><ul><li>Kinds of Hierarchy </li></ul><ul><ul><li>Aggregation </li></ul></ul><ul><ul><li>Class </li></ul></ul><ul><ul><li>Containment </li></ul></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Partition </li></ul></ul><ul><ul><li>Specialization </li></ul></ul><ul><ul><li>Type </li></ul></ul>Hierarchy
  18. 18. Hierarchy Illustrated Squad Training Squad Competing Squad
  19. 19. <ul><li>It is a form of association wherein one class shares the structure and/or behavior of one or more classes. </li></ul><ul><li>It defines a hierarchy of abstractions in which a subclass inherits from one or more superclasses. </li></ul><ul><ul><li>Single Inheritance </li></ul></ul><ul><ul><li>Multiple Inheritance </li></ul></ul><ul><li>It is an is a kind of relationship. </li></ul>Generalization
  20. 20. <ul><li>It is a mechanism by which more-specific elements incorporate the structure and behavior of more-general elements. </li></ul><ul><li>A class inherits attributes, operations and relationship. </li></ul>Inheritance Squad Name Coach MemberList listMembers() changeCoach() Training Squad Competing Squad
  21. 21. Inheritance <ul><li>In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass. </li></ul><ul><li>A sample class hierarchy is shown below. </li></ul><ul><li>Superclass </li></ul><ul><ul><li>Any class above a specific class in the class hierarchy. </li></ul></ul><ul><li>Subclass </li></ul><ul><ul><li>Any class below a specific class in the class hierarchy. </li></ul></ul>
  22. 22. Inheritance <ul><li>Superclass </li></ul><ul><ul><li>Any class above a specific class in the class hierarchy. </li></ul></ul><ul><li>Subclass </li></ul><ul><ul><li>Any class below a specific class in the class hierarchy. </li></ul></ul>
  23. 23. Inheritance <ul><li>Benefits of Inheritance in OOP : Reusability </li></ul><ul><ul><li>Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses. </li></ul></ul><ul><ul><li>Thus, you can encode a method only once and they can be used by all subclasses. </li></ul></ul><ul><ul><li>A subclass only needs to implement the differences between itself and the parent. </li></ul></ul>
  24. 24. Inheritance <ul><li>To derive a class, we use the extends keyword. </li></ul><ul><li>In order to illustrate this, let's create a sample parent class. </li></ul><ul><li>Suppose we have a parent class called Person. </li></ul>public class Person { protected String name; protected String address; /** * Default constructor */ public Person(){ System.out.println(“Inside Person:Constructor”); name = &quot;&quot;; address = &quot;&quot;; } . . . . }
  25. 25. Inheritance <ul><li>Now, we want to create another class named Student. </li></ul><ul><li>Since a student is also a person, we decide to just extend the class Person, so that we can inherit all the properties and methods of the existing class Person. </li></ul><ul><li>To do this, we write, </li></ul>public class Student extends Person { public Student(){ System.out.println(“Inside Student:Constructor”); } . . . . }
  26. 26. Inheritance <ul><li>When a Student object is instantiated, the default constructor of its superclass is invoked implicitly to do the necessary initializations. </li></ul><ul><li>After that, the statements inside the subclass's constructor are executed. </li></ul>
  27. 27. Inheritance: <ul><li>To illustrate this, consider the following code, </li></ul><ul><li>In the code, we create an object of class Student. The output of the program is, </li></ul>public static void main( String[] args ){ Student anna = new Student(); } I nside Person:Constructor Inside Student:Constructor
  28. 28. Inheritance <ul><li>The program flow is shown below. </li></ul>
  29. 29. The “super” keyword <ul><li>A subclass can also explicitly call a constructor of its immediate superclass. </li></ul><ul><li>This is done by using the super constructor call. </li></ul><ul><li>A super constructor call in the constructor of a subclass will result in the execution of relevant constructor from the superclass, based on the arguments passed. </li></ul>
  30. 30. The “super” keyword <ul><li>For example, given our previous example classes Person and Student, we show an example of a super constructor call. </li></ul><ul><li>Given the following code for Student, </li></ul>public Student(){ super( &quot;SomeName&quot;, &quot;SomeAddress&quot; ); System.out.println(&quot;Inside Student:Constructor&quot;); }
  31. 31. The “super” keyword <ul><li>Few things to remember when using the super constructor call: </li></ul><ul><ul><li>The super() call MUST OCCUR AS THE FIRST STATEMENT IN A CONSTRUCTOR. </li></ul></ul><ul><ul><li>The super() call can only be used in a constructor definition. </li></ul></ul><ul><ul><li>This implies that the this() construct and the super() calls CANNOT BOTH OCCUR IN THE SAME CONSTRUCTOR. </li></ul></ul>
  32. 32. The “super” keyword <ul><li>Another use of super is to refer to members of the superclass (just like the this reference ). </li></ul><ul><li>For example, </li></ul>public Student() { = “somename”; super.address = “some address”; }
  33. 33. Overriding methods <ul><li>If for some reason a derived class needs to have a different implementation of a certain method from that of the superclass, overriding methods could prove to be very useful. </li></ul><ul><li>A subclass can override a method defined in its superclass by providing a new implementation for that method. </li></ul>
  34. 34. Example <ul><li>Suppose we have the following implementation for the getName method in the Person superclass, </li></ul>public class Person { : : public String getName(){ System.out.println(&quot;Parent: getName&quot;); return name; } }
  35. 35. Example <ul><li>To override, the getName method of the superclass Person, we write in the subclass Student, </li></ul><ul><li>Now, when we invoke the getName method of an object of the subclass Student , the overridden getName method would be called, and the output would be, </li></ul>public class Student extends Person{ : : public String getName(){ System.out.println(&quot;Student: getName&quot;); return name; } : } Student: getName
  36. 36. Final Classes <ul><li>Final Classes </li></ul><ul><ul><li>Classes that cannot be extended </li></ul></ul><ul><ul><li>To declare final classes, we write, public final ClassName{ . . . } </li></ul></ul><ul><li>Example: </li></ul><ul><li>Other examples of final classes are your wrapper classes and Strings. </li></ul>public final class Person { . . . }
  37. 37. Final Methods and Classes <ul><li>Final Methods </li></ul><ul><ul><li>Methods that cannot be overridden </li></ul></ul><ul><ul><li>To declare final methods, we write, public final [returnType] [methodName]([parameters]){ . . . } </li></ul></ul><ul><li>Static methods are automatically final. </li></ul>
  38. 38. Example public final String getName(){ return name; }
  39. 39. <ul><li>It is the ability to hide many different implementation behind a single interface. </li></ul><ul><li>It allows the same message to be handled differently by different objects. </li></ul>Polymorphism <<interface>> Asset getValue(); Stock getValue() ; Bond getValue(); Mutual Fund getValue();
  40. 40. Polymorphism <ul><li>Polymorphism </li></ul><ul><ul><li>The ability of a reference variable to change behavior according to what object it is holding. </li></ul></ul><ul><ul><li>This allows multiple objects of different subclasses to be treated as objects of a single superclass, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to. </li></ul></ul><ul><li>To illustrate polymorphism, let us discuss an example. </li></ul>
  41. 41. Polymorphism <ul><li>Given the parent class Person and the subclass Student of the previous examples, we add another subclass of Person which is Employee. </li></ul><ul><li>Below is the class hierarchy for that, </li></ul>
  42. 42. Polymorphism <ul><li>In Java, we can create a reference that is of type superclass to an object of its subclass. For example, </li></ul>public static main( String[] args ) { Person ref; Student studentObject = new Student(); Employee employeeObject = new Employee(); ref = studentObject; //Person reference points to a // Student object }
  43. 43. Polymorphism <ul><li>Now suppose we have a getName method in our superclass Person, and we override this method in both the subclasses Student and Employee. </li></ul>public class Student { public String getName(){ System.out.println(“Student Name:” + name); return name; } } public class Employee { public String getName(){ System.out.println(“Employee Name:” + name); return name; } }
  44. 44. Polymorphism <ul><li>Going back to our main method, when we try to call the getName method of the reference Person ref, the getName method of the Student object will be called. </li></ul><ul><li>Now, if we assign ref to an Employee object, the getName method of Employee will be called. </li></ul>
  45. 45. Polymorphism <ul><li>public static main( String[] args ) { </li></ul><ul><li>Person ref; </li></ul><ul><li>Student studentObject = new Student(); </li></ul><ul><li>Employee employeeObject = new Employee(); </li></ul><ul><li>ref = studentObject; //Person ref. points to a </li></ul><ul><li>// Student object </li></ul><ul><li>//getName of Student class is called </li></ul><ul><li>String temp=ref.getName(); </li></ul><ul><li>System.out.println( temp ); </li></ul><ul><li>ref = employeeObject; //Person ref. points to an </li></ul><ul><li>// Employee object </li></ul><ul><li>//getName of Employee class is called </li></ul><ul><li>String temp = ref.getName(); </li></ul><ul><li>System.out.println( temp ); </li></ul><ul><li>} </li></ul>
  46. 46. Polymorphism <ul><li>Another example that illustrates polymorphism is when we try to pass references to methods. </li></ul><ul><li>Suppose we have a static method printInformation that takes in a Person reference as parameter. </li></ul>public static printInformation( Person p ) { . . . . }
  47. 47. Polymorphism <ul><li>We can actually pass a reference of type Employee and type Student to the printInformation method as long as it is a subclass of the class Person. </li></ul>public static main( String[] args ) { Student studentObject = new Student(); Employee employeeObject = new Employee(); printInformation( studentObject ); printInformation( employeeObject ); }
  48. 48. <ul><li>It formalizes polymorphism. It defines polymorphism in a declarative way, unrelated to implementation. </li></ul><ul><li>It is the key to the plug-n-play ability of an architecture. </li></ul>Interface
  49. 49. <ul><li>It is a special form of association that models a whole-part relationship between an aggregate (whole) and its parts. </li></ul>Aggregation Team Athletes
  50. 50. Summary <ul><li>Object Technologies </li></ul><ul><li>Objects </li></ul><ul><ul><li>Object's State </li></ul></ul><ul><ul><li>Object's Behavior </li></ul></ul><ul><ul><li>Object's Identity </li></ul></ul><ul><li>Four Basic Principles of Object-orientation </li></ul><ul><ul><li>Abstraction </li></ul></ul><ul><ul><li>Encapsulation </li></ul></ul><ul><ul><li>Modularity </li></ul></ul><ul><ul><li>Hierarchy </li></ul></ul>