88Pro Thinking

cat /senthoor/mind | grep thought > blog

Wednesday, October 27, 2004

Virtual Function

There was an interview question asking “What’s Java's answer for Virtual Function?”. In order to answer that you got to know what is a Virtual Function. Check out the C++ Below code.

C++ Code


class Base
{
public:
void show()
{ cout << "\nBase";}
};

class Derv1 : public Base
{
public:
void show()
{ cout << "\nDerv1";}
};

class Derv2 : public Base
{
public:
void show()
{ cout << "\nDerv2";}
};

void main()
{
Derv1 dv1;
Derv2 dv2;
Base* ptr;

ptr = &dv1;
ptr->show();

ptr = &dv2;
prt->show();
}



Above C++ case has a base class named Base and two derived classes Derv1 and Derv2 both overriding the show() method. What will be the output if you execute this code? First ptr is being assigned with Derv1 refference and then later with Derv2 refference. After each assignment the show() method is executed. However the output will be,

Base
Base

Why is that? Becuase the C++ compiler ignores the contents (Object Type) of the pointer ptr and chooses the member function that matches the type of the pointer (Refference Type). The rule in C++ is that the compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer.

So if you want to achieve the polymopic effect(for your program to print Derv1 and Derv2) what you should do? You have to declare the draw method in the base class(Base) virtual.

public:
virtual void show()
{ cout << "\nBase";}
};

Java Code


class Base{


public void draw(){
System.out.println("Drawing Base");
}
}
 

class A extends Base{
public void draw(){
System.out.println("Drawing A");
}

}
 
class B extends Base{
public void draw(){

System.out.println("Drawing B");
}
}
 
public class JavaVirt{
public static void main (String args[]){

Base baseA = new A();
Base baseB = new B();
baseA.draw();
baseB.draw();
}
}



Whats the results?

Drawing A
Drawing B

Why? Because in Java all the methods are virtual by default. This link summarizes the difference between Java and C++ when it comes to virtual methods, elegantly.

"When you design a class in C++, you have to decide which functions could possibly be overriden by derived classes, and declare these functions as virtual.

In Java, it's the other way around.

KEY POINT Member functions are "virtual" by default, and it's only if you explicitly want them not to be overriden that you include a special keyword (final)." Extracted from Java Learning Centre.


If you think the story ends there, you are highly mistaken. C# inherits behavior of C++ when it comes to virtual functions. Here are some interesting reading on these two different schools of thought.


Anders Hejlsberg’s Thoughts on Virtual Being Default

Scott Meyers thoughts on Virtual and Non-Virtual functions

C# developer thoughts on Virtual By Default

Check the content under “Binary Compatibility” in this Interview with James Gosling

posted by 88Pro / Wednesday, October 27, 2004

Friday, October 15, 2004

Finally Its here

Google has released Desktop Search tool. Just tried it for few minutes and I am impressed with the results. I was having difficulty locating a document and with Google Desktop Search it was so damn easy to find it and to find every other document related to it. As usual already security and spyware concerns have popped up in the web. Microsoft is also planning to capture desktop search space and have already acquired Lookout. However I don’t see it’s happening at least for a year, but when it happens I am sure the search facility will be integrated into their OS. In the search business who knows more about user behavior has control and Google has competitive advantage in that space. However, first hurdle for Google is going to be to fight the security concerns and establish its credibility for its Desktop Search Tool.

posted by 88Pro / Friday, October 15, 2004

Wednesday, October 13, 2004

GMail! Who's idea was it?

I was reading through “The Inmates Are Running The Asylum” by Alan Cooper

Many email users get dozens or hundreds of email messages everyday. Most communications are sent either in response to some messages, or in expectation of a reply. These sequence of messages, called threads, bounce back and forth between two or more people. …

And yet not a single email program available today treats email messages as part of a sequence. They act as though threads either don’t exist, or are an insignificant attribute of only few emails.

It’s easy to understand that viewing threads instead of messages lets the user clearly see the connections and flow between individual messages and how they form coherent conversations. When examined from a task or feature point of view, all you can see is that you need to either send or reply.

It’s not a particularly difficult programming problem to handle email as a series of threaded conversations; it is just that it has never been done that way, and programmers are reluctant to innovate on the user’s behalf. [Extract from “The Inmates are Running The Asylum”, Chapter 4, Page 61]


The book was published in 1999 and in 2004 we had Google’s Gmail with feature Cooper has talked about in his book on threaded conversations which an email client should have if it is trying match user’s goals. I really thought this was Google’s original idea and it does not seems to be the case, and I can’t believe that from 1999 no one (even the big players) did anything to include this feature into their email client very well knowing it will disrupt the way email is used in day to day life. I am glad that Google took the first step and I am sure there will be lot following the trend in the future.

PS: Alan Cooper is also known as Father of Visual Basic.

posted by 88Pro / Wednesday, October 13, 2004

Sunday, October 10, 2004

Sri Lankan Airlines UL422 In-flight Service?

When monies are exchanged as payment in return for services provided, the customer inherently procures the right to receive those services at the standards of quality expected – this is the essence of customer service. Unfortunately my friends and I suffered what can only be described as atrocious customer service, during our travel on flight Sri Lankan Airlines UL422 (Colombo-Bangkok) on 25th September 2004.

We were absolutely disappointed with the in-flight service crew. It is shocking as to the number of times the “steward call” button had to be pressed for someone to attend to us. I recall pressing the button at least 15 times (during the space of an hour) - maybe they take you seriously only after the 14th press. With so many beautiful words in the English vocabulary, how hard it seemed for one steward to find the word “Please”. “Raise it!” were the orders handed down to me to lift my glass for a cup of coffee. The tone of voice used was disrespectful, and almost insultive. As a qualified professional, who has paid hard earned money for this ticket, I at least deserve to be treated with respect and courtesy. Ironically, unfriendly treatment from the “Worlds Friendliest Cabin Crew”.

Very few “smiles” had been loaded on the plane that day - just enough for business class I believe. Non were served to us. What irked us further was that this ignorant mistreatment, seemed especially designed for the economy class Sri Lankan passenger community, while our fairer skinned peers sitting along side us were the beneficiaries of better attention and care.

The disappointment went beyond the steel walls of our aircraft, and into the restrooms at the Bangkok airport. I found it hard to hide my face, as the countless other passengers discussed how poor the service was on the SriLankan flight – with discussions in English, and harsh words being used to describe their pain, the message may have traveled with other passengers, around the world.

As relatively frequent flyers on many other airlines, and as proud Sri Lankans, we were saddened that day to travel on our national carrier. On that flight SriLankan failed all of us. On that flight SriLankan failed Sri Lanka.

posted by 88Pro / Sunday, October 10, 2004

Thursday, October 07, 2004

Solaris Vs Linux

This is getting pretty interesting and exciting to see the events unfold every day. Everyone one knows what happened to SCO when it tried to mess with Linux so Sun doesn’t want to go on the same path. Instead of waging a WAR against Linux they are going after Redhat the largest Linux distributor. In the process they are making sure they set the records straight, Redhat != Linux. Here is a quote from McNealy

"We love Linux," said McNealy at last week's meeting of the Massachusetts Telecommunications Council in Newton. "We just don't love Red Hat." [extracted from linuxinsider.com].

Here is a sneek preview of missiles being fired across.

Round 1
Analysts on OpenSolaris by Sun kernel developer Eric Schrock

A rebuttal of a single Sun misinformed developer by Linux Kernel developer Kroah-Hartman

Round 2
Rebutting a rebuttal by Sun kernel developer Eric Schrock

A continued discussion with a Sun developer, round 2 by Linux Kernel developer Kroah-Hartman

posted by 88Pro / Thursday, October 07, 2004

This page is powered by Blogger. Isn't yours?

doteasy.com - free web hosting. Free hosting with no banners.