Successfully reported this slideshow.

Object oriented techniques

6,973 views

Published on

Understand how derived classes inherit from base classes, explore how to add and modify members in derived classes, learn to control how derived classes inherit from base classes, understand how to create and use interfaces and explore techniques for organizing your classes.

Published in: Technology
  • Be the first to comment

Object oriented techniques

  1. 1. Object-OrientedTechniques Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  2. 2. Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  3. 3. Objectives• Understand how derived classes inherit from base classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  4. 4. Objectives• Understand how derived classes inherit from base classes• Explore how to add and modify members in derived classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  5. 5. Objectives• Understand how derived classes inherit from base classes• Explore how to add and modify members in derived classes• Learn to control how derived classes inherit from base classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  6. 6. Objectives• Understand how derived classes inherit from base classes• Explore how to add and modify members in derived classes• Learn to control how derived classes inherit from base classes• Understand how to create and use interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  7. 7. Objectives• Understand how derived classes inherit from base classes• Explore how to add and modify members in derived classes• Learn to control how derived classes inherit from base classes• Understand how to create and use interfaces• Explore techniques for organizing your classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  8. 8. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  9. 9. Agenda• Inheritance Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  10. 10. Agenda• Inheritance• Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  11. 11. Agenda• Inheritance• Interfaces• Organizing Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  12. 12. Inheritance Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  13. 13. Inheritance• Inefficient to have similar classes with the same members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  14. 14. Inheritance• Inefficient to have similar classes with the same members• Create a more generic (base) class and then inherit (derive) from the generic class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  15. 15. Inheritance• Inefficient to have similar classes with the same members• Create a more generic (base) class and then inherit (derive) from the generic class• Derived classes inherit the members of the base class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  16. 16. Inheritance• Inefficient to have similar classes with the same members• Create a more generic (base) class and then inherit (derive) from the generic class• Derived classes inherit the members of the base class• Inheritance defines an “is-a” relationship Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  17. 17. Inheritance• Inefficient to have similar classes with the same members• Create a more generic (base) class and then inherit (derive) from the generic class• Derived classes inherit the members of the base class• Inheritance defines an “is-a” relationship  A corporation is a customer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  18. 18. Inheritance• Inefficient to have similar classes with the same members• Create a more generic (base) class and then inherit (derive) from the generic class• Derived classes inherit the members of the base class• Inheritance defines an “is-a” relationship  A corporation is a customer  An individual is a customer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  19. 19. Inheritance Hierarchy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  20. 20. Inheritance Hierarchy• Customer is the base class and is the most generic representation of a customer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  21. 21. Inheritance Hierarchy• Customer is the base class and is the most generic representation of a customer• Corporation and Individual are derived classes, inheriting from the base class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  22. 22. The “is-a” Relationship Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  23. 23. The “is-a” Relationship• Be thorough when defining the “is-a” relationships among objects in your code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  24. 24. The “is-a” Relationship• Be thorough when defining the “is-a” relationships among objects in your code• If differences between two types of customers (like Corporation and Individual) are significant, it may warrant making separate classes such as Domestic and Foreign Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  25. 25. Adding Members to Derived Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  26. 26. Adding Members to Derived Classes • Common properties and methods can go in the base class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  27. 27. Adding Members to Derived Classes • Common properties and methods can go in the base class • Unique properties and methods can be added to derived classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  28. 28. Overriding Derived Members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  29. 29. Overriding Derived Members• Override a property or method in a derived class to change its behavior Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  30. 30. Overriding Derived Members• Override a property or method in a derived class to change its behavior• You are replacing the base class member with the derived class member Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  31. 31. Overloading Derived Members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  32. 32. Overloading Derived Members• Overload a property or method of a base class to create a specialized version in a derived class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  33. 33. Overloading Derived Members• Overload a property or method of a base class to create a specialized version in a derived class• The base class member still exists and can be used in addition to a derived class member Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  34. 34. Calling Base Class Members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  35. 35. Calling Base Class Members• Members of a derived class can call members of the base class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  36. 36. Abstract Classes and Members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  37. 37. Abstract Classes and Members• An abstract class is designed to be generic and is incomplete Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  38. 38. Abstract Classes and Members• An abstract class is designed to be generic and is incomplete• You cannot create an instance of it and can only derive from it Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  39. 39. Abstract Classes and Members• An abstract class is designed to be generic and is incomplete• You cannot create an instance of it and can only derive from it• Abstract class can contain abstract properties and members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  40. 40. Abstract Classes and Members• An abstract class is designed to be generic and is incomplete• You cannot create an instance of it and can only derive from it• Abstract class can contain abstract properties and members  Derived classes cannot call these properties from the abstract class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  41. 41. Abstract Classes and Members• An abstract class is designed to be generic and is incomplete• You cannot create an instance of it and can only derive from it• Abstract class can contain abstract properties and members  Derived classes cannot call these properties from the abstract class  They must implement them on their own Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  42. 42. Sealed Classes and Members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  43. 43. Sealed Classes and Members• Seal a class to prevent inheriting from it Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  44. 44. Sealed Classes and Members• Seal a class to prevent inheriting from it• Seal a member in a derived class to prevent further derived classes from overriding it Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  45. 45. Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  46. 46. Interfaces• Define a set of properties and methods that a class will implement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  47. 47. Interfaces• Define a set of properties and methods that a class will implement• Contain no implementation code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  48. 48. Interfaces• Define a set of properties and methods that a class will implement• Contain no implementation code• A class must implement all members defined in an interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  49. 49. Interfaces• Define a set of properties and methods that a class will implement• Contain no implementation code• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  50. 50. Interfaces• Define a set of properties and methods that a class will implement• Contain no implementation code• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members  You can override and overload interface members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  51. 51. Implementing an Interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  52. 52. Implementing an Interface• A class must implement all members defined in an interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  53. 53. Implementing an Interface• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  54. 54. Implementing an Interface• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members  You can override and overload interface members Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  55. 55. Implementing an Interface• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members  You can override and overload interface members• Class can implement multiple interfaces but can only derive from one class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  56. 56. Implementing an Interface• A class must implement all members defined in an interface  Visual Studio adds member declarations automatically, you add code to implement members  You can override and overload interface members• Class can implement multiple interfaces but can only derive from one class• Use interfaces to implement smaller sets of members and to enable classes to implement only interfaces they need Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  57. 57. Interfaces in the .NET Framework Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  58. 58. Interfaces in the .NET Framework• The .NET Framework contains many interfaces you can use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  59. 59. Interfaces in the .NET Framework• The .NET Framework contains many interfaces you can use• IComparable provides a general way of comparing value types or classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  60. 60. Interfaces in the .NET Framework• The .NET Framework contains many interfaces you can use• IComparable provides a general way of comparing value types or classes  Call CompareTo as a method of one type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  61. 61. Interfaces in the .NET Framework• The .NET Framework contains many interfaces you can use• IComparable provides a general way of comparing value types or classes  Call CompareTo as a method of one type  Pass as argument the type you are comparing the first type with Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  62. 62. Organizing Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  63. 63. Organizing Classes• Classes are a good way to organize your code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  64. 64. Organizing Classes• Classes are a good way to organize your code• You can also organize your classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  65. 65. Partial Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  66. 66. Partial Classes• Helpful on teams where you are writing some class methods and other developers are writing the rest Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  67. 67. Partial Classes• Helpful on teams where you are writing some class methods and other developers are writing the rest• Split the class definition across multiple files Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  68. 68. Partial Classes• Helpful on teams where you are writing some class methods and other developers are writing the rest• Split the class definition across multiple files• Compiler combines the partial classes into one class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  69. 69. Partial Classes• Helpful on teams where you are writing some class methods and other developers are writing the rest• Split the class definition across multiple files• Compiler combines the partial classes into one class• Calling class uses class the same whether it is made up of partial classes or not Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  70. 70. Nested Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  71. 71. Nested Classes• Class defined within another class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  72. 72. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  73. 73. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  74. 74. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  75. 75. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information o CustomerName, City, Region, etc Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  76. 76. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information o CustomerName, City, Region, etc  Customer.Financial Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  77. 77. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information o CustomerName, City, Region, etc  Customer.Financial o CreditLimit, ChangeCreditLimit Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  78. 78. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information o CustomerName, City, Region, etc  Customer.Financial o CreditLimit, ChangeCreditLimit  Customer.Sales Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  79. 79. Nested Classes• Class defined within another class• Organize class members so they are easier to use at runtime• Group members of Customer class  Customer.Information o CustomerName, City, Region, etc  Customer.Financial o CreditLimit, ChangeCreditLimit  Customer.Sales o RecordSales Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  80. 80. Namespaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  81. 81. Namespaces• A way to organize related classes into groups Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  82. 82. Namespaces• A way to organize related classes into groups  System.Data contains classes to work with data once you have retrieved it from a data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  83. 83. Namespaces• A way to organize related classes into groups  System.Data contains classes to work with data once you have retrieved it from a data source• Namespaces can be nested Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  84. 84. Namespaces• A way to organize related classes into groups  System.Data contains classes to work with data once you have retrieved it from a data source• Namespaces can be nested  System.Data.SqlClient contains classes to retrieve data from SQL Server Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  85. 85. Namespaces• A way to organize related classes into groups  System.Data contains classes to work with data once you have retrieved it from a data source• Namespaces can be nested  System.Data.SqlClient contains classes to retrieve data from SQL Server  System.Data.OracleClient contains classes to retrieve data from Oracle Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  86. 86. Namespaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  87. 87. Namespaces• By default, the application namespace is the name of the project Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  88. 88. Namespaces• By default, the application namespace is the name of the project• You can change this in Project Designer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  89. 89. Namespaces• By default, the application namespace is the name of the project• You can change this in Project Designer• Create your own namespaces to organize classes in class libraries Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  90. 90. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  91. 91. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  92. 92. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  93. 93. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!• Learn more on SlideShare Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  94. 94. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!• Learn more on SlideShare  Object-Oriented JavaScript Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

×