Showing posts with label OOPs. Show all posts

Different Styles of Programming

1. Overview

Purpose of this document

The purpose of this document is to help you understand the basics of different styles of programming. In my opinion, the best programming style is object oriented programming. In this document I will discuss different kinds of developing, like Functional Programming, Modular Programming and eventually, Object Oriented Programming. Of course, all keywords and relevant information belonging to each style will be discussed.
In this document, different styles of programming will be explained:
  • Functional programming
  • Modular programming
  • Object Oriented Programming

2. Functional programming

What is functional programming

Functional programming is a style of programming that emphasizes the evaluation of expressions rather than the execution of commands. This means that a program, written in a functional programming language, consists of a set function definitions and an expression, whose value is output as the program’s result. There are no side results (like an assignment) to expression evaluation, so an expression will always evaluate to the same value, if its evaluation terminates.

Because of this, an expression can always be replaced by its value without changing the overall result. This is called referential transparency.
The word ‘function’ actually can be replaced with the term ‘action’. A function executes something, for example Drive(brand_of_car)

Referential transparency

Pure functional languages achieve referential transparency by forbidding assignment to global variables. Each expression is a constant or a function application whose evaluation has no side effect, it only returns a value and that value depends only on the definition of the function and the values of its arguments.

3. Modular programming

What is modular programming

A module, in modular programming, is a series of functions (or procedures) that are related in some way. This way of programming is used in many applications.

Way of work

Given a problem, you begin with analysis of the problem. You carefully look at the requirements of the problem and you make sure all questions are answered before you begin with design of the modules. Then, you break the problem in subproblems. Each of these problems you solve in one or more modules. All the modules together solve the complete problem. Modules itself may be divided into smaller modules.

you divide the problem in sub-problems.
In the same way as you break the problem in smaller problems, you break a module in sub-modules.

A program (module) is divided in sub-modules.
As said, each sub-problem will be solved in the different sub-modules. In the end the whole problem is solved in the whole module.
A module is a set of functions (actions, see chapter two) that are related to each other. For example, a module called “Driving” contains the functions drive(brand_of_car), accelerate(speed) and stop() . A modulair program is always more clearly than a pure functional program because the different modules are classified on base of relevance.

References

Modular programming

http://www.eee.bham.ac.uk/dsvp_gr/roxby/eem1e1/lecture7/sld004.htm

Modular design and programming

http://www.cs.runet.edu/~jchase/pdl.html

4. Object Oriented Programming (OOP)

What is OOP?

Object Oriented Programming (or OOP) is a revolutionary concept that changed the rules in computer program development. OOP is organized around objects rather than actions. Normally, a program has a simple flow: input data, process the data and output a result. OOP has a different view; what we really care about are the objects we want to manipulate, not the logic required to manipulate them. Examples of such objects can be human beings (for example, described by name, address and so on), or buildings and floors (whose properties can be described and managed) or even little things, like a button or a toolbar.
Object Oriented Programming is an alternative to modular programming. The design technique associated with OOP is Object Oriented Design. In an object oriented program, the modules are classes rather then procedures. A class is a collection of objects.

Objects

As said, OOP is all about objects. An object is actually a container filled with information. You can see an object as a black box, which sends and receives messages. A black box, or object, contains code and data. Code are sequences of computer instructions, and data is information on which the instructions operate. In C ++ units code are called functions and units of data are called structures. Functions and structures are not connected to each other. For example, a function can operate on more than one structure, and a structure can be used in more than one function.
A structure can be public data, these can be accessed by another object. An object can also contain private data. This data can only be accessed by the object itself, and not by another object. The private data is implemented so that an outstanding object cannot modify data while it’s not necessary or data that is not supposed to be modified.

An object

Messages

All the communication between objects, is done by messages. An object sends, but also receives messages. The object, to which the message is sent, is the receiver of the message. Messages define the interface of the object. At least everything an object can do is represented by his messages. Actually, an object usually can do more, because an object also can have private functions.

Messages
Normally, messaging is done between the objects. One object receives a message from an other object, and sends a message back to the same, or to an other object.

This is just a simple presentation of messaging between the objects. Even in a small program, the amount of objects can be very large. In this example, the first object receives input. This can be a message from another object or, for example, something the user types. The first objects processes this input, sends a message to the second object. This message can be a result of a function, for example. The second objects processes the message and sends a result back to the first object. With this result, the first object does a new calculation, for example, and sends the result of this to the third object. This object also processes the data and gives output. This output can be a new message to another object, or simply print something on the user’s monitor.

Information hiding

Providing access to an object by sending and receiving messages, while keeping the details of the object private, is called information hiding. Another word, which means the same, is incapsulation. Information hiding is a good thing; one object should only know that things about another object that are relevant. Make members in an object private or protected when possible.

Methods

An object is defined by his class. A class determines everything about the object. Objects are individual instances of a class. It’s possible to have, for example, more buttons on one dialog. Each button is an instance of the buttonclass. Another example: consider the class ‘Dog’. An object from this class is called ‘Spot’. The ‘Dog’ class defines what the object is. It is possible to have more than one object of this class; you might want to call them ‘Fido’, ‘Rover’ and so on. Each one of them is an instance of the class ‘Dog’. Now, let’s say that the ‘Dog’ class defines they can understand messages like ‘Bark’ and ‘Roll-over’. The action that the message carries out is called a method. This is, in fact, the code that’s getting executed when the message is received by an object.

spot.bark();
Arguments are often supplied as a part of a message. For example, the message ‘Roll-over’, may need the arguments ‘how fast’ and a second argument, ‘how many times’. Arguments specify the way an action behaves.

Reuse

Inheritance also means, that you reuse. You don’t have to write all the code again, you just reuse one or more classes that have the behavior you want.
Now, what if we want to create a class (named ‘Wolf’) that can do exactly what ‘Dog’ does? We don’t have to rewrite the entire class; we just create a class derived from ‘Dog’. This class inherits all the existing messages of the so-called base-class; it has the same behavior.
In the class ‘Wolf’ we may create new (extra) messages, for example ‘hunt’. This class supports al the messages of ‘Dog’, plus some new ones, including ’hunt’.

Data modeling

Data modeling is a first step in designing an object oriented program. As a result of data modeling, you can define the objects. A simple approach to creating a data model that allows you to visualize the model, is to draw a square to represent each individual data item that you know about, and then to express relationships between each of these data items. The relationships between the data items, can be expressed with words like ‘is part of’, or ‘is used by’ or ‘uses’ and so forth. From this total description, you can create a set of classes and subclasses that define all the general relationships.
With this data model, you can easily create a set of classes or even a complete program, because this data model defines the working of the program.

Differences between OOP and functional & modular programming

The difference between OOP and functional and modular programming, is that the objects in OOP only manipulate their own condition. For example: Peugeot::Drive(), Citroen::Drive(), Car::Accelerate(speed), and car::Stop() . The condition of all the objects together specify the condition of the complete program. An object oriented program is always easier to read than a modular program, because all the objects represents their information. The way an OO program works can be defined by drawing the relations between the objects. That is exactly why it is always a good idea to draw a data model with the objects, and their relations, you need.
Besides that, an OO program is easier to maintain, because changing the functionality of an object will (usually) have no effect on the state of other objects. For implementing new functionality, it might be enough to simply add on or more objects to the application and define their relations.
The fact of reusing code can be seen with the possibility to base specialized information (Peugeot, Citroen) on general information (car).

Nonsense

In C we had to code our own bugs. In C++ we can inherit them.

References

Object Oriented Programming
http://www.whatis.com/oop.htm
What is Object Oriented Software:
http://www.soft-design.com/softinfo/objects.html

A look at what is wrong with OOD/OOP based on CPian responses to the question "What is wrong with objects".

Table Of Contents

Introduction
People Problems
        Training
        Ad hoc Methodologies
        Formal Education  
Concrete Technology Problems
        Technology Confusion
            More Than A Container
            To Inherit Or To Encapsulate, That Is The Question
            The Gene Pool -- Single Inheritance vs. Multiple Inheritance
            Is It An Object Or A Property
            An Interface Is Not The Same As Inheritance
            Are Objects Sufficiently Decomposed
 Relationship Counseling
            The Only Child Syndrome
            Having Affairs Leads To Entanglement, Divorce, And Alimony
Philosophical Problems
       
Abstraction And The Loss Of Information
            Representational Abstraction
            Model Abstraction
            Concept Abstraction
            Death By Abstraction 
There Is No Crystal Ball
            The Undiscovered Country
            The Consumer  
Reality Bites
            The Differences Are Too Great
            Concepts Do Not Translate To Implementation
            Data Entanglement
            Project Constraints
        The Atlas Design
    Conclusion

Introduction

When I asked "what's wrong with objects?" I received numerous responses, all of which were excellent and spanned the spectrum of software design from hard core implementation issues to philosophical issues. What surprised me the most was that from the original 17 responses, they almost all pointed out different problems. It seemed to me that here was an excellent collection of knowledge that was ripe to be compiled into a single repository. So that's what this article article is all about. A "thank you" to everyone that answered my question. I hope you find it entertaining as well as informative. Anything wrong or just plain stupid in this article is entirely my fault, not yours. If you disagree with something, let me know. Heck, there's a few things I disagree with myself, but if you don't take a position, how will you learn?

People Problems

Several CPians pointed out that object oriented programming (OOP) fails because of inadequate people skills.

Training

Training is a big factor. The learning curve might be expressed by the following graph. This is a fairly generic representation of a learning curve--for instance, it doesn't go into specialization, such as SQL, client-server, web applications, etc. I've put some effort into putting together what I think is a reasonable way of learning programming. For example, if I were to put together a curriculum on programming, it would probably look like this.

This above graph is only half the picture. Here's the other half (print them out and paste them together!):

The point being that once you are competent at the fundamentals, you can begin looking at design issues, reworking your code, and other techniques that strengthen your abilities as a programmer/designer. This learning curve is quite difficult because learning OOP is intimately involved with learning frameworks such as MFC, tools such as debuggers, and language enhancements (for lack of a better word) such as STL. In fact, learning OOP requires learning a lot of non-OOP concepts first!
For the sake of completeness, I'm going to try some one-liners describing each of these stages and expanding upon the Guru's Triangle.
Programming Fundamentals This is pure architecture and the mechanics of Von Neumann machines
Programming Tools Compilers, debuggers, source Control, defect trackers, profilers, etc
Language Fundamentals Programming fundamentals applied to a specific language syntax
Procedural Languages Writing good procedural code
API's and SDK's Learning the foundations of the platform
Encapsulation Reviewing procedural code from the perspective of encapsulating form and function
Modifiers The art of hiding internal implementations and exposing public interfaces
Polymorphism Taking advantage of strong typed languages
Inheritance-Specialization How to specialize the functions of class
Inheritance-Abstraction Is thinking abstractly something that can be taught?
Inheritance-single vs. multiple Single vs. Multiple inheritance issues
Interfaces An interface is an order to the programmer to implement functionality. It is not inheritance
Application Of Objects Thinking abstractly--Is it a baseball object or a ball with the property values of a baseball?
Application Of Objects "is a kind of" vs. "has a" issues
Templates/Generics/Meta-data Yet another layer of abstraction
STL A can of worms--very useful, but easily abused and misapplied
Frameworks MFC, .NET (there are no other planets with intelligent life on them)
Refactoring, Level I Intra-method refactoring--Fixing expressions, loops, conditionals, etc.
Refactoring, Level II Intra-object refactoring--Fixing internal object implementation problems
Design Patterns, Level I Creational patterns--These are the easiest to get one's arms around
Refactoring, Level III Inter-object refactoring--Fixing hierarchies
Design Patterns, Level II Structural patterns--decoupling object dependencies
Refactoring, Level IV Design pattern refactoring--hope you never have to do this
Design Patterns, Level III Behavioral patterns--Learning to identify behaviors in your code and decoupling the generic behavior from the specific implementation

Ad hoc Methodologies

The posts I've made in the past regarding methods such as eXtreme Programming have met with a variety of responses, mostly negative. I find it disturbing (yet unsurprising) that it seems that every generation has to re-learn the mistakes of the past. I'm not particularly convinced that we're moving in a forward direction when it comes to things like improving quality and customer relationships, not to mention reducing bugs, meeting deadlines, and accurately estimating projects. It seems more like we're moving sideways. TQM, ISO 9001, CMM, Agile methods, and a variety of others all seem to address the same problem from a different angle. Every five years or so there's a new fad in "how to write programs successfully".
But what's really disturbing is that, from my dialogs with other programmers, it seems very few of them actually know anything about any of these methodologies. It appears that there's quite a bit of ad hoc software development going on out there. I think it's time to stop moving sideways and move forward instead. Books on refactoring and design patterns are a step in the right direction, in my opinion, because they provide concrete (or mostly concrete) tools for programmers to use that will actually make them better programmers. But the concepts are also hard to learn. Which is why I'm so keen on things like automation and developing a framework that enforces good programming practices. Easier said than done, and there's trade-offs too--the programmer loses a degree of creativity and control when the framework forces him/her to interact with other code in a very specific way. And worse, who's to say that what I'm doing is really a forward solution? It may be sideways, or even, heaven forbid, a step backwards!

Formal Education

What happened to formal education? People get degrees nowadays to get jobs and better pay rather than actually learning something. Learning seems to have taken second seat in the orchestra of life. But there's some justification to this also. My experiences with institutes of higher learning here in the United States (and in particular, California), has been terrible. Antiquated machinery, obsolete programming courses using out of date operating systems and tools, and professors that grade on "following directions" rather than solutions that try to be efficient and high performance. And the biggest complaint of all--colleges and universities haven't a clue what real-life programming is like. Sadly, formal education seems to be a necessary evil at this point, at least in the computer science field. It used to be that universities were the leaders in innovation and research. Now they seem more a haven for the socially inept and "education" leaves one technically incompetent.

Concrete Technology Problems

Technology Confusion

Many people pointed out that there's a lot of confusion with regards to using OOP. Object oriented design (OOD) only provides guidelines. There's a lot about OOD that is fuzzy and learned only through painful experience.

More Than A Container?


During the course of computer architecture development, data and instruction have become separated physically (in address space), providing the means to create multithreaded, multi-tasking systems that are protected from stepping on each other. Objects serve to unify the divergence of data and instruction. Yes, they're still physically separated and confined to the application and it's data, but the point is that an object binds together data and operations into a logical structure. So, is that all an object is? Yes, with regards to an object itself, that's it. It's when objects are used in relation to other objects that things get more interesting, and more complicated.
Now, the diagram above is a bit silly, but I think it's also illustrative of the evolution in both hardware and software architectures. The last column, "Aspect Computing", is something I have totally invented, but it seems to me that as distributed services develop and components/component communication is standardized, a significant amount of "programming" will simply be gluing together disparate components, handling events, and coordinating the flow of data between them.

To Inherit Or To Encapsulate, That Is The Question

The first thing I learned when designing an object structure was to ask myself, is object A "a kind of" object B, or does it "have" an object B. Unfortunately, this is just the tip of the iceberg. Many relationships are not clearly defined and are sometimes easily seen as both. For example, in GUI design, if I want a customized control, should the custom control be "a kind of" the generic control, or should it "have a" generic control? There are pros and cons to both.
If the object is derived:

then the user (another programmer) most likely (unless the base class is declared as private in the derived class) has complete access to the base class methods and the custom control needs to ensure that it handles all the functionality of the base class.
Listing 1: Inheritance
class BaseClass
{
public:
virtual ~BaseClass() {}

void Foo(void) {}
};

class DerivedClass : public BaseClass
{
public:
void Bar(void) {}
};

// usage:

DerivedClass* dc=new DerivedClass();
dc->Foo();
dc->Bar();

If the control is contained:

then access to the generic control can be strictly controlled (no pun intended).
Listing 2: Encapsulation
class InnerClass
{
public:
// note the missing virtual destructor
void Foo(void) {}
};

class OuterClass
{
private:
// private restricts access by derived classes
InnerClass ic;

public:
void Bar(void) {}
};
To make matters worse, depending on how the base control was implemented, the programmer may not have a choice. Non-virtual destructors, methods that aren't declared as virtual, and private members often makes it impossible to specialize an object through derivation.
Another concern is the trade-off between growing a rigid inheritance structure vs. growing a highly dependent associative structure. In both cases there is a strong coupling between objects. However, with an inheritance structure, the base classes can easily break the application by changing implementation.

This problem is slightly mitigated with encapsulation because encapsulation allows you to implement wrappers around other objects, whereas inheritance does not.

Wrappers are an important way of protecting yourself from changes made in other objects. Depending on the implementation of the base class (for example, .NET's Form class, it may be necessary to implement a derived class to gain access to protected methods and encapsulate the specialized class.
Inheritance also locks you into a structure that may become inadequate over time. It may become desirable to specialize the functions of two or more objects (multiple inheritance) or to change the behavior of the object by deriving it from something else. Especially with languages that do not support multiple inheritance (like C#) or don't support the issues around duplicate methods implemented by both classes (such as in C++), encapsulation may be a better alternative.
As you can see, the decision to inherit vs. encapsulate is more than just a simple relationship decision. It involves questions of access, support, design, extensibility, and flexibility.

The Gene Pool -- Single Inheritance vs. Multiple Inheritance

Consider three different kinds of balls:
  • golf ball
  • basketball
  • tennis ball
which are neatly represented by the following classes, all derived from Ball:
Listing 3: What appears to be a reasonable class structure
class Ball
{
public:

Ball(void) {}
virtual ~Ball() {}

virtual void WhoAmI(void) {printf("Ball\r\n");}
virtual void Bounce(void) {printf("Ball\r\n");} 

double diameter;
}

class GolfBall : Ball {}
class BasketBall : Ball {}
class TennisBall : Ball {}
Each of the specialized versions implements some additional properties specific to it. For example:
Listing 4: The GolfBall derivation includes some specialization
class GolfBall : Ball
{
public: 

GolfBall(void) : public Ball() {}
virtual ~GolfBall() {}

virtual void WhoAmI(void) {printf("GolfBall\r\n");}
virtual void Bounce(void) {printf("GolfBall\r\n");}

DimplePattern p;
Compression c;
}
(from http://news.bbc.co.uk/sportacademy/bsp/hi/golf/equipment/other_gear/html/ball.stm and http://www.golftoday.co.uk/clubhouse/library/qandaballs.html) Listing 5: The BasketBall derivation includes some specialization
class BasketBall : public Ball
{
public:

BasketBall(void) : Ball() {}
virtual ~BasketBall() {}

virtual void WhoAmI(void) {printf("BasketBall\r\n");}
virtual void Bounce(void) {printf("BasketBall\r\n");}

Category cat;
ChannelSize csize;
Color color;
}
(from https://www.sportime.com/products/smartbasketballs.jsp) Listing 6: The TennisBall derivation includes some specialization
class TennisBall : public Ball
{
public:

TennisBall(void) : Ball() {}
virtual ~TennisBall() {}

virtual void WhoAmI(void) {printf("TennisBall\r\n");}
virtual void Bounce(void) {printf("TennisBall\r\n");} 

Speed speed;
Felt feltType;
Bounce bounce;
}
(from http://tennis.about.com/library/blfaq22.htm) Now let's say that we want a new ball, a GobaskisBall ball, that combines the compression of a golf ball, the color of a basket ball, and the felt type of a tennis ball. From a novice point of view, using the idea that we're creating a specialized ball with characteristics of three other balls, the Gobaskis ball might be constructed as this:
Listing 7: Deriving a new ball with features of all of the other three
class GobaskisBall : public GolfBall, public BasketBall, public TennisBall
{
public:
GobaskiBall(void) : GolfBall(), BasketBall(), TennisBall() {}
virtual ~GobaskiBall() {}

virtual void WhoAmI(void) {printf("GobaskiBall\r\n");}
virtual void Bounce(void) {printf("GobaskiBall\r\n");}
}
which is the wrong application of inheritance because it creates the "diamond of death":

(I'll add some explanation of this shortly.  For now, let's just say I did have something written, but I made a really really stupid mistake and it was best to erase it before the rest of you all noticed!) 

Is It An Object Or A Property?

One of things that's really confusing when creating an object model is determining what is an object and what is a property. There is contention between how the real world models things and how they are best modeled in an OOP language. Using the example above, also consider that the inheritance model is inefficient and will probably result in a lot of dependant code being recompiled when additional properties are added.
In the above example, to avoid multiple inheritance issues, the programmer will most likely put all the properties of interest into a new class derived from Ball.
Listing 9: Putting the properties in a new class derived from Ball
class GobaskisBall : public Ball
{
Compression c;
Color color;
Felt feltType; 
}
But the astute programmer will realize that these properties are pretty generic. Not only can they be put together in a variety of combinations, but the "requirements" for the ball might change as the application is developed. This leads to the realization that having specialized ball classes is probably the wrong way to go. What is needed instead is a single ball class that has a collection of ball properties. A "ball factory" method can then construct whatever specific ball instance is needed, both in terms of the properties that describe the ball and the value of those properties:
Listing 10: Abstracting the concept of the ball and creating using collections instead
class Ball
{
Collection ballProperties;
}
We have now eliminated the specialization and created something called a "collection". (There's more going on here too, which is described in the section "Death By Abstraction" below). As I have hopefully illustrated with the examples above, the concept of inheritance doesn't work all the time, and using abstraction and collections creates design time decisions as to how to implement an object model--is the object actually a first class object or is the concept of the object better expressed as properties? This is not an easy answer, and there is no right answer.

An Interface Is Not The Same As Inheritance

A base class can contain fields and default implementations. An interface contains neither, requiring the derived class to provide all the implementation. For example, using C#, all the methods and properties of IBall have to be implemented.
Listing 11: Interfaces require implementation
interface IBall
{
void Bounce();
double Diameter {get; set;}
}

public class TennisBall : IBall
{
private double diameter;

public double Diameter
{
get
{
return diameter;
}

set
{
diameter=value;
}
}

public void Bounce() {}
}
As illustrated in this example, making an interface class out of the Ball has its problems. For example, a default function behavior for Bounce cannot be implemented. Even worse, each class has to implement it's own Diameter property and access methods. Microsoft's solution to this is to automate the IDE so that you can press the Tab key and it will generate all the interface stubs. Woohoo. This does nothing for language constraints and/or bad object models. (Yes, I grudgingly will admit that it does force you into some good practices, such as wrapping fields with get/set property access methods.) At this point, we have the worst of both worlds, in my opinion--a language that doesn't support multiple inheritance, and an interface class construct that requires lots of duplications in code. So what's the alternative?
First, interface classes should not be viewed as a solution for multiple inheritance. Instead, they should be viewed as an implementation requirement. This may be obvious to the experienced programmer, but it isn't to the junior programmer. In fact, I clearly remember the Javaheads I used to work with saying that interfaces were a replacement for multiple inheritance. This is not true, in my opinion. The phrase "class Foo implements the interface Biz" clearly shows what interfaces are useful for--implementation of functionality. For this reason, interfaces should also be very small--they should only specify the functionality required the implement a very "vertical" requirement. (OK, that's Marc's recommendation).
The problem still remains though of how to specify default behavior and how to employ the ease of specifying a group of parameters that all similar (as in derived) classes can include in their own specialization. Obviously, this can be done by using a class instead of an interface, but this may not be the desirable approach. Something has to give in a single-inheritance language. Conversely, maybe the question should be: "is this idea of having default behaviors and a collection of default fields such a good idea?" And the answer to this is probably "no, it's actually not a good idea". But to understand "why not" isn't easily conveyed when someone is just learning about OOP. Read on...

Are Objects Sufficiently Decomposed?

One day a young fellow was walking home from the university where he was studying for a degree in music. As was his custom, he always walked by a graveyard. On this particular day, he heard some strange music coming from the graveyard. It seemed oddly familiar, but he just couldn't place it. This happened again the next day. On the third day, he brought his professor along to help him identify the music. The professor exclaimed, "Why, that's Beethoven decomposing!"
First off, in the above example, the IBall interface is too complicated--it hasn't been sufficiently decomposed. What should really exist is:
Listing 12: Decomposing IBall
interface IBounceAction
{
void Bounce();
}
and
interface IBallMetrics
{
double Diameter {get; set;};
}
This separates the shape of the ball from the an action, such as bouncing. This design change improves the extensibility of the application when, for example, a football is required, which is not a spherical shape.
Secondly, and this is perhaps the harder case to argue, the concept of "diameter" should be abstracted. Why? Because of unforeseeable dependencies on the meaning of this value. For example, it may be calculated from the radius. The circumference or volume of the ball may be calculated from the diameter or the radius. Or given the volume, the diameter can be calculated. The application becomes more flexible by making the concept of "diameter" more flexible. Hence, diameter shouldn't just be an integral type, it should perhaps be an object in its own right:
Listing 13: Making "diameter" a first class citizen
public class RoundBallMetrics
{
private double d;

public double Diameter {get {return d;} set {d=value;}}
public double Radius {get {return d/2;} set {d=value*2;}}
public double Circumference {get {return d*Math.PI;} set {d=value/Math.PI;}}
public double Volume 
{
get {return (4.0/3.0)*Math.PI*Math.Pow(d/2, 3);}
set {d=2*Math.Pow((3.0*value)/(4.0*Math.PI), 1.0/3.0);}
}
public double SurfaceArea
{
get {return 4.0*Math.PI*(d/2)*(d/2);}
set {d=2*Math.Sqrt(value/(4.0*Math.PI));}
}
}
(Note, I haven't tested these equations for accuracy!) To digress for a very long moment, we now have a nice class that could use an interface (yes, an interface!) so that anyone implementing the metrics of a ball has to implement these functions. But we have to be careful, because some balls aren't round, having a major axis and a minor axis like a football, which means that the diameter, radius, and circumference are different depending on the axis. By using an interface, we can specify that certain functions need to be implemented while leaving that implementation to the needs of the application. Solving only the round ball problem, the interfaces might look like this:
Listing 14: Some useful interfaces for ball metrics
interface I3DMetrics
{
double Volume {get; set;}
double SurfaceArea {get; set;}
}

interface I2DMetrics
{
double Radius {get; set;}
double Diameter {get; set;}
double Circumference {get; set;}
}

// ooh, look. Sort-of multiple inheritance!
interface IBallMetrics : I2DMetrics, I3DMetrics
{
// adds nothing
}

public class RoundBallMetrics : IBallMetrics
{...}
Furthermore, this class can now also implement streaming, integration with the Forms designer, and other useful features (most of which make the class entangled with other objects though--tradeoffs, it's always about tradeoffs).
Back to the point. We now have a nice class for dealing with the diameter of a round ball. The interface IBall now needs to only implement a getter function which returns a first class ball metrics citizen. This allows for different ball shapes and sizes:
Listing 15: A possible ball implementation
interface IBall
{
IBallMetrics {get;}
}

public class RoundBall : IBall, IBounceAction
{
private RoundBallMetrics rbm;

public IBallMetrics {get {return rbm;}}
public void Bounce() {}
}
Now that we've made a ball with an abstraction to handle its metrics, the problem of default data and default implementation with regards to interfaces has resolved itself--there is no such thing! Ever! If you want default properties or implementation, then you have to implement that behavior in a base class and specialize from that class, which leads to all sorts of problems in both single inheritance and multiple inheritance languages. So the rule of thumb is, don't implement default properties and methods.
In conclusion of this section, I can only say that I hope I have demonstrated adequately the technical challenges involved in the issues of managing properties, objects, interfaces, inheritance, decomposition, and collection. Every application is a unique challenge in this regard.

Relationship Counseling

Relationships between different families is just as important as the relationships within your family. Sad but true, getting involved with other families often means that you usually get ensnared in their politics and are often asked to "take sides", being an unbiased observer (or so they think!)

The "Only Child" Syndrome

In object models, we have parents and children, but we don't have brothers and sisters. Because object models are hierarchies, they are an incomplete model of relationships. This gets the programmer in all sorts of trouble when relationships between families must be created. Consider the following hierarchies and their relationships to each other:

Object models do a great job of representing hierarchies, but they don't address the problem of relationships between objects within the syntax of the C# or C++ language. For that, you have to look at things like Design Patterns. A useful design pattern to talk to your siblings is the telephone. Call them up (they probably won't answer anyways) and leave a message:

Having Affairs Leads To Entanglement, Divorce, And Alimony

Messaging is an excellent way of avoiding entanglement, but it also requires an infrastructure in which synchronization, workflow, and timeouts have to be handled (and it makes itself amenable to worker threads as well). For example, when the login process sends a message to the database to lookup the user, it must wait for a response or timeout if no response is received with a certain amount of time. Other design patterns also decouple, or disentangle, objects from each other while working within the same thread, thus eliminating the need to handle synchronization and other related issues.
Entanglement results in the death of a design. When I look at an entangled application, I usually tell my client that it's easier to rewrite it than to refactor it. Entangled object relationships must be refactored early in order to rescue the project. The objects must be divorced from each other, and this usually involves a lot of expense on the part of both parties--lawyer fees, court room battles, etc. The best thing is not to get entangled in the first place. Using a messaging system, for example, maintains an acquaintance relationship rather than creating an intimate love affair. Yes I know, it's boring.

Philosophical Problems

Besides people and technical problems, there's a lot of philosophical problems with OOP, mostly related to the way the real world works.

Abstraction And The Loss Of Information

Abstraction is generalization, and when generalization occurs, information is not just moved, it's actually lost. One of the hazards of abstraction is that information that would normally be contained in the structure is lost as the structure is abstracted. Instead, all the information is in the data. While this makes the program more flexible (because it can handle different kinds of data), it also makes the objects more obtuse--how should they really be used?

Representational Abstraction

The ultimate representational abstraction in C# (and yes, in Java) is the object. In C++, it's the void pointer. An object can be anything, and since saying:
object o;
is not informative at all, the concept of "reflection" is necessary so that o can find out what it is: Listing 16: Obtaining object type information
object o=new RoundBallDiameter(); 
Type t=o.GetType();
Console.WriteLine(t.ToString());
Answer: interfaceExample.RoundBallDiameter

Model Abstraction

Hierarchies create abstraction, but here the information loss isn't as critical because it's not so much the concept that's abstracted, but the container for the concept. The abstracted container still retains the information regarding what can be done with the object. For example, in this hiearchy:

the Window class is fairly abstract, but it can still contain methods to manipulate for:
  • position
  • text
  • selection events
  • background color
  • border style
  • etc...
and thus the contextual information is preserved even if the container itself is abstracted.

Concept Abstraction

Conceptual abstraction is where the concept that you are implementing is abstracted. Instead of the abstraction being represented in a hierarchy, the abstraction is represented in a single container using collections to manage the properties and methods of the concept. This kind of an object can represent anything, including other objects. From the mundane, such as a Matrix class:
Listing 17: A Matrix class
public class Matrix
{
private ArrayList columnList;
private int cols;
private int rows;

public ColumnList this[int col]
{
get
{
return (ColumnList)columnList[col];
}
}
...
}

public class ColumnList
{
private ArrayList rows;

public object this[int row]
{
get
{
return ((Cell)rows[row]).Val;
}
set
{
((Cell)rows[row]).Val=value;
}
}
...
}
which can be used to manage any two dimensional data (similar to a RecordSet) to the absurd (but still sometimes necessary):
Listing 18: Abstracting the concept of an object
public class AbstractObject
{
public string name;
public Collection methods;
public Collection properties;
public Collection events;
}
conceptual abstraction loses all the information about the concept itself. While powerful, it's often inefficient. But sometimes it's inescapable. When I was working on a project to automate satellite designs, I was forced to implement a highly abstract model--the Category-Attribute-Unit-Type-Value (CAUTV) model in order to fully capture the desired concepts:

In this model (actually, this is only a piece of a much more complex model involving assemblies, automatic unit conversions, components, etc.), a piece of equipment belongs to a category, such as a travelling wave tube amplifier (TWTA). The TWTA has attributes, such as mass, power, thermal loss, amplification, and noise. Each of these attributes is measured in a particular default unit, such as kilograms, watts, milliwatts, etc. Each unit might have one or more types--minimum, maximum, nominal, average, and so forth. A particular instance of the equipment is then composed of a value which combines the unit, type, and attribute. Because this information could not be quantified by the engineers (and in fact was different depending on equipment manufacturer), a very abstract model had to be constructed to support the requirements.
As should be fairly evident, this abstraction has resulted in the complete loss of contextual information. A model like this could be just as easily be used to describe satellite equipment as it could to describe a fantasy role-playing character. It also becomes much harder to convey the application of the model. This kind of abstraction requires "explanation by example" in order to understand the context in which the model lives. Less abstract models communicate their intent through "explanation by definition".

Death By Abstraction

It is entirely possible to over-abstract. My rule of thumb is to consider a concrete concept, then consider the first two levels of abstraction. Usually only one level of abstraction suffices and is useful. Going beyond the first abstraction can lead to a lot of confusion regarding the appropriate application of an abstracted object. At its worse, I sometimes find myself abstracting an abstraction, only to realize that I haven't changed anything but the names of the classes!

There Is No Crystal Ball


Programming is a lot like predicting the future. I think there's two areas of prediction--the functional area and the implementation. Coming up with a feature set for an application is a prediction on what will be considered useful in the future. A statistic I came across once is that 90% of the people only use 10% of the features of a word processor, but that 10% is different depending on who you ask. So predicting the customer's needs, especially as the rest of the world is rapidly changing, is difficult. But the crystal ball has another purpose too. It has to tell the programmer how the code will be used in the future too. So in reality, the programmer has to make educated guesses in two areas--what's important to the customer, and what's important to his/her peers. This second area I'll elaborate on a bit.

The Undiscovered Country

Writing code, whether the intention is to make it re-usable or not, requires predicting the future. There are many considerations when predicting how the code will be used, including design issues, implementation issues, and communication issues. Some of the things that might be considered when writing code:
  • are the inputs validated?
  • are the outputs validated?
  • can I rely on the components to which I'm interfacing?
  • have I considered all of the ways this code might be used?
  • is the design and the implementation flexible enough to handle requirement changes?
  • is it documented sufficiently so that I or someone else can figure out what I did?
  • will the code survive the next leap in technology, both hardware and software?
A lot of these answers require making tradeoffs. Sometimes the wrong tradeoff is made, but only in hindsight. What COBOL programmer would have thought that their code would still be in use at the turn of the century, when saving two bytes on a year record was added up to a lot of saved disk space? How many old PC's (even by today's standards) will still be operational when the year 2038 comes around, and the hardware clocks all reset to 1970? Who could have predicted when I wrote the remote video surveillance software in DOS in 1987 that the Japanese police department would be our biggest customer and that they wanted to program in Japanese? Unicode and 16 bit characters weren't even around, and Windows 3.1 wasn't an environment for high performance graphic applications! And how much of this code was re-usable when processor and graphic performance got good enough to migrate the code to Windows?

The Consumer

Predicting changes in technology and the lifetime of the application is one problem, but the other problem is predicting the needs of one's peers. Programmers are producers--they make things. Other programmers are consumers--they use things that other programmers have written. As a consultant, I'm like the snake eating it's tail--I consume a lot of what I produce. Predicting what I'm going to need in the future and writing the code well enough so that what I write is useful to myself later on is hard enough! Imagine trying to do this for someone else. But that's what we constantly try to do. In part, the psychology behind this is that we'd like something of ourselves to live on after the project is completed (a sort of mini-death). It's also a challenge--can I write something that someone else will find useful enough to use in the future? And it's ego stroking--something I did is useful by someone else.
All of these factors cloud the fact that the consumer (the next programmer) wants her own legacy, her own challenge, her own ego stroked. So what happens? Old code is criticized, condemned, and complained about (the Dale Carnegie three C's), followed by enthusiastic "I can do it better" statements. Maybe it's true, and maybe it's not. But the fact remains that programmers, being an anti-social lot, are actually fierce competitors when it comes to implementation re-use.

Reality Bites

Besides the psychology of re-use, there's a lot of good reasons for why re-use fails. If you're wondering why I'm talking about re-use, it's because that's one of the "side-effects" that object technologies are supposed to help with--objects are re-usable, cutting down on development costs and debugging costs.

The Differences Are Too Great


There are really two layers of code--application specific and application generic. Re-use is going to happen only in the application generic area. When application code is highly entangled and/or has poor abstraction, the amount of re-use is going to be minimal because the objects are bound too tightly to the application-specific domain. This of course is not the fault of objects, but in and of themselves, they do nothing to prevent this. However, objects can be used to disentangle code, such as implementing design patterns, creating abstractions, and wrapping framework implementations such as MFC and .NET with proxies and facades. The approach that I have found that produces a high level of re-use involves componentizing the various objects and using a framework in which I write simple scripts to orchestrate the flow of information between all the components. This is the "Aspect Computing" concept in the diagram at the beginning of the article.

Concepts Do Not Translate To Implementation


Concepts do not translate well into implementation because the concept does not address the low level of detail that is required during implementation. For this reason, it's impossible to predict how much time an application will take to develop. Certainly it's possible to produce a highly detailed design, but another thing that I've found over and over again is that implementation often reveals problems with the design. The design might look great on paper, but implementation shows problems. This extends beyond the design to the concept itself. On numerous occasions I have discovered flaws in the concept because during implementation I better understand the problem that's really trying to be solved. It can be argued that this is a result of inadequate communication, poor training, poor documentation, not enough time invested in explaining the concept, etc. This may be true, but I will counter-argue that when implementation begins, no matter how well the concept is defined and the design supports that concept, problems will occur.

Data Entanglement

Just as objects can be entangled with each other, entanglement occurs between the objects and the data that drives the objects. For example, in the AAL, there's a deep entanglement regarding the XML schemas (XSD) and functionality of the components. Other dependencies exist--as CPian Brit pointed out, a dialog is dependant upon the dialog layout, the string table, bitmaps, and resource ID's. All of this results in the object being non-portable. Again, this is not the fault of objects. However, it deeply affects issues revolving around objects.

Project Constraints


This should be a no-brainer, so I'm just putting it in here for completeness. Sometimes re-use, disentanglement, abstraction, design patterns, refactoring, etc., all succumb to the constraints of a project, usually time and money. The tug on a project is represented by the above triangle of features, resources (time, money, people), and quality.

The Atlas Design


According to most world concepts, there's someone holding up the earth in the heavens. For the Greeks, that person is Atlas. Atlas can be seen as the framework in which the application design lives. Many applications don't have an encompassing framework, or they rely entirely on existing frameworks, such as MFC, COM, CORBA, ActiveX, and/or dot-NET, plus a myriad of others. In my experiences, the concept of the "Atlas Design"--a meta-design on which the application is built, is rarely considered. The expertise, vision, or funding may be lacking. In one company the vision and funding was there, but the expertise wasn't, which resulted in a truly crippling framework which the programmers found all sorts of ways to circumvent because it was so difficult and annoying to use. In my other articles, I have written about the Application Automation Layer. The AAL is an "Atlas Design"--it is a framework on which all other frameworks can live. Essentially, it creates components out of those frameworks, and often creates components out of subsets of those frameworks.

Conclusion

OK, there isn't anything actually wrong with objects. They solve a particular problem, and they're pretty good at it. But perhaps we're expecting more solutions than objects have to offer. Or perhaps the field of software development is still so new that nobody really knows how to do it. It's pretty clear that the problem doesn't lie with objects, but with people applying them and the tasks they are asked to perform. If I want to win the Indy500, I can't do it with a VW Beetle. It's time to look at who's holding up the world.

Object Oriented Programming Concepts

Introduction

This article provides a brief description about the various Object Oriented Programming concepts.

Object Oriented Programming

It is a type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.

Object

Objects are the basic run-time entities in an object-oriented system. Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages. Different objects can also interact with each other without knowing the details of their data or code.
An object is an instance of a class. A class must be instantiated into an object before it can be used in the software. More than one instance of the same class can be in existence at any one time.

Class

A class is a collection of objects of a similar type. Once a class is defined, any number of objects can be created which belong to that class. A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.

Instance

The instance is the actual object created at runtime. One can have an instance of a class or a particular object.

State

The set of values of the attributes of a particular object is called its state. The object consists of state and the behaviour that's defined in the object's class.

Method

Method describes the object’s abilities. A Dog has the ability to bark. So bark() is one of the methods of the Dog class.

Message Passing

The process by which an object sends data to another object or asks the other object to invoke a method. Message passing corresponds to "method calling".

Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

Encapsulation

It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short, it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data. The act of placing data and the operations that perform on that data in the same class. The class then becomes the 'capsule' or container for the data and operations.
Storing data and functions in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.

Inheritance

It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.

Polymorphism

Polymorphism means the ability to take more than one form. An operation may exhibit different behaviours in different instances. The behaviour depends on the data types used in the operation.
It is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. In general, polymorphism means "one interface, multiple methods", This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler's job to select the specific action (that is, method) as it applies to each situation.

Generalization

Generalization describes an is-a relationship which represent a hierarchy between classes of objects. Eg:- a "fruit" is a generalization of "apple", "orange", "mango" and many others. animal is the generalization of pet.

Specialization

Specialization means an object can inherit the common state and behavior of a generic object. However, each object needs to define its own special and particular state and behavior. Specialization means to subclass. animal is the generalization and pet is the specialization, indicating that a pet is a special kind of animal.

Advantages of OOP

Object-Oriented Programming has the following advantages over conventional approaches:
  1. OOP provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly defined interface.
  2. OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
  3. OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer.

OOP & C#

OOP & C#

   The skeleton of object - oriented programming is of course the concepts of class. This C# tutorial on OOPS explains classes and their importance in implementation of object oriented principles.
   Any language can be called object oriented if it has data and method that use data encapsulated in items named objects. An object oriented programming method has many advantages, some of them are flexibility and code reusability.
   All the programming languages supporting Object oriented Programming will be supporting these three main concepts:
  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Encapsulation in C#:

   Encapsulation is process of keeping data and methods together inside objects. In this way developer must define some methods of object's interaction. In C# , encapsulation is realized through the classes. A Class can contain data structures and methods. Consider the following class.

public class Aperture
{

public Aperture()
{

}

protected double height;
protected double width;
protected double thickness;

public double GetVolume()
{

double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}
}

   In this example we encapsulate some data such as height, width, thickness and method GetVolume. Other methods or objects can interact with this object through methods that have public access modifier. It must be done using . operator.

Inheritance in C#:

   In a few words, Inheritance is the process of creation new classes from already existing classes. The inheritance feature allows us to reuse some parts of code. So, now we have some derived class that inherits base class's members. Consider the following code snippet:

public class Door : Aperture
{

public Door() : base()
{

}

public bool isOutside = true;
}

   As you see to inherit one class from another, we need to write base class name after : symbol. Next thing that was done in code Door () constructor also inherits base class constructor. And at last we add new private field. All members of Aperture class are also in Door class. We can inherit all the members that has access modifier higher than protected.

Polymorphism in C#:

   Polymorphism is possibility to change behavior with objects depending of object's data type. In C# polymorphism realizes through the using of keyword virtual and override. Let look on the example of code:

public virtual void Out()
{
   Console.WriteLine("Aperture virtual method called");
}
//This method is defined in Aperture class.
public override void Out()
{
   Console.WriteLine("Door virtual method called");
}


   Now we need to re-define it in our derived Door class. The usage of virtual methods can be clarified when we creating an instance of derived class from the base class:

Aperture ap = new Door();
ap.Out();
   In such cases, the runtime keeps record of all the virtual function details in a table called VMT(Virtual Method Table) and then in runtime dynamically picks the correct version of the function to be used. Here it uses Out() method from derived class of course.
   To compile the attached example you need to run .NET console and run the next command: csc filename.cs .

Key Object-Oriented Concepts and how to use them in Visual C#

Abstract
This paper is an overview of Object-Oriented concepts and how to implement them in Visual C#. We begin with an overview of Object-Oriented concepts in C# and then represent an detailed example in Visual C# that shows how to implement them.

Classes
In Visual C#, all code is contained in a class. Some members of classes are described in Table 1:
Member
Description
Example
Fields

Fields store the data a class needs to fulfill its design.
Fields are declared within the class block by specifying the access level of the field, followed by the type of the field, followed by the name of the field.

private double seconds;
Properties

Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields.
Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.
public double Hours
{
get { return seconds /
3600; }
set { seconds = value
* 3600; }
}
Methods

A method is a code block containing a series of statements.
public void AddGas(int gallons) { }

Events

An event is a way for a class to provide notifications when something of interest happens.
For example, a class that encapsulates a user interface control might define an event to occur when the user clicks on the control.
The control class does not care what happens when the button is clicked, but it does need to tell derived classes that the click event has occurred. The derived classes can then choose how to respond.


Table 1 – Some members of classes

Example: This example shows how to implement members of a class.
public class List
{
const int defaultCapacity = 4;
Constant
object[] items;
int count;
Fields
public List(): List(defaultCapacity) {}
public List(int capacity) {
items = new object[capacity];
}
Constructors
public int Count {
get { return count; }
}
public string Capacity {
get {
return items.Length;
}
set {
if (value < count) value = count;
if (value != items.Length) {
object[] newItems = new object[value];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;
}
}
}
Properties
public object this[int index] {
get {
return items[index];
}
set {
items[index] = value;
OnListChange();
}
}
Indexer
public void Add(object item) {
if (count == Capacity) Capacity = count * 2;
items[count] = item;
count++;
OnChanged();
}
protected virtual void OnChanged() {
if (Changed != null) Changed(this, EventArgs.Empty);
}
public override bool Equals(object other) {
return Equals(this, other as List);
}
static bool Equals(List a, List b) {
if (a == null) return b == null;
if (b == null || a.count != b.count) return false;
for (int i = 0; i < a.count; i++) {
if (!object.Equals(a.items[i], b.items[i])) {
return false;
}
}
return true;
}
Methods
public event EventHandler Changed;
Event
public static bool operator ==(List a, List b) {
return Equals(a, b);
}
public static bool operator !=(List a, List b) {
return !Equals(a, b);
}
Operators
}

If you want to create methods or properties that can be called without first creating an object, declare those items as static. (Equivalent of a Visual Basic .NET module)
Visual C# uses six key concepts for working with classes. Table 2 describes these key concepts.

Concept
In Visual C#
Definition
» You define classes using the class keyword.
» All executable code is part of a class.

Access
» There are five levels of access to classes and their members:
  • public
  • internal
  • protected
  • protected internal
  • private
» For more information, refer to table 3

Inheritance
Classes can inherit members from base classes and override or overload members of the inherited class.

Constructors and destructors
» Classes have constructors and destructors that are called when an object based on the class is created or destructors.
» Constructor methods have the same name as their class.
» Destructor methods use the class name preceded by a tilde (~).

Delegates
» The delegates keyword provides a safe way to call methods by their address rather than by their name.
» This is the .NET equivalent of a callback.
» Delegates are commonly used with events and asynchronous procedures.

Abstract classes and interfaces
» You can create interfaces and abstract classes.
» Interfaces define the member names and member parameter lists for classes that use the interface.
» Abstract classes provide the members to be inherited by classes derived from them.
Table 2 – Key Object-Oriented Concepts





Creating Classes and Providing Access
In Visual C#, use the class keyword to define classes. Use on of the access keywords described in table 3 to define which other classes can use the members of the current class.

Visual C#
Available to
Public
ALL members in all classes and PROJECTS.
Internal
» All members in the current PROJECT.
» in Visual Basic: Friend
Protected
» All members in the current CLASS and in classes DERIVED from this member's class.
» Can be used only in member definitions, not for classes or modules definitions.

Protected internal
» All members in the current PROJECT and all members in classes derived from this member's class.
» Can be used only in member definitions, not for classes or modules definitions.
Private
Members of the current class only.

Table 3 – Levels of Access for Classes


Inheritance
Visual C# uses the keywords described in Table 4 for creating base classes and deriving new classes from them.

Visual Basic
Visual C#
Use to
Inherits
derivedclass : baseclass
Base on class on another, inheriting members from the base class.

Overridable
virtual
Declares that a member of the base class can be overriden in a derived class.

Overrides
override
Declares that a member of a derived class overrides the member of the same name in the base class.

Shadows
new
Declares that a member of a derived class hides the member of the same name in the base class.
MustInherit
abstract
Declares that a class provides a template for derived classes. This type of class is called an abstract class, and it can’t be instantiated.

MustOverride
abstract
Declares that a member of a class provides a template for derived members. This type of member is called an abstract member, and it can’t be invoked.

MyBase
base
Call a base class member from within the derived class.

Me
this
Call a member of the current instance of a class.

Interface
interface
Create an interface that defines the members a class must provide.

Implements
classname : interface
Use an interface definition in a class.

Table 4 – Overview of the Inheritance Keywords


Overriding, Overloading and Shadowing Members
A derived class inherits the members of its base class. A member's signature includes its name, parameter list, parameter types, and return type. If the derived class defines a member with the same signature, the derived member overrides the base member. If a derived class defines a member with the same name but a different parameter list, parameter type, or return type than the base member, the derived member either overloads or shadows the base member. A member overloads another member if the base member is still available. A member shadows another member if the derived member replaces the base member.


In the Abstract
» An abstract class is a class that provides a template for derived classes, and it can’t be instantiated.
» An abstract method is a virtual method with no implementation.
» An abstract method is declared with the abstract modifier and is permitted only in a class that is also declared abstract.
» An abstract method must be overridden in every non-abstract derived class.


Interface-to-Face
Interfaces are similar to abstract classes in that they both provide a template that you can use to create new classes. The difference is that interfaces don’t provide any implementation of class members, whereas abstract classes can implement members that then become common to all the classes derived from them.

When you implement a particular interface in a class, instances of that class can be used for any argument or variable declared as that interface.


Detailed Example
Now, we represent an example that shows how to implement following class diagram in Visual C#.

Example.jpg
Figure 1 – Class Diagram of Detailed Example

// (1): (virtual)
//      In Visual C#, members that can be overriden must be declared
//          as virtual.
// (2): (Sphere: Circle)
//      Sphere class inherits all of methods and properties defined //          in Circle.
// (3): (override)
//      Sphere overrides the methods for Area, because
//          sphere use a different formula for this calculation.
//      A virtual method can be overridden in a derived class.
// (4): (new)
//      Sphere shadows the Center method because
//          sphere have an additional coordinate (z)
//          and you wouldn't want users to accidentally set
//          xy-coordinates without setting a z-coordinate.
// (5): (base)
//      Notice that Sphere uses the base keyword to call
//          the base class's Center method within the shadowed 
//          method.

// Interface for all shapes.
public interface IFigure
{
    float Top
    {
        get;
        set;
    }
    float Left
    {
        get;
        set;
    }

    float Area();
    float Perimeter();
}
// To use the inteface, implement it in a class
public abstract class Shape : IFigure
{
    // Constructor
 public Shape()
 {
 }

    public abstract float Top
    {
        get;
        set;
    }
    public abstract float Left
    {
        get;
        set;
    }

    public abstract float Area();
    public abstract float Perimeter();
}
public class Circle : Shape
{
    float fxCenter, fyCenter, fRadius;
    // Constructor
    public Circle()
    {
        // Intialize internal variables.
        fxCenter = 0;
        fyCenter = 0;
        fRadius = 0;
    }

    public override float Top
    {
        get
        {
            return fxCenter - fRadius;
        }
        set
        {
            fxCenter = value + fRadius;
        }
    }
    public override float Left
    {
        get
        {
            return fyCenter - fRadius;
        }
        set
        {
            fyCenter = value + fRadius;
        }
    }
    public float Radius
    {
        get
        {
            return fRadius;
        }
        set
        {
            fRadius = value;
        }
    }

    public override float Area()
    {
        return (float)(System.Math.PI * Math.Pow((double)fRadius, 
                       2));
    }

    public override float Perimeter()
    {
        return 2 * fRadius * (float)System.Math.PI;
    }

    public virtual void Center(float X, float Y)    // (1)
    {
        fxCenter = X;
        fyCenter = Y;
    }
}
public class Sphere : Circle                        // (2)
{
    float fCenter;

    // Constructor
    public Sphere()
    {
        // Initialize internal variable.
        fCenter = 0;
    }
    public float Front
    {
        get
        {
            return fCenter - base.Radius;
        }
        set
        {
            fCenter = value + base.Radius;
        }
    }

    public override float Area()                    // (3)
    {
        return (float)(4 * Math.PI * Math.Pow((double)base.Radius, 
                       2));
    }

    public void Center(float X, float Y, float Z)
    {
        base.Center(X, Y);                          // (5)
        fCenter = Z;
    }

    public new void Center(float X, float Y)        // (4)
    {
        this.Center(X, Y, 0);
    }

    public float Volume()
    {
        return (float)((4 / 3) * System.Math.PI * 
                       Math.Pow((double)base.Radius, 3));
    }
}
public partial class _Default : System.Web.UI.Page 
{
    // Displays the shape info on the Web form.
    // Because the Shape abstract class in this example implements 
    //     the IFigure interface,
    //     all derived classes from Shape can be used as the 
    //     arguments of the type IFigure.
    private void ShowShapeInfo(IFigure Shape)
    {
        // Since Shape argument is IFigure, we know it has these 
               members.
        Response.Write(String.Format("Shape Top : {0} <br>", 
                       Shape.Top));
        Response.Write(String.Format("Shape Left: {0} <br>", 
                       Shape.Left));
        Response.Write(String.Format("Shape Area: {0} <br>", 
                       Shape.Area()));
        Response.Write(String.Format("Shape Primeter: {0} <br>", 
                       Shape.Perimeter()));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a circle
        Circle MyCircle = new Circle();
        MyCircle.Radius = 2;
        MyCircle.Center(10, 2);
        // Create a sphere
        Sphere MySphere = new Sphere();
        MySphere.Radius = 10;
        MySphere.Center(10, 20, 25);
        // Show info about each shape.
        ShowShapeInfo(MyCircle);
        ShowShapeInfo(MySphere);
    }
}


Some of the Resources
Ø      Published by Microsoft Press “MCAD/MCSD Self-Placed Training Kit: Developing Web Applications with Microsoft Visual Basic.NET and Microsoft Visual C#.NET”
Ø      Published by Microsoft Press “C# Language Specification 1.2”
Ø      Microsoft Developer Network