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

More Related Content

PPTX
Virtual Functions | Polymorphism | OOP
PPT
Inheritance
PPT
vb.net Constructor and destructor
PDF
Shell scripting _how_to_automate_command_l_-_jason_cannon
PPT
Inheritance C#
PPTX
Features of java
PPTX
Exception handling in Java
PPTX
Looping statement in vb.net
Virtual Functions | Polymorphism | OOP
Inheritance
vb.net Constructor and destructor
Shell scripting _how_to_automate_command_l_-_jason_cannon
Inheritance C#
Features of java
Exception handling in Java
Looping statement in vb.net

What's hot (20)

PPTX
Introduction to Java
PDF
Lecture 8 Library classes
PPT
PPTX
Object Oriented Programming Using C++
PPT
Inheritance
PPTX
The Singleton Pattern Presentation
PDF
Static and Dynamic Behavior
PPT
Packages and interfaces
PDF
Pointers and call by value, reference, address in C
PDF
Programación 3: Clases y objetos en Java
PPTX
Polimorfismo en Java
PPSX
Control Structures in Visual Basic
PPTX
Java string handling
PPTX
class diagram
PPTX
Inheritance in OOPs with java
PPTX
Partes de la pantalla de eclipse
PPTX
Inheritance
PPTX
Ppt on this and super keyword
PPTX
Encapsulation C++
PPTX
estructura de sistemas operativos
Introduction to Java
Lecture 8 Library classes
Object Oriented Programming Using C++
Inheritance
The Singleton Pattern Presentation
Static and Dynamic Behavior
Packages and interfaces
Pointers and call by value, reference, address in C
Programación 3: Clases y objetos en Java
Polimorfismo en Java
Control Structures in Visual Basic
Java string handling
class diagram
Inheritance in OOPs with java
Partes de la pantalla de eclipse
Inheritance
Ppt on this and super keyword
Encapsulation C++
estructura de sistemas operativos
Ad

Viewers also liked (20)

PDF
Object oriented software engineering concepts
PPTX
Cs690 object oriented_software_engineering_team01_ report
PPT
Object Oriented Concept
 
PDF
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
PPT
Object oriented analysis
PPT
Object-oriented concepts
PDF
Javaprogbasics
DOCX
A project report on libray mgt system
PDF
Oop principles a good book
PPT
Object-Oriented Concepts
PDF
Library management system
PDF
Jedi course notes intro to programming 1
PDF
Software Engineering with Objects (M363) Final Revision By Kuwait10
PPT
Qtp Object Identification
DOCX
Trabalho De Ingles
PDF
Hands-on Guide to Object Identification
PDF
Object Oriented Concept Static vs. Non Static
PPTX
Studentmanagementsystem
PDF
TD-635-02-PSBO
Object oriented software engineering concepts
Cs690 object oriented_software_engineering_team01_ report
Object Oriented Concept
 
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
Object oriented analysis
Object-oriented concepts
Javaprogbasics
A project report on libray mgt system
Oop principles a good book
Object-Oriented Concepts
Library management system
Jedi course notes intro to programming 1
Software Engineering with Objects (M363) Final Revision By Kuwait10
Qtp Object Identification
Trabalho De Ingles
Hands-on Guide to Object Identification
Object Oriented Concept Static vs. Non Static
Studentmanagementsystem
TD-635-02-PSBO
Ad

Similar to Jedi slides 2.1 object-oriented concepts (20)

PPTX
Introduction to OOPs second year cse.pptx
PPT
Chap11
PPT
Object -oriented analysis and design.ppt
PDF
PPTX
java part 1 computer science.pptx
PPTX
OOP Presentation.pptx
PPTX
OOP Presentation.pptx
PDF
JAVA-PPT'S.pdf
DOCX
Java oop concepts
PPT
9781439035665 ppt ch10
PPTX
Java Inheritance - sub class constructors - Method overriding
PDF
OOPs Concepts - Android Programming
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
PPTX
Java chapter 5
PPTX
JAVA-PPT'S-complete-chrome.pptx
PPTX
JAVA-PPT'S.pptx
PPT
Object and class
PPSX
Oop features java presentationshow
PPTX
SKILLWISE - OOPS CONCEPT
PPTX
Inheritance and Polymorphism in java simple and clear
Introduction to OOPs second year cse.pptx
Chap11
Object -oriented analysis and design.ppt
java part 1 computer science.pptx
OOP Presentation.pptx
OOP Presentation.pptx
JAVA-PPT'S.pdf
Java oop concepts
9781439035665 ppt ch10
Java Inheritance - sub class constructors - Method overriding
OOPs Concepts - Android Programming
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
Java chapter 5
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S.pptx
Object and class
Oop features java presentationshow
SKILLWISE - OOPS CONCEPT
Inheritance and Polymorphism in java simple and clear

Recently uploaded (20)

PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
KodekX | Application Modernization Development
PDF
cuic standard and advanced reporting.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced IT Governance
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Transforming Manufacturing operations through Intelligent Integrations
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
GamePlan Trading System Review: Professional Trader's Honest Take
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Spectral efficient network and resource selection model in 5G networks
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
KodekX | Application Modernization Development
cuic standard and advanced reporting.pdf
Review of recent advances in non-invasive hemoglobin estimation
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced IT Governance
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Transforming Manufacturing operations through Intelligent Integrations
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm

Jedi slides 2.1 object-oriented concepts

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