• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we extend multiple classes ?

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ChildClass extends ParentClass {}

this class extends ParentClass. Can ChildClass extend multiple classes ?

public class ChildClass extends ParentClass_1 and ParentClass_2{}
Is this possible ? If not then to use the methods of both classes, one class can be extended and other class can be imported in the ChildClass ?

thanks
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:public class ChildClass extends ParentClass {}

this class extends ParentClass. Can ChildClass extend multiple classes ?

public class ChildClass extends ParentClass_1 and ParentClass_2{}
Is this possible ? If not then to use the methods of both classes, one class can be extended and other class can be imported in the ChildClass ?

thanks


No. You can't.

If you want your class to use methods of other classes, as you said, import them and use their methods. Do extend when you want to override a method. If you want a class (A) to override methods of two classes (B & C) then you can go for multi-level inheritance.

Example:

public class B extends C
public class A extends B

Now methods of both B & C will be available in A for override.
 
Campbell Ritchie
Marshal
Posts: 52602
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "import?" You can't "use" methods of other classes, unless they are static.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do you mean by "import?" You can't "use" methods of other classes, unless they are static.


If it's question for me then I meant import the package containing class and then use it's method.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import doesn't serve any purpose but readability. If you dont want to write fully qualified name of a class then you can import it. In java 1.5 static import has been addded where after doing static import you can directy call static method in the class.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page import="ca.rogers.evolution.presentation.content.model.CommonImage"%>

If I import a page as coded above in jsp, then I can only use static methods of the page.
instead of that, can I import a class or package in jsp as it is imported in java class ? If not, then how to use non-static methods of a java class in jsp ?
 
Campbell Ritchie
Marshal
Posts: 52602
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use any instance methods of that class. You can statically import static public members of a class and call them as if they were private static members of your class. You cannot use instance members of another class inside your class, except by inheritance.You can invoke the valueOf and format methods from inside your class, but the instance methods of String can only be invoked on a String object. That is what you can do in Java.

No idea about JSP: I shall move you to the JSP forum where that sort of question is usually discussed.

 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:public class ChildClass extends ParentClass {}
this class extends ParentClass. Can ChildClass extend multiple classes ?
public class ChildClass extends ParentClass_1 and ParentClass_2{}
Is this possible ? If not then to use the methods of both classes, one class can be extended and other class can be imported in the ChildClass ?
thanks
Thanks to God, James Gosling, whoever it was that did not let that happen.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nirjari,

I will take one example to explain you....

now supppose in class C i want use class A method without import


now same scenario with inport



same is valid for jsp as well
 
Campbell Ritchie
Marshal
Posts: 52602
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivendra tripathi wrote: new a.B.printFullName();
new B.printFullName();
Surely it's

new B("Campbell Ritchie").printFullName();

assuming there is an appropriate constructor?
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that it matters, just wanted to add... I think he just missed the "()"

One doubt that came to me while going through this thread... an import in jsp is a static import?

Regards
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No; it's an import. Just look at the generated code.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(And as a CLOS user I really miss multiple inheritance. Java's lack of mixins is a real PITA.)
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can't use any instance methods of that class.

You cannot use instance members of another class inside your class, except by inheritance.


Why can't? I think I am missing something.
Why can't I create an instance and use it's instance method in my class?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can, but that's not what it sounded like you were asking.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!