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
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.
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
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.
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 " );} }
Code is working fine, I just want to know the logic behind it. Why the output b3 ?
According to my logic, output is b13
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.
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?
I get "b 3" as output. (with a space). How does your execution remove the space?Output b3
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.