codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  references and superclasses  mrnewbiw at 12:39 on Saturday, October 08, 2005
 

I have a college assignment which I am having trouble with:
Can you store a reference to a superclass object in a subclass reference variable? In each case either give an example or explain why not.

  Re: references and superclasses  crwood at 21:34 on Saturday, October 08, 2005
 

A subclass is everything that its superclass is - it extends the superclass. We usually extend something to change how it behaves or to add something to it. Otherwise, why bother?

Another way of asking the question is: Can a subclass reference be limited to only its superclass? Can an A be exactly a Super and no more?

No. If you say "A a = reference_to_a_Super" then what happens when the reference "a" is used to access something in A that Super does not have? Even "B b = reference_to_a_Super" does not compile.


public class Test
{
public static void main(String[] args)
{
Super s = new Super();
A a = new A();
B b = new B();
System.out.println("s = " + s + "\n" +
"a = " + a + "\n" +
"b = " + b);
if(a instanceof Super)
System.out.println("a instanceof Super");
if(s instanceof A)
System.out.println("s instanceof A");

// cast subclass to its superclass
Super s1 = (Super)a;
System.out.println("s1 = " + s1);
// s1 cannot access anything in A except what A inherited from Super
// s1 is the part of A that was inherited from class Super
// s1 is no longer an A
//System.out.println(s1.aStr); // won't compile
Super s2 = (Super)b;
System.out.println("s2 = " + s2);

// create a Super from an A
Super s3 = new A();
System.out.println("s3 = " + s3);

// do not compile - incompatible types
//A a1 = new Super();
//B b1 = new Super();

// cast superclass to a subclass
// compile but throw ClassCastExceptions at runtime
//A a2 = (A)s;
//B b2 = (B)s;
}
}

class Super
{
String superStr = "I am Super";

public String toString() { return superStr; }
}

class A extends Super
{
String aStr = "I am A";

public String toString() { return aStr; }
}

class B extends Super
{
// nothing extra so B and Super are the same...
}


  Re: references and superclasses  Romel Evans at 06:54 on Sunday, October 09, 2005
 

In .NET, a superclass is referenced by the "base" reference. So if you want to execute a method called xyz in the base class, you just say base.xyz();.

Hope this helps.

Romel Evans








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Date script issues
•  perl script help needed
•  onChange issue
•  perl remote execution
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007