Search This Blog

18 June, 2011

Abstract method and Abstract class part-1

Pointer to object and inheritance:-

In case of inheritance, a base class pointer is capable of storing address of an object created from the class through which the pointer has been declared or any of its derived class but through the pointer of the base class the members of the derived class cannot be accessed unless they are declared or defined in the base class.

This is one of most important topics.
follow some examples :-

Eg.1)
class aaa
{
public void sam()
{
System.out.println("Hello");
}
}
class bbb extends aaa
{
public void tom()
{
System.out.println("Boys");
}
}

class psp
{
public static void main(String mm[])
{
aaa p1;
p1.new aaa();
p1.sam();
p1.tom();
/*Incorrect because P1 knows nothing about tom*/ 

aaa p2;
p2.new bbb();
p2.sam();
p2.tom();

aaa p3;
p3=new bbb(); 
/* Correct, a base class pointer can store the address of a derived class object. */

p3.sam();
p3.tom();  
/*Incorrect because p3 knows nothing about tom since its declared from class aaa */

bbb p4;
p4=new aaa(); 
/*Incorrect because a derived  class pointer cannot store the address of a base class object. */
}
}
-----------------------------------------------------------------

Eg.2)
class aaa
{
public void sam()
{
System.out.println("Hello");
}
}
class bbb
{
public void tom()
{
System.out.println("Boys");
}
}

class psp
{
public static void main(String mm[])
{
aaa p1;
p1.new bbb();
p1.sam();
p1.tom();
}
}
/* In above code through the base class pointer a method of the derived class gets invoked because the same method is in the base class.*/
-----------------------------------------------------------------

In C++ through the base pointer if you need to invoke the method of the class whose object is being created then the base class should be declared as virtual. In C++ if the method being called is not Virtual in the base class then in all cases the base class method gets invoked through the base class pointer.

If the base class method is virtual then the method of the class whose object is being created gets invoked through the base class pointer.

So it can be said that in c++ there are two lookup pattern for methods :-

1.For non virtual methods:- The method will be searched in the class through which the pointer has been declared.

2.For Virtual methods:- The method will be searched in the class whose objects address is in the pointer.
-----------------------------------------------------------------
* In Java the rule is slightly different. In java their is only one lookup pattern. In java the concept of virtual method doesn't exists.
The method will always be searched in the class whose objects address is in the pointer through which the method is being called. But remember for that the method should exist in the class through which the pointer has been declared.

In the above example since the pointer p1 is holding the address of an object of class bbb, the tom will be looked into bbb class and since it exists in bbb class it will be executed.
-----------------------------------------------------------------

No comments:

Post a Comment

Meetme@