Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: java inheritance

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java inheritance

    in java if a parent class and child class has method with the same name and if an object of child class is made and method is called for that object then why the parent's method is executed ?? for example

    public class parent
    {
    public void a(){
    System.out.println("i am parents method");
    }

    public class child {
    public void a()
    {
    System.out.println("i am child's method");
    }

    public static void main(string[] args)
    {
    child c1=new child();
    c1.a();
    }


    when child calls method a() the method of child class should be executed but why it does not happens ?? why the parent;s method a() is executed??

    plzz reply soon


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java inheritance

    There is no relationship between the class Parent and the class Child. Try again by having the Child class extend the Parent class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java inheritance

    oh i forget to write that now consider this code
    public class parent
    {
    public void a(){
    System.out.println("i am parents method");
    }

    public class child extends parent {
    public void a()
    {
    System.out.println("i am child's method");
    }

    public static void main(string[] args)
    {
    child c1=new child();
    c1.a();
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java inheritance

    You forgot to post the contents of the command prompt window that shows the program's output.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java inheritance

    <public class parent
    {
    public void a(){
    System.out.println("i am parents method");
    }
     
    public class child extends parent {
    public void a()
    {
    System.out.println("i am child's method");
    }
     
    public static void main(string[] args)
    {
    child c1=new child();
    c1.a();
    } >

    this code display the output "i am parent's method"

    while it should display "i am child's method"
    because child object has called the method

    and sorry if the pattern of my post is wrong. i am new here so don't really know the pattern

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java inheritance

    The code:
    System.out.println("i am parents method");
    }
    What you said is printed: "i am parent's method"

    Its clear that the posted code is not what is executed and printing the output. The code does not have the '
    that you say is shown in the print out.

    Make sure that the output posted matches the code that is posted.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java inheritance

    <public class myclass1
    {
    public static void method1()
    {
     System.out.println("i am parent method");
    }
    }
    public class MyClass extends myclass1
    {
    public static void method1()
    {
    System.out.println("i am child method");
    }
    public static void main(String args[])
    {
      MyClass c1=new MyClass();
      c1.method1();
    }
    }>

    i am parent method

    this is the output however output should be

    i am child method

    because object of child is created

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java inheritance

    Please copy the full text of the command prompt window from when the code is executed showing the command line and the printout from the program.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.


    Why is the code different every time you post this question?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Inheritance problem
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2013, 07:34 PM
  2. [SOLVED] Java inheritance
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 10:42 PM
  3. why java does not support multiple inheritance?
    By venkatsairam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2012, 05:25 PM
  4. Constructors and Inheritance in Java
    By diav in forum Object Oriented Programming
    Replies: 3
    Last Post: November 10th, 2011, 09:45 PM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM