Home arrow static arrow Java Programming [Archive] - extend vs import
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - extend vs import
This topic has 10 replies on 1 page.

Posts:114
Registered: 7/5/04
extend vs import  
Jul 31, 2004 2:03 AM



 
Hi

Im getting confused about the idea of importing packages into my class and extend class. What I actually doing when I extend a class?

Thanks
D
 

Posts:11,200
Registered: 7/22/99
Re: extend vs import  
Jul 31, 2004 2:40 AM (reply 1 of 10)



 
You are creating a new subtype, kind of like "dog" and "cat" are both subtypes of "pet." Creating sub types or extending classes is also called inheriting. It's an important feature in object oriented languages that are based on the idea of classes.
http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
http://java.sun.com/docs/books/tutorial/java/javaOO/subclasses.html
 

Posts:114
Registered: 7/5/04
Re: extend vs import  
Jul 31, 2004 5:08 AM (reply 2 of 10)



 
Ok..

As I understand I get all the superclass behavior by extending my subclass to it. So in my program I want to create a custom type of array that inherit the behavior of LinkedList.

So I extend my custom DataItemArray with LinkedList. Now from the instance I can call to all the LinkedList methods. So in order to declare the change between my own subclass behavior to the superclass one I have to override the superclass method?
public class DataItemArray extends LinkedList{   public DataItemArray() {  }  public String toString(String s){    //How can I change the contents of toString() in order to return a string that contains all my array?  }} 


Thanks
D
 

Posts:47
Registered: 4/1/03
Re: extend vs import  
Jul 31, 2004 6:37 AM (reply 3 of 10)



 
David_Green

"How can I change the contents of toString() in order to return a string that contains all my array" ??

i don't know what you want ? give me more description
 

Posts:11,200
Registered: 7/22/99
Re: extend vs import  
Jul 31, 2004 6:52 AM (reply 4 of 10)



 
As I understand I get all the superclass behavior by
extending my subclass to it. So in my program I want
to create a custom type of array that inherit the
behavior of LinkedList.
Inheritance is often overused. Why can't you just use the existing LinkedList class, what features do you need to add to it?

So I extend my custom DataItemArray with LinkedList.
Now from the instance I can call to all the LinkedList
methods. So in order to declare the change between my
own subclass behavior to the superclass one I have to
override the superclass method?
Technically no; it's enough to declare that your class extends another and that's it. Here's a completely legal declaration of a DataItemArray class:
public class DataItemArray extends java.util.LinkedList { }
That's however not a very good idea because it's not adding anything new to LinkedList; it would be easier just to use the existing class. It would make sense if you added a substantial feature like a cache of most frequently used items to make the get(index) operation faster.
 

Posts:37,103
Registered: 3/30/99
Re: extend vs import  
Jul 31, 2004 9:12 AM (reply 5 of 10)



 
As I understand I get all the superclass behavior by
extending my subclass to it.

You don't "extend your subclass to the superclass." Rather your subclass extends the superclass. If you extend LinkedList (which is probably not a good design choice), then your class IS A LinkedList, and it can do everything a LL can do. If you have an instance of your class, then you can call any of LL's public methods on that object. Your class also has access to LL's protected methods.

So in my program I want
to create a custom type of array that inherit the
behavior of LinkedList.

Okay, so you want a class that does everything a LL does. I wouldn't call it an array though, as "array" has a very specific meaning in Java.

So I extend my custom DataItemArray with LinkedList.

No. Your class extneds LL. You're not extending your class. Your wording here makes it sound like you create a class, and then add functionality to it by gluing a LL on. That's not how inheritance works. You would inherit from LL if you wanted to create a special kind of LL that did something differently the LL does it, or that adds some custom functionality to LL. When you inherit from a superclass, your class IS that superclass at heart.

Now from the instance I can call to all the LinkedList
methods.

Not sure what you mean here. Like I said, a user of your class can call all of LL's public methods, since an instance of your class (an object) IS an instance of LL as well. Additionally, the code that you write inside your class can call or override LL's protected methods.

Do you understand what an "instance" is?

So in order to declare the change between my
own subclass behavior to the superclass one I have to
override the superclass method?

If by "declare the change" you mean you want a particular method to behave differently in your class than in the superclass, then, yes, you do that by overriding one or more of the superclass' methods.

public class DataItemArray extends LinkedList{ public DataItemArray() {}public String toString(String s){//How can I change the contents of toString() in orderto return a string that contains all my array?}}

You don't change the contents of toString, you change the behavior. As to how, well, you put the code in there that builds a String the way you want it to. What to you want your string to look like? I think the behavior of LL is that if you add "a", "b", and "c" to the list, then what toString returns will be "(a, b, c)". You just need to write code that returns whatever kind of String meets your requirements. What part are you having trouble with?
 

Posts:37,103
Registered: 3/30/99
Re: extend vs import  
Jul 31, 2004 9:16 AM (reply 6 of 10)



 
Oh, and importing has nothing to do with functionality or behavior. It has precisely zero effect at runtime. All it does is save you some typing. When you say
import java.util.LinkedList;
that just tells the compiler that when you say "LinkedList" in your code, you mean "java.util.LinkedList".

When you say
import java.util.*;
that just tells the compiler that when you use a class or interface name that's not in the same package as the current class (the one you're writing) and is also not one of the fully-qualified classnames specified for import (e.g. java.util.LinkedList) then it should look at all the classes in java.util to see if one matches the class or interface name you used.

It doesn't add any functionality to your class. It just lets you use "LinkedList" instead of "java.util.LinkedList".
 

Posts:114
Registered: 7/5/04
Re: extend vs import  
Jul 31, 2004 12:12 PM (reply 7 of 10)



 
Hi

The only different I want in my LL extended class is toString() method. I want it to return a string describing the contents whose fits my needs. I did it all by using the keyword super. i add all elements to its superclass.

public class DataItemGroup extends LinkedList{  int tag=0;   public DataItemGroup(int tag) {    this.tag = tag;  }  public String toString(){    String s="";    s= s+ "\nGroup " + this.tag + " Members Are :\n" ;    for(Iterator it=super.iterator();it.hasNext();){      DataItemElement di = (DataItemElement)it.next();      s=s+di.toString()+"\n";    }    return s;  }  public int getTag(){    return this.tag;  }}


is that correct using?

Now my problem is like that, the constructor receive instances and I put them in the superclass,now how can I return it to the caller?

public class DataItemArray extends LinkedList{   /** The arr is used for cloned string storage. */  LinkedList arr = new LinkedList();   //copy the reciverd list to its superclass array  public DataItemArray(LinkedList diArray) {    for(int i=0;i<diArray.size();i++){      super.add(diArray.get(i));    }  }   public LinkedList getArray(){    return super.?????  }


Thanks
D
 

Posts:37,103
Registered: 3/30/99
Re: extend vs import  
Jul 31, 2004 1:13 PM (reply 8 of 10)



 
is that correct using?

I don't know what you mean by this question. If it does what you need it to do, then it is one correct solution. Are you asking if it's "the right way" or "the best way"? There rarely is a "right" or "best" way to do something. At minimum, it has to function according to the requirements. Beyond that, you have considerations of performance, maintainability, meeting the instructor's requirements as to which constructs to use or not to use.

One thing I would change is that I'd use a StringBuffer to build up the string from the elements, and then when you're done, call sbuf.toString() to return the String. This is more efficient than repeatedly appending to a String, alhtough unless your list is large, it's unlikely to make a significant difference.

Also, you don't have to cast before calling toString.
Object obj = iter.next();String str = obj.toString(); 
The correct toString method will be called without the cast.

And in fact, you don't even have to call toString
str = str + obj; 
will end up calling toString for you. That's what the + operator does for String. It's one of the convenient little quirks of the language.

That's just general info though. If you go with the StringBuffer, you can just do
sbuf.append(it.next()).append(newline); 
What's newline? Well, rather than using "\n" you should use
System.getProperty("line.separator")
to get your end of line character(s).


Now my problem is like that, the constructor receive
instances and I put them in the superclass,now how can
I return it to the caller?

Huh?

public class DataItemArray extends LinkedList{ /** The arr is used for cloned string storage. */LinkedList arr = new LinkedList(); //copy the reciverd list to its superclass arraypublic DataItemArray(LinkedList diArray) {for(int i=0;i<diArray.size();i++){super.add(diArray.get(i));}}</div>

The above will work, if you're trying to construct one of your objects by adding all the elements of a LinkedList to it. I think LinkedList has a constructor that takes another List or Collection as an arg, so you can just do this:
public DataItemArray(Collection col) {    super(col);} 
which will have the same effect.

By the way, never iterate over a LinkedList using a counter and list.get(i). That's O(n^2). So a list with 3 times as many items will take 9 times as long. Always use an Iterator for iterating over a Collection.

Also, you don't need to call super.add. If you're going to call an add method, just call add(). Instance methods are polymorphic, so regardless of which add() you think you're calling, it will be this.add(). In your case, this.add() is inherited from super.

public LinkedList getArray(){return super.?????}

Why do you want a method that returns an ArrayList? Your class IS a LinkedList. If you want to return the original list from which your class was constructed (why, I can't imagine) then you probably want composition, rather than inheritance. Don't extend LL. Just implement List and take a List as an arg at construction time.

That's one solution, but it really depends on what you're trying to do. If you really want something that is a LL except that it has a different toString, then keep extending as you do, but get rid of that getArray method, as it serves no purpose.
 

Posts:114
Registered: 7/5/04
Re: extend vs import  
Aug 1, 2004 5:29 AM (reply 9 of 10)



 

public class DataItemArray extends LinkedList{ /** The arr is used for cloned string storage. */LinkedList arr = new LinkedList(); //copy the reciverd list to its superclass arraypublic DataItemArray(LinkedList diArray) {for(int i=0;i<diArray.size();i++){super.add(diArray.get(i));</div>

By the way, never iterate over a LinkedList using a
counter and list.get(i). That's O(n^2). So a list with
3 times as many items will take 9 times as long.
Always use an Iterator for iterating over a
Collection.


Hi

Thanks for your tips!

ok...ill use Iterator to get elements but,is the same rule apply to add() with counter?

Thanks
D
 

Posts:37,103
Registered: 3/30/99
Re: extend vs import  
Aug 1, 2004 10:23 AM (reply 10 of 10)



 
ok...ill use Iterator to get elements but,is the same
rule apply to add() with counter?

You mean add(int index, Object element)? I've never used that method. Usually when you add to a list, you just add at the end, with add(Object element).

If you're going to insert at random indices, then LinkedList is probably what you want. It will take longer to find the index than with ArrayList, but the insert will, on average, but quicker, since with ArrayList you have to move every element >= to that index, whereas with LL, you just have to update a couple of pointers.

Read the API docs and the collections tutorial so that you understand the difference between LinkedList and ArrayList and where each performs well.

http://java.sun.com/docs/books/tutorial/collections/

In any case, you can't use and iterator for adding to a list. Only for viewing and removing.
 
This topic has 10 replies on 1 page.