Midterm 1 Flashcards
Object Oriented Design
The attributes and behaviors are contained within a single object, whereas in procedural, or structured, design, the attribute and behaviors are normally separated.
Why use objects
(1) Writing code that can be easily modified and extended.
(2) Making it difficult to make certain kinds of errors when writing code.
(1) Improve testability.
Overall, reduce the probability that the program will have certain kinds of bugs.
What is an object
has specific data attributed to it and unique functions associated with it. For instance, a student Object might have W-Number, gender, DOB, SSN, etc. and would have behaviors where we could getCalculateGPA, getCoursesTake, setCGPA etc.
What is a class
A blueprint, template, cookie cutter for an object. When we use new Student(x, y, z) a new student object will be constructed.
Things Described by a class
Attributes: Name, type, whether or not the attribute is externally visible.
Methods: Name, parameters, return values, whether they are public or private.
Constructors: methods that are used to initialize new objects.
Encapsulation
Bundling data and behavior together in an object so that only the external behavior is exposed.
from the perspective of client classes, the external behavior can be thought of as what the object “does” or what the object is “responsible” for.
Some Authors consider information hiding and encapsulation as being synonymous.
Others consider information hiding as the principle and encapsulation as the programming language mechanism.
Inheritance
Idea: the child inherits both interface and implementation from the parent.
Inheritance: IS-A relationship
a manager is an employee and an employee is a person. etc.
Anything that can be done to the person can be done to the the employee and then onto the manager.
Polymorphism
Type (1): Multiple functions with the same name. The actual function invoked is chosen based on types of the parameters as determined by the compiler at compile time. i.e. System.out.print can print has nine different version.
Type (2): In OO there are Multiple function that have the same name and signature but the actual function invoked is chosen during the run-time, wherein it is chosen based on the actual type of the parameter(s).
Polymorphism: Overloading
Multiple functions which are distinguished by the compiler based on the type of the parameters.
e.g. toString(String x), toString(int x), toString(int x, int y)
Polymorphism: Overriding
When the implementation of a function in a child is used instead of the implementation of the “same” function in a parent, the function in the child is said to override the implementation in the parent.
Composition: HAS-A relationship
Composition is when one object “contains” or HAS-A another.
Eg. a car HAS-A engine and the engine IS-A Engine4Cyl or IS-A Engine6Cyl.
Constructors in OO
Constructors are methods that share the same name as the class and no return type. e.g. we have the method public Student(int x, int y) when we call the method Student Bob = new Student(4, 3) the Student method constructs the new student object bob that has the specified attributes created in the student object. The constructor will allocate memory for the new object.
Default constructor
If the case of absence of a constructor, the class will still compile. If the class provides no explicit constructor, a default constructor will be provided by the compiler. Besides the creation of the object itself, the only action that a default constructor will take is to call the constructor of its superclass.
Method Overloading
Overloading allows a programmer to use the same method over and over, as long as the “signature” of the method is different each time.
Signature = method name + parameter list
public int getCGPA(String WNum)
public int getCGPA(int WNum)
public int getCGPA(String WNum, String level)
Note:
In Java and C# the return type is not part of the signature. the following methods would conflict even though the signature is the same.
public int getGPA(string WNum, String Level)
public String getGPA(String WNum, String Level)
Ways of Error Handling
Ignore the Problem–Not a good idea! (crying is better)
Check for potential problems and abort the program when you find a problem. (Use print functions everywhere)
Check for potential problems, catch the mistake, and attempt to fix the problem. Implementing something like peekFirst or getFirst in the Deque class)
Throw an exception. (In general, preferred way)