methods

What is the difference between accessing a class method by means of importing vs. extending?
[106 byte] By [palmmjsg23] at [2007-9-26 4:32:33]
# 1
Please don't cross post.
jschell at 2007-6-29 17:47:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Never mind jschell :)

Importing is a means of increasing the namespace of your current class to include classes without having to specify the fully qualified package name. It is purely a help to you, the developer, in that it means you can have less to type.

import java.io.File;

means you can refer to File objects thus..

File f = new File() instead of java.io.File f = new java.io.File();

Note, for classes with the same classname but different fully qualified package names you can have ambiguities which the compiler will throw out as errors, for example

import java.sql.*;

import java.util.Date;

imports are legal however....

Date d = new Date() will cause an ambiguity due to the presence of the java.sql.Date class. So importing can cause its own problems sometimes. Never fear, you can use the fully qualified package name to remove the problem thus.

java.util.Date jud = new java.util.Date();

java.sql.Date jsd = new java.sql.Date();

Extending a class and calling methods in objects of that new class is an entirely different topic.

If you extend a class, the new class inherits all methods and properties of the extended class plus you can add properties and methods to the new class in order to "extend" it. Some of those new properties and methods can override or replace the properties and methods of the old class.

Class A has method foo(); and method baa();

Class B extends A and overrides baa();

Calling foo in an object of class B results in the original method inherited from Class A (there is no object of class A here, so there is no 'call chain' to worry about at all).

Calling baa() in the same object causes the new method baa() being called rather than the inherited one.

Please try to remember that cross topic posting is not always welcome and that some people react in an unsuitably intolerant fashion to it.

andyba at 2007-6-29 17:47:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...