• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super class as a reference type why?advantage?  RSS feed

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why aMethod3 IS NOT ACCESSIBLE IN SUBCLASS?

Why does most of the designers using type as a suprer class,what is the significance of using Parent class's reference?What is the advantage of it..?
Is it possible call variable in the suprer class which is abstract class using super keyword?
[ August 03, 2003: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write

the compiler doesn't "know" that t contains an instance of Subtest1. The type of t is test1, so only methods in test1 are available. You can supply that missing knowledge using a cast, if necessary -- i.e., this compiles:

I'm sorry, but I don't understand the rest of your questions.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use [code] tags to preserve indentation when you post code. I've added them myself in your previous post.
Why are there two Subtest classes?
I think you're asking why do people frequently recommend that you declare types using a superclass or interface type rather than the actual type - e.g. why say
List list = new ArrayList();
rather than
ArrayList list = new ArrayList();
The reason for this is that, if the supertype (namely the List interface in this case) has all the method declarations that you need, then using List rather than ArrayList makes your code more flexible later. You may discover that a LinkedList is actually more appropriate for your situation - you can change it with

List list = new LinkedList();
and since all subsequent code only uses the List interface, that subsequent code doesn't need to know or care about the fact that you chanced the ArrayList to LinkedList. This gives you more flexibility, which is often a good thing. However, if your code really needs to use a method that's unique to the ArrayList class, then you should declare the class as ArrayList - you don't want anyone to replace it with LinkedList later, because that will bread the code which uses an ArrayList-only method. So, try to declare using a supertype when possible, but if you need to use a more specific type, then use it. Does that make sense?
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can supply that missing knowledge using a cast, if necessary -- i.e., this compiles:

code:
--------------------------------------------------------------------------------
test1 t = new Subtest1();((Subtest1) t).aMethod3();
--------------------------------------------------------------------------------

I do not understand this code snippet .please explain it
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The first line creates a variable of type test1 pointing to a Subtest1 object (I'm sure you knew that already.)
The second line calls the method aMethod3 in class Subtest1 on the object t is referring to. To do this, it has to tell the compiler that t is not referring to a test1 object, but to a Subtest1 object (we have to do this because the class test1 doesn't have an aMethod3() method.) The value of the expression ((Subtest1) t) is a reference to a Subtest1 object. Then we just call aMethod3() on that reference as normal, using .aMethod3() .
OK?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!