Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This,extend,and implement

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I am having a hard time understanding these three items.
The "This",reference, "extend", and "implement".
Can someone put it in a nice and easy way for me to understand what the heck it is? How do I use them and of course a little example would be nice. I hope I am not asking too much. I have tried the tutorial but its too complicated to understand. Thanks a bunch in advance.
 
These are pretty basic oop ideas, and I will try to explain them. "this" is a reference to the object that you are, for instance:

public class Example
{
private String str;

public Example( String test )
{
this.str = test;
}
}

"this" can also be passed in methods, which can be pretty handy if you need to pass the current object reference to a method of another object.

Extends and Implements are intended for subclassing. You can implement as many interfaces as you want, but only extend a single class.

/*
No Implementation in these methods
*/
public interface ExampleInterface
{
public String getStr();
public void setStr( String str );
}

public class Example implements ExampleInterface
{
private String str;

public String getStr()
{
return( str );
}

public void setStr( String str )
{
this.str = str;
}
}

That will help you with Interfaces, now for Abstract classes:

public abstract class ExampleBase
{
private String str;

public abstract String getStr();
public void setStr( String str )
{
this.str = str;
}
}

public class Example extends ExampleBase
{
// only need to implement the getStr method
public String getStr()
{
return( str );
}
}


Hope that helps. If you need anymore help, be sure and ask.

-gw
 
hi,
In this example,

public class Example
{
private String str;

public Example( String test )
{
this.str = test;
}
}
instead of using this.str to assign test to str.
Would it also work if I just simply do this?
str=test;
 
To your last question "YES". You would only NEED to use the
Code:
this
Object reference, in this case, if the provided method argument as the same name as the class member. Exmaple:
Code:
public class Example
{
    private String str;

    public Example(String str)
    {
        this.str = str;
    }
}
This removes the ambiguity of what is what. Since the local scoped
Code:
str
has precedence over the the class member
Code:
str
.

To give you some definitions.

The keyword 'this' is an Object reference. It is a reference to the in scope Object. It can be used to pass a reference to the current in scope Object to a method or constructor that takes an Object or a class of the same type as the Object it is referencing.

Exmaples of showing when you would use it an how:
Code:
public class Cat extends Animal implements ActionListener
{
  private String aString  = null;
  private JButton aButton = new JButton("I can talk!");

  public Cat()
  {
    this("Meow!");
    // Using the 'this' keyword in the above manner
    // allows you to call another constructor of the 
    // same class, usually to deligate some work to.
    // This call HAS TO BE done first in a consructor
    // if you are going to do it.
    // Doing this allows you to have alot of constructors,
    // But they all in someway deligate to another
    // constructor where the real work is done.
  }

  public Cat(String aString)
  {
    super(aString);

    this.aString = aString;
    // You do this for the reasons I described above.
    // The reason why you would write code that needs
    // to do this, is that you have a name of a member
    // that is a good name, and self documenting as to
    // it's function.  So it is great idea to use it in
    // a constructor or method description also for the
    // same reason.  So you call the 'this' keyword so
    // the compiler knows that you want to assign the
    // local constructer scoped 'aString' to the class
    // member aString.

    aButton.addActionListener(this);
    // Using the keyword 'this' in the maner above allows
    // the programer to pass a reference of the current in
    // scope object (Cat) to another Object.  In the
    // above case pass a reference to the current Cat
    // Object to the aButton Object setting the Cat
    // Object as it's action listener.

    MyInnerClass innerClass = new MyInnerClass();

    innerClass.setMyString();
  } 

  public void actionPerformed(ActionEvent event)
  {
    System.out.println(aString);
  }

  private class Kitten
  {
      private String aString = null;

      public void setMyString()
      {
        this.aString = Cat.this.aString;
        // Okay this is to show you another useful use of
        // of the keyword 'this'.  If you are in an inner
        // class and you need to access a member varible
        // or method that has the same name or description
        // as a member varible or method of the inner class
        // then you HAVE TO state which 'this' Object
        // reference you want.  So you have to same the
        // name of the outter class first, that use the 
        // dot operator and then the this keyword, and 
        // and free here you use as normal.  You have to
        // do it like that because the current in scope 
        // object is the inner class.  Because the inner
        // class has a member of the same name as the
        // outter class you should use the 'this' keyword
        // for clairity, but it is not needed since it the
        // inner class 'aString' is in scope it will be
        // used.  So it could have been typed as so:
        //aString = Cat.this.aString;        
      }
  }
}
This was a short code example with a lot of comments to exaplain what is happening and why. I hope that helps explain the 'this' keyword.

The other two keywords 'extends' and 'implements' are used by Java to handle class inheritance. You can "extend" other classes, and you can "implement" interfaces.

An interface is an Object that defines public members and/or methods that an implementing class must "implement".

Extending a another class gives you all the public and protected members and members and methods of that class for free. You can use them in your, or people using your class can use the public ones.

Basicly if you extend a class your class is that class. So in the above example the Cat object is a Animal. Therefore you can do what every an Animal can do. Plus you can add your own things that a Cat can do that an Animal can not do. A Cat can talk, it can meow. So you implement an ActionListener interface so that you can be an ActionListener. You add the Cat to a button as an action listener, so that if the button is clicked, the Cat
will "Meow", or rather print meow to the system console.

The difference here between extending the class, and implementing an interface is that "if" there are any public or protected members in the base class Animal you get them for free, with out having to do anything but extend the class. But you can only extend on class and that's it, you can be any other classes. On the other hand when you implemented the interface you have to do some work to be come this Object. You have to implement all the methods and members it defines. But you can implement as many interface Objects as you want or need.

This provides a safer form of multiple Object inheritance.

I hope this answers you questions about the tree keywords, and provides a little example as to help. I if this was helpful in that, now you have more questions. I would like to recommond a great book for a new commer to Java, with or withour prior programming experience,0 Core Java 2 - Volume I - Fundamentals, published by Sun Press/Prentice Hall. This is one of the few programming books I recommend reading cover to cover, in chapter order. Don't skip arround. After reading this book if you do the exersises you should have a great grasp on the fundamentals of java, and will not only know the answer to many of your future and current questions, but know where to go, and how to get answers to questions fast.

Anyways this essay should end know before it turns into a book... good luck.

(Please forgive any typo's and statements that may be unintelligible, since it is 1am and I am about to fall asleap at the keyboard. If something seams unitelligible just ask and when I am more awake I shall try to clear it up... hehe)
Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top