Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Tuesday, December 7, 2010
Monday, October 11, 2010
Monday, September 6, 2010
The Tao of Polymorphism
"In object-oriented programming (OOP), Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects." - Wikipedia
I dont know why everyone tend to use most difficult and hardest terms even for simple concepts. As for example above definition refers to Inheritance. We are not here to discuss Inheritance, but the advantages of Inheritance.
Labels:
Java,
Something More
Saturday, August 28, 2010
Inheritance implementation in Java
"Understanding a concept does not mean merely remembering the definition, it includes knowing the working or application of that concept." - Come closer to the screen, look at above sentence and try to understand (its the definition of understand) it. I know its recursive but I will clarify it with the example of Inheritance:
Assume your instructor has chosen you for questioning, and he demands: "Explain Inheritance?". You somehow manage to utter some words "reuse", "code", "hierarchical relationship" and get the clean cheat. You get seated and wipe-off the perspiration.
If this scenario is common then you are a victim. You merely know the definition or have faint idea but do you know at what stage is inheritance comes in to the picture? Apart from by-hearting the definition, it is equally important to know how a concept is implemented. Lets have a look @ how Inheritance is implemented in Java. Beginning with process of compilation,
Assume class "Child" is child of class "Parent". We define both classes as follows:
class Parent class Child extends Parent
{ {
int a; int b;
} }
Now going by definition, we know that sub class inherits all the members of parent class, hence "Child" class should have two members conceptually:
int a;
int b;
And to verify it we create an object and try to access member a, and it works.
child ob = new Child();
ob.a = 10;
Weighing both sides,
This gap is filled at compilation level and Java handles Inheritance at compile time. Thus,
Compilation includes defining structure of each class. The moment javac senses one class to be child of another, it adds all members of super class to structure of child. Thus the class layout after compilation would be...
class Parent class Child
{ {
int a; int a;
} int b;
}
Getting This? YES or NO??? Ok, Repeating once more,
Inheritance (sub-class has all the members of super-class) is detected at compile time. If any class is found to be "child" of super-class, all members of super-class is added to structure of child-class. Finally all layouts is written to .class file and thus saved.
Now that's called Understanding Inheritance. I know you will let me know what have you inherited from this post via comments.
Assume your instructor has chosen you for questioning, and he demands: "Explain Inheritance?". You somehow manage to utter some words "reuse", "code", "hierarchical relationship" and get the clean cheat. You get seated and wipe-off the perspiration.
If this scenario is common then you are a victim. You merely know the definition or have faint idea but do you know at what stage is inheritance comes in to the picture? Apart from by-hearting the definition, it is equally important to know how a concept is implemented. Lets have a look @ how Inheritance is implemented in Java. Beginning with process of compilation,
Assume class "Child" is child of class "Parent". We define both classes as follows:
class Parent class Child extends Parent
{ {
int a; int b;
} }
Now going by definition, we know that sub class inherits all the members of parent class, hence "Child" class should have two members conceptually:
int a;
int b;
And to verify it we create an object and try to access member a, and it works.
child ob = new Child();
ob.a = 10;
Weighing both sides,
- At code level there is no member 'int b' in child class
- At execution level Child class has the member 'int b'.
This gap is filled at compilation level and Java handles Inheritance at compile time. Thus,
Compilation includes defining structure of each class. The moment javac senses one class to be child of another, it adds all members of super class to structure of child. Thus the class layout after compilation would be...
class Parent class Child
{ {
int a; int a;
} int b;
}
Getting This? YES or NO??? Ok, Repeating once more,
Inheritance (sub-class has all the members of super-class) is detected at compile time. If any class is found to be "child" of super-class, all members of super-class is added to structure of child-class. Finally all layouts is written to .class file and thus saved.
Now that's called Understanding Inheritance. I know you will let me know what have you inherited from this post via comments.
Labels:
Java,
Something More
Thursday, August 19, 2010
Sign Extension in Java
Sign extension is a rule in Java by which widening conversion is carried out. While we are on the mood to know something, Let us see the whole process by an Example.
Labels:
Java,
sign extend,
sign extension,
Something More,
widening conversion
Thursday, July 22, 2010
Batch scripts in action - Java LAB
As I mentioned in earlier blog the batch scripts are very helpful. Below is another incident when Command-prompt-programming comes to rescue.
Java newbies know the usual procedure to compile and run Java program
- Open Notepad
- Write program(Optional) and save it as something.java
- Open CMD
- Run following command "set path=c:\program Files\Java\jdk1.6.0\bin"
- Navigate to your folder having something.java
- Run command "javac something.java"
- Run command "java something"
Thats a lot of procedure. Lets put Batch to help.
One more thing. I got to make folder "Lab_DATE" every lab and do respective practicals in them. Hence I created "start.bat" with following obvious commands.
mkdir Lab_
cd Lab_
cmd notepad
set path=C:\Program Files\Java\jdk1.6.0\bin
But it had a problem. The CMD window closed after executing last command. I needed it live after changing the path. Try it and you will know.
Help was the first thing that came to this computer engineers mind. Running "cmd /?" yields help. Thankfully there is /K switch that keeps terminal live after executing command. Finally tweaking start.bat to
mkdir Lab_
cd Lab_
cmd /C notepad
cmd /K set path=C:\Program Files\Java\jdk1.6.0\bin
did the trick... "Cut the crap" in my words.
I am still searching for some script that would write my Java program ;-)
Labels:
Batch Scripts,
Java,
Something More
Wednesday, July 21, 2010
Evolution Of Languages
I read the first chapter of Java-The complete Refernce.
C was dominant for many following years. But environment was changing. Lets see evolution of C++ the first OOP language.
Apart facts, this topic was most interesting.
Long ago, Before computers were invented . . . Just joking, Not so long ago lets us see evolution of first language Fortran
Scenario: Computers were limited to big companies.Average people still did not have the smell of computers. Small softwares were needed. Assembly language prevailed at that time and there was dire need of another programming language which was easy to debug.
Hence evolved Fortan.
Years passed. Now computers were becoming public. But yet no language was able to tackle new problems. Moreover Fortan was using GOTO - the most dark word in world of programming. So code had many GOTO's and jumps made larger programs intractable. It was called Spaghetti code. Lets see birth of revolution "C".
Scenario: There were several languages, Each for different purposes but none robust. Hence there was a need of ONE-FOR-ALL language that had definite structure.
Hence evolved C.
It was a total revolution. It was structured, It was robust, It was efficient and it was one-for-all. But one factor that C had and none language possessed was the feeling of "Programmers Language".
C was dominant for many following years. But environment was changing. Lets see evolution of C++ the first OOP language.
Scenario: Hardware was out in public. Programs were becoming gigantic and difficult to handle. Procedure orientation could not handle the growing complexity. At the same time new concept of OOP was introduced to handle prevailing complexity.
Finally something struck Stroustroup's mind and C++ evolved.
Everybody was happy with C++. Once again the same thing - Environment was changing.
Computers were common now. Internet was about to bloom. But now C++ was lagging the upcoming need, Portablity.
Scenario: Machines were out and so was variety among them. Variety of processors, variety of OS were bothering software vendors. Every new architecture or OS would require new COMPILER - difficult piece of software. At the same time internet was blooming. This opened a new dimension to software world. Web apps were failing to satisfy large diverse internet audience in multiple factors like security, ability to run on all OS and all architecture, Compactness and ability to run quickly.
Thus some brains @ Sun started to work and finally announced Java.
Java had it all. Portability to run on any PC, Structured, Ability to handle complexity, Robustness and blah blah blah....
Lets see what do current scenario demands. I am ready for it ;-)
Labels:
Academics,
Java,
Something More
Saturday, July 17, 2010
Environment Variables in Windows
Being a computer engineering student, I had to face Java language this year.
On the first lab some instructions were given to run commands prior compiling (Setting path.)
set path=c:\program files\java\jdk1.6.0\bin
Surprisingly (for me) just running javac command then worked. I knew there was no command called "javac" and the current directory in cmd did not have any "javac" file.
Curiosity bubbled and I asked the instructor of this, and he explained:
The command "set path" alters the default-set system path "C:\windows" and "C:\windows\system32".
It can be verified by My Computer > Properties > Advanced tab > Environment Variables Button.

Hence First lesson: System path (Under Environment Variables) is like virtual current directory. You can execute exe's lying in these folders, just like you execute them as if they are in your current directory.
Lemme explain,
Suppose there is an executable "temp.exe" in directory D:\temp. When you start command prompt, you are at c:\Documents and settings\
Now try to run "temp.exe".
Failed ? Eh ?
Reason is, there is no file called "temp.exe" in your current folder.
Navigate to D:\temp\ and then run "temp.exe". That goes well.
So to run any executable just by name (as we did by just typing "javac") we need to be in that directory.(My perception before lab)
But windows provides a mechanism through which you can setup another directories virtually as your current directories and execute files in them as if they were in your current directory.
Best example could be "notepad". Running this command opens up notepad editor. The executable for notepad is located @ C:\windows\system32 folder. Hence this directory being set as path by default we can run files in this folder just by names.
When I altered the path by "Set path" command, I could no longer run "notepad". It had the error.

Hence the confusion waded away and I carried on with Java.
Labels:
Java,
Programming,
Something More
Subscribe to:
Posts (Atom)

