Why A isn't printed ?why isn't in outpout | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why A isn't printed ?why isn't in outpout

29th Dec 2017, 2:14 AM
Hamed
Hamed - avatar
6 Answers
+ 9
because aliens? it would help if we could see the code
29th Dec 2017, 2:29 AM
jay
jay - avatar
+ 2
Hi Jay there s a code i don't understand .I've published it.It's the only code i got published
29th Dec 2017, 1:52 PM
Hamed
Hamed - avatar
+ 2
Hi Hamed, Your question looks quite similar to one I had to ask myself while I studied the material on Polymorphism (Overriding & Overloading). Kindly take a look at the code below and see if this is where you need clarity. class A {     public void do() {         System.out.println("A");     }         public void do(String str) {         System.out.println(str);     } } class B {     public static void main(String[] args) {         A object = new A();         object.do("B");     } } I took a closer look at the code in order to see what I was missing -- why the output did not have "A" printed to it like I (erroneously) expected it to. Here's what I found: The first version of the do() method specified in class A of this code WILL ONLY print the String "A" to the output on one condition -- that is, if the do() method is called anywhere within the main method using the "object" instance created from class A as follows: object.do(); However, according to the given code, that has not been done. Instead, just the second version of the do() method which takes a String argument/parameter has been called as shown below: object.do ("B");
6th Jan 2018, 12:21 PM
Shakara Kiing
Shakara Kiing - avatar
+ 2
Another point to note (this was WHAT actually got me confused at first) is that the following code which creates the "object" instance of the class A... A object = new A(); ...WILL NOT print the String value "A" to the output. This would have happened if the first version of the do() method was specified AS A CONSTRUCTOR. Even then, the method name would have to be different from do() since it would need to have the same name as its class. Kindly look through the modified code below: class A { A() {         System.out.println("A");     }     public void do(String str) {         System.out.println(str);     } } class B {     public static void main(String[] args) {         A object = new A();         object.do("B");     } } Now, the output of this code will be: A B This is because the previous do() method has been re-specified as a constructor with the "in-built" functionality of printing the String value "A" to the output each time a new instance of class A is created. Therefore, this piece of code... A object = new object(); ...is precisely what becomes responsible for printing "A" to the output, while another piece of code... object.do("B"); ...prints the String value "B" to the output right after "A". I hope this helped.
6th Jan 2018, 12:21 PM
Shakara Kiing
Shakara Kiing - avatar