In Object Oriented concepts there are two type relations which exists in between two objects.
1. IS A Relationship
2. HAS A Relationship
Inheritance and “is a”
We can understand this better by considering the “is a” idea
A subclass object “is a” superclass object.However, some extra instance variables and methods may have been added and some other methods may have been changed
Note that “is a” is a one way operation.Subclass “is a” superclass (specific "is a" general)With modifications / additions Superclass is NOT a subclass (general not "is a" specific Missing some properties
Ex: Bird “is a” Animal
Human “is a” Animal
Fish “is a” Animal
Bird, Human and Fish are all Animals
However, an Animal is not necessarily a Bird, Human or Fish
HAS A relationship
In OOP, Has-A relationships are relationships where one object contains, or controls another object. It is contrasted with an Is-A relationship.
Objects having Has-A relationships are models for composition, or an object being contained inside of the parent object.
Examples of Has a Relationships:
A Person object has-a(n) Address Object.
A Teacher Object has-a Student Object.
A Car Object has-a Tire Object.
A Document Object has-a Keyword or Tag Object.
Generically, an Object has-a DAO or Gateway Object
In these examples, you notice that the objects are not related in a parent-child type of relationship. They (typically) cannot respond to the same type of events or messages.
Contrast this with the Is-A relationship that are models for Inheritance.
Showing posts with label Technology. Show all posts
Showing posts with label Technology. Show all posts
Saturday, November 10, 2007
Object Oriented Programming
Object-Oriented Programming consists of 3 primary ideas:
Data Abstraction and Encapsulation
Operations on the data are considered to be part of the data type
We can understand and use a data type without knowing all of its implementation details.Neither how the data is represented nor how the operations are implemented
We just need to know the interface (or method headers) – how to “communicate” with the object Compare to functional abstraction with methods.
Inheritance
Properties of a data type can be passed down to a sub-type – we can build new types from old ones.We can build class hierarchies with many levels of inheritance
Polymorphism
Operations used with a variable are based on the class of the object being accessed, not the class of the variable.Parent type and sub-type objects can be accessed in a consistent way.
Constructors
These are special instance methods that are called when an object is first created.
They are the only methods that do not have a return value (not even void).
They are typically used to initialize the instance variables of an object.
Accessors
These methods are used to access the object in some way without changing it
Usually used to get information from it.
Mutators
Used to change the object in some way
Since the instance variables are usually private, we use mutators to change the object in a specified way without needing to know the instance variables
B.setCharAt(0, ‘j’); // B == “jello there”
B.delete(5,6); // B == “jello here”
B.insert(6, “is “); // B == “jello is here”;
These methods change the contents or properties of the StringBuffer object
Data Abstraction and Encapsulation
Operations on the data are considered to be part of the data type
We can understand and use a data type without knowing all of its implementation details.Neither how the data is represented nor how the operations are implemented
We just need to know the interface (or method headers) – how to “communicate” with the object Compare to functional abstraction with methods.
Inheritance
Properties of a data type can be passed down to a sub-type – we can build new types from old ones.We can build class hierarchies with many levels of inheritance
Polymorphism
Operations used with a variable are based on the class of the object being accessed, not the class of the variable.Parent type and sub-type objects can be accessed in a consistent way.
Constructors
These are special instance methods that are called when an object is first created.
They are the only methods that do not have a return value (not even void).
They are typically used to initialize the instance variables of an object.
Accessors
These methods are used to access the object in some way without changing it
Usually used to get information from it.
Mutators
Used to change the object in some way
Since the instance variables are usually private, we use mutators to change the object in a specified way without needing to know the instance variables
B.setCharAt(0, ‘j’); // B == “jello there”
B.delete(5,6); // B == “jello here”
B.insert(6, “is “); // B == “jello is here”;
These methods change the contents or properties of the StringBuffer object
Subscribe to:
Posts (Atom)