#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Java Inheritance Help


    Hey guys, this is for an assignment. I am very new to java. Below, I will be posting a code and three questions. Can someone please help me with figuring out what the answers are supposed to be? I really appreciate it.


    public class A {

    private int number;
    protected String name;
    public double price;

    public A()
    {
    System.out.println( "A() called");
    }

    private void foo1()
    {
    System.out.println("A version of foo1() called");
    }

    protected int foo2()
    {
    System.out.println("A version of foo2() called");
    return number;
    }

    public String foo3()
    {
    System.out.println("A version of foo2() called");
    return "Hi";
    }
    }

    public class B extends A
    {
    private char service;

    public B()
    {
    super();
    System.out.println("B() called");
    }

    public void foo1()
    {
    System.out.println("B version of foo1() called");
    }

    protected int foo2()
    {
    int n = super.foo2();
    System.out.println("B version of foo2() called");
    return (n + 5);
    }

    public String foo3()
    {
    String temp = super.foo3();
    System.out.println("B version of foo3()");
    return ( temp + "foo3");
    }
    }

    public class C extends B
    {
    public C()
    {
    super();
    System.out.println("C() called");
    }

    public void foo1()
    {
    System.out.println("C version foo1() called");
    }
    }

    Questions:

    1) What fields ans methods are not inherited?
    2) What is the output of the following code sequence?
    B b1 = new B();

    3) What is the output of the following code sequence?
    B b2 = new B();
    b2.foo1();

    I would greatly appreciate any help!! Thank you guys so much!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by java.noob
    Questions:

    1) What fields ans methods are not inherited?
    2) What is the output of the following code sequence?
    B b1 = new B();

    3) What is the output of the following code sequence?
    B b2 = new B();
    b2.foo1();
    For Q1: To be able to answer this question you need to understand the basic concepts of inheritance. Read some more notes on inheritance and you should be able to figure this out.

    For Q2 and Q3: You might need to read more on object instantiation and method invocation to be able to answer Q2 and Q3.

    Let us know your findings and where you're stuck.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by noobie1000
    For Q1: To be able to answer this question you need to understand the basic concepts of inheritance. Read some more notes on inheritance and you should be able to figure this out.

    For Q2 and Q3: You might need to read more on object instantiation and method invocation to be able to answer Q2 and Q3.

    Let us know your findings and where you're stuck.
    Thanks for this information. After looking further into what you told me, I read more about inheritance and object instantiation. From what I know, the following answers would be:


    1) number and foo1() are not inherited from A
    2)
    A() called
    B() called
    3)
    A() called
    B() called
    B version of foo1() called

    Would you be able to verify for me if this is correct?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    1. I also don't see variables "name" and "price" being inherited from class A and variable "service" from class B.

    2. Answers to Q2 and Q3 are correct. Hope you understand the sequence in which constructors are being called. There is good note regarding this in the chapter on constructors from the book - "Java The Complete Reference" by Herbert Schildt.

IMN logo majestic logo threadwatch logo seochat tools logo