Results 1 to 2 of 2
  1. #1
    danportin is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default [Homework] Null Pointer

    I'm going to flag this as homework, even though I'm not posting the exact piece of homework I was working on; but I'm new to Java, so bear with me. Suppose I have a set of classes that look like this:

    Java Code:
    abstract class A {
    class B extends A {
       String b;
       String display (String ind) { return ind + b ; }
    class C extends A {
       A b;
       String c;
       String display (String ind) {
          String disp = ind + c + "\n";
          disp += b.display(ind + "   ") + "\n";
          return disp;
    Basically, I have a tree structure and I need to recursively construct a value of type String by calling the function display on each attribute in each class. What I'm not sure how to do is create a function display in class A that can be called on objects of type B and C, depending on which class I give it. When I try to do this, I get a compiler error telling me that functions in inheriting classes cannot override the function in class A. But I can't just create a function in class A, because then I couldn't access the functions in class B and C.

    [Edit]. This is the equivalent Haskell code , to illustrate what I want. Basically I don't understand how to "generically recurse" on objects (values) of class (type) A.

    Java Code:
    data A = B String | C String A A
    display :: String -> A -> String
    display ind (B b) = ind ++ b
    display ind (C c l r) = ind ++ c ++ "\n"
                              ++ (display (ind ++ "   ") l) ++ "\n"
                              ++ (display (ind ++ "   ") r) ++ "\n"
    Last edited by danportin; 10-25-2010 at 03:19 PM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    Java Code:
    public class Parent {
      public void foo() {
    class Child extends Parent {
      public void foo() {
    class TestInh {
      public static void main(String[] args) {
        Parent p = new Child();;
    Even though p is a Parent type variable, it points to a Child object, consequently, the output will be Child. If you overload methods, objects will know to call their version of the method, even if the variable that points to them is of their super class. Is that what you needed?
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  2. Null Pointer Execption
    By bl00dshooter in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 10:38 PM
  3. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts