on 09-18-2007 1:03 PM
Hi Gurus.
please clear my doubt
1. If we need to inhrit a class which is in diff package.
then do we need to import that package before inheriting.
2. If we import a package,what does it conceptually mean?
3. If I have imported a package , can i use methods of that class without inheritance
and if not
then by which object i can use its methods,(imported class obj or superclass obj)
Hi
1)
a. by using Inheritance your class will get all the properties of the inherited class.
b. you always dont need to inherit a class inorder to use it in other class, just by importing that in your class and by creating an instance for that you can use classes in different package
c. the example shown by franz is really good, ( extending class without importing it )
2) The imported package may contain several classes which will be repeatedly used, it may contain several calculations. like library functions. if you have imported it you can instantiate those class in your class and use it according to your requirements, remember to know about access restrictions.
3) you can use the members of a class without inheriting it just by importing it.
again you need to know about public, private, protected, and default access restrictions
A . any method which is declared <b>Public</b> can be accessed form any where inside or outside the package
B . any method declared <b>Private</b> can not be accessed out side the class. it cant even be accessed form a subclass.
C . any method with protected can be accessed from any where inside the package, even its subclassed or not.
D any method with default access can be accessed only inside the package.
Regards
Abhijith YS
null
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
1. Not necessarily, you could either use an import statement
import java.util.ArrayList
class Test extends ArrayList { ... }
or a fully qualified class name
class Test extends java.util.ArrayList { ... }
2.
hava a look at this
<a href="http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html">part of a java tutorial</a>
3. Could you please be a bit more precisely, I just to not quite understand what you want to know
Regards
franz
reward points if useful
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to create an object to use it's methods (if the Method is not static)
example:
import java.util.ArrayList;
class Test {
public void aMethod () {
ArrayList myList = new ArrayList();
myList.add (new Object());
}
// or with fully qualified names
public void anOtherMethod () {
java.util.HashTable table = new java.util.HashTable();
table.put ("ABC","DEF");
}
}
maybe have a look at a good java tutorial
regards franz
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.