Two Laptop Bag
The moose likes Beginning Java and the fly likes see this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "see this code" Watch "see this code" New topic

see this code

anilkumar vankayalapati

Joined: Mar 22, 2006
Posts: 7
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

Joined: Jan 03, 2006
Posts: 490
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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
I agree. Here's the link:
subject: see this code
It's not a secret anymore!