OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560
Object-oriented Concepts Objects – things names with nouns Classes – classifications (groups) of similar objects Instances – specific examples of a particular class Abstraction – generalized class features; essential facts that distinguish one class from another Properties (Data or Attributes) Operations (Methods or Behaviors)
Unified Modeling Language (UML) UML Class Diagram Class Name Data Members Methods Gradebook Name Grade1 Grade2 Average SetStudent ComputeAverage GetStudent
Encapsulation and Data Hiding Encapsulation - packaging data members and methods into a single unit (class or instance of a class) (Public) Interface – well defined external boundary of a class; interact with an object through interface Data Hiding – related to encapsulation, parts of an object not required to use the object are hidden Protects code from deliberate or unintended access Others can use code without understanding how it works Allows code changes to not affect implementation
Pseudocode Class Gradebook private Name private Grade1 private Grade2 private Average public SetStudent Input Name, Grade1, Grade2 End public ComputeAverage Average = (Grade1 + Grade2)/2 End public GetStudent Display Name, Average End End Class
Data Member and Method Access Levels Public variables or methods, are those that are visible to all classes. Private variables or methods, are those that are visible only to the class to which they belong. Generally all data members are declared as private Protected variables or methods, are those that are visible only to the class to which they belong, from any subclasses of that class, or from other classes included with the class in a package (a grouping of related classes).
Class and Object Diagrams Gradebook Name Grade1 Grade2 Average SetStudent ComputeAverage GetStudent Gradebook_J001 Name = Bob S. Grade1 = 94 Grade2 = 96 Average = 90 SetStudent ComputeAverage GetStudent Gradebook_J002 Name = Bill P. Grade1 = 76 Grade2 = 84 Average = 80 SetStudent ComputeAverage GetStudent Class Diagram Object Diagram
Driver Programs Driver programs starts the processing and creates (instantiates) and interacts with (sends messages to) objects Start Gradebook: Gradebook_J001 Gradebook_J001.SetStudent Gradebook_J001.ComputeAverage Gradebook_J001.GetStudent Stop
Constructors Constructor – method used to instantiate (create) and initialize (set initial data member values) for an object Default – constructor automatically provided by programming language (e.g. C++ or JAVA). Usually doesn’t initialize data members which may cause problems Custom – constructor designed to initialize data members. May have more than one for a class. Have the same name as the class they are constructing Parameters – data input to a method, including a Constructor
Two Constructor Examples Class Gradebook Public Gradebook Name = “A. Student” Grade1 = 0 Grade2 = 0 Average = 0 End Public Gradebook (Sname, Grd1, Grd1) Name = Sname Grade1 = Grd1 Grade2 = Grd2 Average = 0 End End Class NOTE: I have omitted the data members and other methods from the above
Overloading The ability to use the same method name to call (invoke) different methods based on the number or type of parameters (inputs) Can be applied to constructors and other methods Compare these constructor calls: Gradebook: MyGradebook Gradebook: YourGradebook (“Brian F.”, 95, 84)
Destructors Destructors are methods used to destroy objects created by constructors May release (deallocate) memory which had been assigned for an object’s data members Default Destructor – included by the programming language or Custom Destructor can be written
Other important terms Return type – the data type of the data returned by a method when it finishes process. You can’t overload a method by using a different return type however as it is not part of the method signature. Method signature – the general form for describing a method, usually consisting of access modifier, method name, and parameter list (types and order of parameters). (JAVA method signature only contains method name and parameter list) public sample (string, float, float)
Generalization/Specialization Classes can have relationships with other classes Generalization/Specialization – one type of relationship Allows sharing of descriptions and identifications Used to simplify and shorten implementation tasks Generally thought of as a requirement for an OOPL Generalizations – the common features of objects of a particular class Specializations – the differences between features of objects of a more general class Two classes have a generalization/specialization relationship: the more specific class is always consistent with the more general class and contains additional information
Inheritance The mechanism that allows defining a more general class and then later a more specific specialized class by adding new details The specific class inherits all the properties, methods, and relationships of the general class
Class Hierarchy Employee Name ID Employee GetEmployee SetEmployee DeleteEmployee Faculty Rank Salary Faculty GetEmployee SetEmployee ComputePay Staff Hours Rate Staff GetEmployee SetEmployee ComputePay Superclass (aka base class parent class) Subclasses (aka derived class child class) Indicate a generalization/specialization “is-a” relationship between classes GetEmployee and SetEmployee are overriden from the base class
Polymorphism Polymorphic method – method with the same name as a method in another class within the same class hierarchy See ComputePay method on previous slide Each is unique, neither was inherited Each calculates pay differently and appropriately Conceptually, Overloading and Polymorphic Methods are both implementations of Polymorphism (from the Greek for “many forms”)
Classes and Subclasses Class Employee private Name private ID public Employee Name = “A. Employee” ID = End Public GetEmployee * End Public SetEmployee * End Public DeleteEmployee * End End Class* implementation omitted
Classes and Subclasses Class Faculty (base Employee) private Rank private Salary public Faculty Rank = “No rank assigned” Salary = 0 End Public GetEmployee * End Public SetEmployee * End Public ComputePay SWITCHCASE CASE “instructor” Salary = “$35,000” CASE “professor Salary = “$60,000” ENDCASE End End Class* implementation omitted
Abstract and Concrete Classes Abstract class – one in which one or more of the class methods are identified but not defined Abstract classes are never instantiated as objects Declare with keyword “Abstract” Concrete class – class which is not abstract Abstract Class Account private AccountNumber [rest of class implementation omitted] End Class