• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java

 
sac abc
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to use this and super keyword
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use such vague titles for your questions: read this.

how to use this and super keyword
Practice, practice, practice.


The "this" keyword refers to the object you happen to be using at the time. It cannot be used in a static method.
  • You can use it at the beginning of an overloaded constructor. this(foo, 1); passes control from one constructor to another which has (foo, int) as its parameters.
  • It can refer to the whole object. A call like "MySerializingClass.serialize(this, myFile);" serializes the entire object into the file "myFile."
  • It can be used before the name of an instance field or instance method to distinguish them from local names. In a method with "bah" in its parameters or local variables, "this.bah" means the "bah" which is a field, not the "bah" which is a local variable.

  • The "super" keyword refers to that part of the object which is also in its superclass.
  • You have to use it as the first line in at least one constructor unless the superclass has a public no-argument constructor; "super(foo);" calls the constructor in the superclass which has "foo" as its parameters.
  • You can call "super.bah()" from anywhere inside any instance method if the "bah" method has been overridden.
  • If you can do this there has been some bad design, but if there is a field which is shadowed by a field in the subclass with the same name and you have access to it (!), you can call it with "super.baah" rather than "baah."
  • The last example is very bad design (no encapsulation, two variables with the same name), so never use it.

    If any more details are required, you will have to go through the books.
     
    Srinivas Kalvala
    Ranch Hand
    Posts: 257
    Firefox Browser Hibernate Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,

    Super can be used in following ways,

    one is to call the super() constructor.

    second is to call the super. method and another one is to
    super.attribute assignemnt.

    and explore about "this".... all the best
     
    Sanjaya Sugiarto
    Ranch Hand
    Posts: 229
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this: executing actual object. It's like 'me' in VisualBasic.
    super: you call the parents object.

    Used in inheritent cases (interfaces, abstracts, extends, etc).





    If you do new HelloWorld2() than the output would be: hi hello in HelloWorld1. If you do new HelloWorld2("hi ", "ok i got this") than the first output would be: hi hello in HelloWorld2.
    Notice I used this() and super() --> only for constructors.
    There are also this.<methodsname> and super.<methodsname> (with dot) --> for all others except constructors. The principle is the same though.
     
    Jay Suttiruttana
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "this" can identify the member of the class with relation to its variable/attribute.

    ex:

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic