Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

see this code

 
anilkumar vankayalapati
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
some methods()
}
class B extends class B
{
Some methods
}
class c extends class B
{
public static void main(String a[])
{

A a=new C();
// what is use of reffering like this if we cant access the methods in class C by a refferrin variable
}
}
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking, that is not true. You can access overridden methods(from A and B) in class C, and from those you can access methods in C, that are not overridden.

Using base class references to derived class objects can be very powerful, although it tends to be overused by people who think it is always the best way. It is a very useful feature in things like design patterns, not something you need to know now, but keep it in the back of your mind, when you are ready. Basically, it gives a way to keep code loosely coupled and easil to modify.

For now, just understand how it works and what pitfalls exist like trying to call a method in C that is not defined in a parent, or trying to use a derived class reference to a base class object. When you go deeper into OO concepts it will make sense.
[ March 29, 2006: Message edited by: Rusty Shackleford ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic