cancel
Showing results for 
Search instead for 
Did you mean: 

inheritance

manubhutani
Active Contributor
0 Kudos

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)

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

manubhutani
Active Contributor
0 Kudos

Thanks

Have given u points

3. I mean to say that for eg if i import a package

then in my class can i use methods of this imported class with the object of my class

or i need to use its methods by declaring an object of this imported class in my class.

Former Member
0 Kudos

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