Wednesday, September 03, 2014

OOD Review

Class and Object


A class is an abstract definition of something that has attributes (sometimes called properties or states) and actions (capabilities or methods). An object is a specific instance of a class that has its own state separate from any other object instance


Inheritance and Polymorphism


Inheritance enables a class to provide behavior for a more-specialized version of the class. When class B inherits from class A , class A is B’s parent or base class and class B is A’s subclass. All the behaviors defined by class A are also part of class B, though possibly in a modified form. In fact, an instance of class B can be used wherever an instance of class A is required.

Polymorphism is the ability to provide multiple implementations of an action and to select the correct implementation based on the surrounding context. 

For example, a class might define two versions of a method with different parameters. Or the same method might be defined both in a parent class and a subclass, the latter overriding the former for instances of the subclass. 

Method selection may occur when the code is compiled or when the application is run.

tostring() method in java.

Constructor and Destructor


in Java, constructor is a method with same  name as  class name. it is called while instantiate an object. destructor is a method called finalizer, which is called before  GC destroy the object. it gives object a chance  to change something that is related to its destruction.

Interface and Abstract Class

Interface declares a set of related  classes, out side of any class, independent of class hierarchy. Usually no variable declaration.

An abstract class is uncompleted class which can not be instantiated. it provides common functions, declaration of functions etc. it's in class hierarchy.

A class implement an interface while it extends virtual class.

Virtual Method

Heart of polymophism.

A virtual method is a method whose implementation is determined at run time based on the actual type (class) of the invoking object. Nonstatic Java methods are always virtual, in C# and C++, methods are only virtual when declared with the virtual keyword - nonvirtual methods are the default.

for  example, class B extends class A, and override method AMethod, while casting class B to class A and invoke AMethod, it is class B's implementation invokded instead of  class A's.

simply put, in Java, it uses the final version of implementation based on original type of instance.

Encapsulation

it is called information hiding. object expose essential information for manipulation,  but not all the internal details. like computer, everybody knows to use it with mouse, keyboard and screen, they do not need to know internally how it works.

it  is a way to combine related data and operations together and controls how data is accessed. it hides the detail of how data is  processed internally and it exposed only the chosen operations which is clean and simplified interface  for working with them.


The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Abstraction


it is a common set of features of subtypes.

Aggregation


it models relationship of whole and part. like zoo and  animals. their connections is not that strong.

Combination


it's an aggregation. but usually components connected closed to make an aggregate.  like human and its body parts.


No comments: