Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Ganish Patil

Ranch Hand
+ Follow
since Mar 24, 2015
Ganish likes ...
Eclipse IDE MySQL Database Tomcat Server Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ganish Patil

Welcome to CodeRanch!  

Rahul Zanwar wrote:the above code works properly

Are you sure? because void of display method of Parent is missing.

according to the law of polymorphism, the method calling should be:
when a parent class reference holds instance of child class, the child class method will be called.

Only if the parent class's method is overridden in the child class.

  • Here in your code, Child class doesn't extend Parent class

  • If this is working, then what is the rational behind this.?

    This will not work because, below code give compile time error Type mismatch: cannot convert from Child to Parent, because Child is not sub class of Parent. and although you add ; it gives compile time error which says The method display() from the type Parent is not visible, because it has private access modifier which means that method can only be accessed within Parent class but here you are trying to access it in Test class which is invalid. So this will not work.

    Rob Spoor wrote:For every set of overloaded methods, the compiler will use the most specific one

    Ohh I see,

    In your second example, there are three varargs methods, and so the most specific one is the Integer... one.

    Most specific method is having Integer then after that would be Number then Object. am I correct?

    Now, if your override in OneSubclass would also have used Integer... instead of Integer[], you' see the same behavior as the second example.

    Yes I got that now, It was too confusing but your explanation cleared it. Thank you so much  
    4 years ago
    I was studying varargs so found one of the links which baffles me varargs overriding old topic link
    Example 1:Copied code from the above link
    1. (String) => (String, Number[])
    2. (String, int) => (String, Number[])
    3. (String, Integer) => (String, Number[])
    5. (String, int, int) => (String, Number[])
    7. (String, int, int, int) => (String, Number[])

    Example 2:Same way another example I tried with
    Size of y is: 0
    (String) invokes  methodOne(String str, Integer... y)
    Size of y is: 1
    (String,int) invokes  methodOne(String str, Integer... y)
    Size of y is: 1
    (String,Integer) invokes  methodOne(String str, Integer... y)
    Size of y is: 3
    (String,int,int,int) invokes  methodOne(String str, Integer... y)
    Size of y is: 4
    (String,[]int) invokes  methodOne(String str, int[]y)

    My confusion is why in example 1 it calls doIt(String str, Number... data) but not doIt(String str, Integer... data)? where in example 2 it calls methodOne(String str, Integer... y) but not methodOne(String str, Number... y) as example 1 do?
    4 years ago
    Welcome to CodeRanch!  and Congratulation Nirbhay, It's really a good score  
    4 years ago

    Campbell Ritchie wrote:I hope that will help

    Yes helped a lot, Wiki is really a great source of information, I better peruse them. Thank you  
    4 years ago
    Congratulation Sahadete !! It's really a big achievement  
    4 years ago

    Kamila Bertran wrote:Hmm... Following the logic the output should be b hn x

    4 years ago
    What do you think what would be the output, If we remove this() on line no 11? here
    4 years ago

    Kamila Bertran wrote:House extends Building , Building's no argument constructor is going to be called as well?

    Yes, have look at this
    4 years ago

    Carey Brown wrote:

    I kept pondering on that how is this printing correct output  instead of fullyqualifiedclassname@hashcode then got detail information in source code, It was really awesome. I never knew we could print in this manner also.
    4 years ago
    You're welcome
    4 years ago
    It is executed like this
  • true |  now looks at right side is there higher precedence operator than | answer is yes i.e. + additive operator so goes to +
  • true |  ( false + "hello")  does conversion of boolean to Boolean and then that Boolean reference to String then does String concatination results "falsehello".
  • true | ("falsehello") now you can see | the bitwise and logical operators may be used to compare two operands of numeric type or two operands of type boolean. All other cases result in a compile-time error so | operator can't be applied to boolean and String.
  • so you get that error.
    Try doing this see what you get
    4 years ago
  • 10 + 2 + "SUN" + 4 + 5
  • Evaluation starts from left side.

  • Steps:
  • 10 + Now it looks at right side to check, is there any high precedence operator than +? answer is no so (All are additive operators so same precedence so left associative)
  • ( 10 + 2 ) result = 12

  • 12 + again it looks at right side to check, is there any high precedence operator than +? answer is no so
  • ( 12 + "SUN" )
    String conversion is performed because one operand is String i.e. "SUN" so 12 whose default primitive type is int is converted to Integer like new Integer(12); then this reference value is converted to type String by String conversion. Concatination of String using + operator uses StringBuilder's append method to append them and then converts to String using toString method of StringBuilder.
    Now result =  "12SUN"

  • ( "12SUN" + 4 )  same like above String conversion process result = "12SUN4"
  • ( "12SUN4" + 5 ) same like above String conversion process result = "12SUN45"
  • Final result = "12SUN45"

  • Edit: Then at last passes this String to parameterized constructor of StringBuilder i.e  public StringBuilder(String str)
    4 years ago

    Campbell Ritchie wrote:Maybe. But surely you are better off having an abstract Pet class and subclasses Dog, Cat, etc etc.

    Ohh I see, you mean declare common fields of Pets in Pet abstract class. Common behaviours of Pets comes in Pet abstract class like (Just assumed) sleeping(), drinkingWater() as defined methods of Pet abstract class whose implementation we know and methods (behaviour) which are type specific like makeNoise() where Dog barks and Cat meows so their implementation will be in respective classes i.e. type specific. Means when we know partial implementation then go for abstract class and when have no idea of implementation then go for interface, hope I'm correct?
    4 years ago