Basic oo concepts

Page 1

COMP 2150

2015-02-05 Basic OO Concepts

These includes Objects, Classes, Messages and Methods. Several types of data structures stored together is known as Cartesian Product. Example struct position { double latitude; double longitude; }; The state of variable is the value of each of the variables in the struct. The class A struct is different from a class in that a class defines behaviors that are useable by any instance of the class Encapsulation: controls access to the data in the object.


Classes Classes Classes Classes Classes

can have other classes as fields within them. define the state information of each Object of that class define methods (functions) used by each Object of that class can be subclass of other classes: By Inheriting states and behaviors relate with other classes: inheritance.

ADTs

There is a separation between the interface and implementation, this is done by having a header file and implementation file.

In OO, we translate programs from ADTs to an Active data Structure.

Message: Way to invoke behavior of an object Objects interact by sending messages to each other; these are done through messages Messages may be parameterized such as “ add a user with id X and pin Y� .


The sender’s Objects don’t care how the receiver's Objects accomplishes the task.

Note: Every class in java has a super class whether defined or not.

For classes to be in tree structure, each class must have a parent class using the keyword extends.

In some languages, a class can have more than one super class. This is known as multiple inheritance; java does not support this, but may have interfaces for inheriting from multiple sources.

Binding Binding is associating one thing to another Binding during runtime is DYNAMIC Binding during compile time is STATIC

BASIC OO CONCEPTS IN JAVA/C++

In Java, pointers are reference to Objects. EG: MyClass x = new MyClass() //x is an implicit pointer to MyClass Object


x.getSomething //x accesses a field in the class MyClass y = x // y and x point to the same MyClass Object

In Ruby, every variable is a pointer, hence all variables are of same size.

In C++, first we dereference the pointer, using parenthesis, before accessing the data

myList->data is equivalent to (&myList).data DO NOT DO ADDRESS ARITHMETIC IN C++ We don’t instantiate abstract classes Overloading: Methods with the same name but different parameters. Overloading happens at compile time //compiler error - can't overload based on the type returned - //(one method returns int, the other returns a float): int changeDate(int Year) ; float changeDate (int Year);


//compiler error - can't overload by changing just the name of the parameter (from Year to Month):

int changeDate(int Year); int changeDate(int Month) ;

//valid case of overloading, since the methods //have different number of parameters:

int changeDate(int Year, int Month) ; int changeDate(int Year); //also a valid case of overloading, since the //parameters are of different types: int changeDate(float Year) ; int changeDate(int Year);

• Overidding: When a super class’s method is redefined in a subclass. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition(the different class) of the method. Overidding happens at runtime. • • Redefinition: When a subclass defines a method using the same name as a method in the super class but with a different type signature(parameters).


• • Summary of the forms of inheritance 1. Specialization: The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class.

• Specification: The parent class defines behavior that is implemented in the child class but not in the parent class. • Construction: The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class • Extension: The child class adds new functionality to the parent class, but does not change any inherited behavior. • Limitation: The child class restricts the use of some of the behavior inherited from the parent class. • Combination: The child class inherits features from more than one parent class. Although multiple inheritance is not supported directly by Java, it can be simulated in part by classes that use both inheritance and implementation of an interface, or implement two or more interfaces.


2015-02-05


2015-02-05


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.