• Post Reply Bookmark Topic Watch Topic
  • New Topic

How a method of another class is getting executed by new classname.methodname  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Was practicing method inheritance came across this new way of calling a method



Can any one explain the last line in the piece of code.

Thanks
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, Ddemos1 inherits bike.
Here it inherits the behavior defined in default method bike1().
Hence, you are able to call method in bike class using instance of Ddemos1.

To understand this in better way, you need to understand inheritance and polymorphism.

Nice tutorial from Oracle is here.

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Omkar,

Thanks for the reply

my question was

generally what i have learned is

Classname objectname = new Classname();

but found this new way of calling a inherited method or any method in same class. Wanted to know how is that different from this new way i.e. new classname().methodname()

Thanks
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • This creates an anonymous object of Ddemos1 and on that object method bike1() of bike which is having package private access modifer is invoked.
  • Ddemos1 is subclass of bike hence an object of Ddemos1 can access methods of bike which are declared with public, package-private(Accessible to sub class only when both super and sub class are in same package) and protected access modifiers.
  • If Ddemos1 had same method bike1() with same signature then method of Ddemos1 would have been invoked.


  • Edit:
  • Generally we create an object of a class and store it's reference in a reference variable of the same class or it's super class or interface(Super interface of interface) which this class implements.
  • In following code dmsObj1 is a reference variable of type Ddemos1 which stores reference of newly created object of Ddemos1.
  • Example:
  • Why we store reference of an object in a reference variable? because later we may need to use that object to invoke methods or access some fields of that object of Ddemos1 class more than once.

  • Real life example:
  • Suppose if you are travelling in the bus and the person seating beside you obviously be anonymous to you, you can have chat with him while travelling and afterwards you don't feel need or necessary to be in touch with him or contact him anymore once you reach your destination so you will not ask him for his phone number or address or email address etc so further you can have chat or work with him or contact him anymore.
  • So in this case you can talk to him only when your travelling, once you reach destination after words you can't because you have no reference ( contact information ) of him which we let you contact him later on.
  • Same way an anonymous object can be used only once if you don't store it's reference in a reference variable, let that reference variable be of type local variable, method parameter, class variable, instance variable, array component variable etc etc.

  •  
    Omkar Shetkar
    Ranch Hand
    Posts: 100
    2
    Eclipse IDE Java Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Wanted to know how is that different from this new way i.e. new classname().methodname()


    Eg.,



    To call method m1 of class A you must have instance of class A.

    Generally, to create instance of a class, we use new operator.

    will return reference to instance of class A.

    Now, to call m1() either



    or


    In latter example, we are calling m1() on anonymous object reference without needing to create a local variable.
    This is useful when you don't need that object reference further down the method.

    This way of method calling in OOP languages is called Method chaining.

     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks

    Ganesh,

    From what i understand in the above code we are creating an anonymous object when this line is executed. When the method scope ends the object is distroyed. how is this different from a normal object? is it that the normal object that we create can be used multiple times but the anonymous object can be used only once. Correct me if am wrong. And also can you clarify another doubt of mine which is like, does java have the concept of anonymous object's though i have heard about anonymous classes.

    Thanks
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This way of method calling in OOP languages is called Method chaining.


    Wow omkar, that's a new concept that i would be learning today. Thanks for introducing this concept to me.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    krishnadhar Mellacheruvu wrote:When the method scope ends the object is distroyed
    When bike1() method is executed then this anonymous object of Ddemos1 will be eligible for garbage collection but we don't know when precisely it will be GC.

    how is this different from a normal object? is it that the normal object that we create can be used multiple times but the anonymous object can be used only once. Correct me if am wrong.

    Please read my edit part of previous post, apology was late to submit cause of slow internet speed.

    can you clarify another doubt of mine which is like, does java have the concept of anonymous object's though i have heard about anonymous classes.
    I don't think so, it's general term. There is no such concept of anonymous object like anonymous class which is a concept of inner classes which further divides in local class and anonymous class. So anonymous object IMO means just an object whose reference is not stored for further use.
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks

    Ganesh,

    for the example.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One more thing, class name always starts with capital letter like you did with Ddemos1. Bike would look nice and unique rather bike, although you are just doing practice, you better name the methods more expressive else bike and bike1 may create confusion and yes you're most welcome  
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure I would call that method chaining, because you are only calling one method. I wasn't impressed by that Wikipedia article. Horstmann's GBC examples show method chaining a lot better.

    I have my own view of things like:-
    new Foo().go();
  • 1: I use that sort of code frequently.
  • 2: Whenever you have a reference to an object, you can do three things with it:-
  •        a: Ignore it. It will disappear into cyber‑limbo never to be seen again. The spare nextLine call shown here (line 2) is an example of an object reference being ignored. The object returned from that nextLine call is of no use, so it is simply ignored.
  •      b: Use it immediately, for example by myList.add(new Foo()) or new Foo().doSomething(). You may be unable to retrieve the object to use again, but in the case you showed, you only ever need to use that object once.
  •     c: Assign it to a variable. This is the commonest use of a reference. You can now use it again.
  • In the example you showed, you only need to use the Ddemos1 (what a dreadful name for a class) reference once, so there is no need to assign it to a variable.
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks

    Ritchie for pointing out the class name. I will also take into consideration the points you have mentioned
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!