Results 1 to 5 of 5
  1. #1
    freetochoose is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Help - How the inheritance of java works?

    I have a question regarding the inheritance in Java. Suppose I have the following:

    public class Parent {
    public void printALetter() {System.out("Parent A");}
    }

    public class Child1 extends Parent {
    public void printALetter() {System.out("Child1 A")};
    }

    public class Child2 extends Parent {
    public void printALetter() {System.out("Child2 A")};
    }


    now I want to declare a variable of type Parent that can be assigned to either Child1 or Child2.

    Parent p;
    Child1 c1 = new Child1();
    Child2 c2 = new Child2();

    p = c1;
    p.printALetter() // does this print out Child1 A
    p = c2;
    p.printALetter() // does this print out Child2 A

    Somehow in my program both times it prints out Parent.
    which is not what I want.
    Basically I want to have a collection of either Child1 and Child2.
    and in my program, I will just need to get assign any one of them
    to a Parent reference, so it will call the method I want it too.
    AM I Making sense?

    Any help is appreciated.

    ftc

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    10

    Default

    You should be using System.out.println. This should work polymorphically as you want since all the classes override the method. Perhaps someone else can say why this isn't working.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,311
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by freetochoose View Post
    Parent p;
    Child1 c1 = new Child1();
    Child2 c2 = new Child2();

    p = c1;
    p.printALetter() // does this print out Child1 A
    p = c2;
    p.printALetter() // does this print out Child2 A

    Somehow in my program both times it prints out Parent.
    which is not what I want.
    You should put fresh batteries in your Java installation because what you described is incorrect behaviour; all methods (except when static of final) are virtual methods.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    6

    Default

    Java Code:
    class Parent {
      public void printALetter()
      {
        System.out.println("Parent A"); 
      }
    }
    
    class Child1 extends Parent
    {
      public void printALetter() 
      {
        System.out.println("Child1 A");
      }
    }
    
    class Child2 extends Parent 
    {
      public void printALetter() 
      {
        System.out.println("Child2 A");
      }
    }
    
    public class IT
    {
      public static void main(String[] args)
      {
        Parent p;
        Child1 c1 = new Child1();
        Child2 c2 = new Child2();
    
        p = c1;
        p.printALetter(); // does this print out Child1 A
        p = c2;
        p.printALetter(); // does this print out Child2 A
      }
    }
    Output:
    Java Code:
    Child1 A
    Child2 A
    Works on my Java-installation.

Similar Threads

  1. Help with java gui and inheritance
    By sssss in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2011, 05:38 AM
  2. Replies: 0
    Last Post: 10-05-2009, 11:25 PM
  3. Replies: 7
    Last Post: 03-31-2009, 01:29 PM
  4. What are the hot java frames works on demand
    By mallaravi in forum Web Frameworks
    Replies: 1
    Last Post: 10-28-2008, 02:34 PM

Posting Permissions

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