Results 1 to 2 of 2

Thread: Inheritance

  1. #1
    bigo005 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Question Inheritance

    class X
    {
    {
    fun();
    }
    X()
    {
    System.out.println("inside super class");
    fun();
    }
    void fun()
    {
    System.out.println("parent");
    }
    }
    class Y extends X
    {
    Y()
    {
    System.out.println("inside subclass");
    }
    void fun()
    {
    System.out.println("child");
    }
    }
    class Demo extends Y
    {
    public static void main(String args[])
    {
    Y d=new Y();
    d.fun();
    }
    }

    here the output is-
    child
    inside super class
    child
    inside subclass
    child
    My question is when Y object is created X class should be called but here the method inside block is called why? Without the block if i am writing the method it's giving error why

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Inheritance

    Please wrap your code in code tags, that will make it easier to read.

Similar Threads

  1. Inheritance
    By NewToJava_99 in forum New To Java
    Replies: 2
    Last Post: 05-20-2012, 09:03 PM
  2. Help with inheritance
    By waker3210 in forum Java Gaming
    Replies: 6
    Last Post: 05-19-2012, 09:27 PM
  3. JPA Inheritance
    By videanuadrian in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 07:44 AM
  4. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 03:53 PM
  5. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 09:18 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
  •