Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: this keyword and inheritance

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default this keyword and inheritance

    I am starting to learn android app development and right now developing a small app after the hello world application using this tutorial: Starting Another Activity | Android Developers

    I building an object intent of a class named Intent.
    Java Code:
     Intent intent= new Intent (this, SecondClass.class)

    As you can see, the constructor requires two parameters: a context (object of Context class) and another parameter.

    Now, this code is written inside a class which extends the class Activity, and Activity is a subclass of class Context.

    In the tutorial it is written that this keyword is used in the code because Activity is a subclass of Context.

    To the best of my knowledge, the this keyword is used in place of the current object, which will here be an object of Activity. But they are using this in place of an object of Context.

    Now are they doing that because Activity extends Context, i.e. using an Activity object in place of a Context object/ using a sub class object in place of a super class object, i.e. implicitly casting a super class object to a subclass object?

    but I think such casting from super class to sub class is not safe, isn't it?






    Last edited by Nazneen Ali; 11-02-2013 at 01:10 PM.
    Don't forget to smile :-)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: this keyword and inheritance

    It's the other way around, i.e. if D extends B and some method requires a B, you can safely pass it a D because a D is a B (one of the fundamental rules in Object Oriented Programming). So, in your example it's up-casting, i.e. you cast a D to a B which is always safe.

    kind regards,

    Jos
    Nazneen Ali likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: this keyword and inheritance

    Got it! Thank you.
    Don't forget to smile :-)

Similar Threads

  1. this keyword
    By crazy in forum New To Java
    Replies: 6
    Last Post: 07-07-2013, 05:18 PM
  2. new keyword?
    By MW130 in forum New To Java
    Replies: 9
    Last Post: 05-08-2013, 03:26 PM
  3. The this keyword
    By Army in forum New To Java
    Replies: 15
    Last Post: 12-14-2012, 04:01 AM
  4. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •