Is using 'extend' keyword the only way to create a SUB-CLASS?
//Let us say i have a superclass
class SuperClass {
//code here
}
//Now i create a sub-class of superclass
class SubClass extends SuperClass {
//code here
}
What about 'import' statement. Does it create a sub-class too? Kindly explain me in details. Thanks!
> What about 'import' statement. Does it create a sub-class too?
Huh? No.
"import" just allows you to 'shorthand' class names.
The "java.lang" package is imlicitly imported, otherwise you'd have to refer to the String class as java.lang.String - this has nothing to do with subclassing.
> Is using 'extend' keyword the only way to create a subclass?
No, there is a mechanism for creating anonymous subclasses.Thread foo = new Thread() {
run() {
// my code here
}
}
This creates an anonymous subclass of Thread, and creates an instance of this new subclass.
> Kindly explain me in details. Thanks!
"extends" declares a subclass or sub-interface. "implements" declares an implementation of an interface. All of these are subtyping.
"import" simply saves you some typing and keeps your code more readable by telling the compiler that when you write "Foo" you really mean "some.package.Foo."
You know, the Java programming guide book, the Java tutorials, lots of good third-party books...they all tell you how Java works. You should try reading them.