Monday, March 5, 2007

Let's talk bout Java!

Okay… now that we know what objects and classes are, at least from a bird’s eye perspective (of a bird that is 100, 000 m up in the sky :)) let’s see how Java works with OOP from the same height.

Let’s revisit the three steps that are involved in the whole process of OOP.

1. You come up with a description on how your object is supposed to be or what are all the features that your object should have. (I’m sure you could remember the CAD for your dream house). In other words, you define the class.
2. Create the actual instance of this class. You construct the house. Technically, create an object.
3. Do whatever you want with the object. With a house you typically get expensive furniture to add to the grandeur, probably hang a beautiful painting in your drawing room, paste all your favorite clips in your bedroom and so on.

So far so good… so how do you do it with Java?

1. Create a class. A Java class is typically declared using the following syntax:
class MyDreamHome{



}

Pretty straight forward. You declare a class with the best keyword you could think of – “class”. You give in the name of your class after the “class” keyword with a space separating the class name and the key word. If you have any previous programming experience, you must know about blocks. A block is a set of statements clubbed together into… well… a ‘block’ of code. In Java blocks start with a ‘{‘and ends with a ‘}’. A class is a block and when you use the syntax above you say there is a block between ‘{‘ and ‘}’ AND it is a class (there are many varieties of blocks and class is one of them) AND it is called MyDreamHome. Hope I’m not confusing.

2. Second step is to instantiate the class. In other words, I actually create a new object that corresponds to my class definition. Yet again the creation of this new object is done using a spectacularly simple keyword ‘new’. If I need to create a new instance of MyDreamHome, I’d say

MyDreamHome myHome = new MyDreamHome( );

Let’s see what happens here. When I mention the key word new, it means a new instance. But a new instance of what? I should mention somewhere that I want an instance OF TYPE MyDreamHome. So I give in the class name for which I need to create the instance as the successor to the new key word and of course separated by a space. I could hear you saying “Fine. But what is the ( ) after the class name all about?!”. While creating an object, we are actually performing some action and when I perform an action, there is always a method associated with it and whenever a method is involved you always have parentheses ‘()’. Let’s reserve the definition of methods and members for now and assume that method is something that should be used for any actions performed in your program. The part of the expression before the ‘=’ sign should be familiar to you. We just give the object ‘a name’. Well… isn’t that typically what you do when you own a house? You consider things complete only when you hang out a board saying “John enclave” to mark that this is ‘your’ home. Don’t you? :) That is exactly what we do. We give the object a name (myHome) and we say it is of TYPE MyDreamHome.
3. We use the dot operator ‘.’ to get something from or do something on an object. For now forget about what this statement exactly means. But just have in mind that whatever you want to do something on an object or whatever you want from an object, it has to be done/got through a dot that follows the object name. for example myHome.xxx

If you are able to at least vaguely understand how object oriented programming works, it should be good for now.

Chill out at http://forwardszone.blogspot.com.

No comments: