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 7 of 7

Thread: HELP with Inheritance concept here

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

    Default HELP with Inheritance concept here

    Hello everyone, I am new to programming, but I do know the basic concepts of programming, specially Java

    I just want to know, how do I get the output as A) b3 every time I run the program, whats the concept. Please Help

    Java1.jpg


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: HELP with Inheritance concept here

    Please avoid photos and links. Instead if you have question about code, post the code itself here. Please show your code attempt and tell us how/why it's not working.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: HELP with Inheritance concept here

    public class FooBar{
     
        public static void main(String[] args) {
            Foo f = new Bar();
            f.addFive();
            System.out.println(f.a);
        }
    }
     
    public class Foo {
        public int a = 3;
        public void addFive() { a+=5; System.out.print("f ");}
     
    }
     
    public class Bar extends Foo{
        public int a = 8;
        public void addFive() { this.a += 5; System.out.print("b " );}
    }

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

    Default Re: HELP with Inheritance concept here

    Code is working fine, I just want to know the logic behind it. Why the output b3 ?
    According to my logic, output is b13

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: HELP with Inheritance concept here

    Please post your code and its output so we can see what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP with Inheritance concept here

    public class FooBar{

    public static void main(String[] args) {
    Foo f = new Bar();
    f.addFive();
    System.out.println(f.a);
    }
    }

    public class Foo {
    public int a = 3;
    public void addFive() { a+=5; System.out.print("f ");}

    }

    public class Bar extends Foo{
    public int a = 8;
    public void addFive() { this.a += 5; System.out.print("b " );}
    }


    Output b3

    Whats the logic?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: HELP with Inheritance concept here

    Output b3
    I get "b 3" as output. (with a space). How does your execution remove the space?
    What output do you expect?

    If you want to see what order the statements are executed in try debugging the logic. Add some println statements that print the values of variables as the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JADE---Sending an Concept including an concept
    By HansDampf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2014, 01:22 AM
  2. HELP with Inheritance concept here
    By dreamincode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 3rd, 2013, 12:03 PM
  3. In memory concept
    By VaniRathna in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 6th, 2012, 06:48 AM
  4. Help with a GUI Concept
    By UnKnown Conceptz in forum AWT / Java Swing
    Replies: 1
    Last Post: January 28th, 2012, 08:53 AM
  5. Need help with a interface concept
    By drakenlord in forum Object Oriented Programming
    Replies: 2
    Last Post: September 13th, 2011, 03:17 PM