File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Achieving inheritance without extends Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Achieving inheritance without extends" Watch "Achieving inheritance without extends" New topic
Author

Achieving inheritance without extends

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

1. Whether it is possiblr to acieve the inhertiance without using inheritance concept. That is without using the keyword extends, it is possible to inherit. If so, how? with some example coding. and what are the pros and cons of using it?

2. In the inner class, whether it is possible to do like this:



3. What is anonymous class with one example pls?

Thanks in advance,
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
...would you like fries with that? :roll:

How far have you got on these questions by yourself. You need to ask more specific questions, no-one will do your homework for you!


SCJP 1.4, www.gsi3d.org.uk
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

This is the question asked in interview to my friend. He didn't know the answer for this and asked me.

I told it may be(I am not sure, that why I asked in this forum to clarify) using the anonymous class we can achieve(I think).
I read about anonymous class in the event handling. I don't know how to use this class. That's why I asked it? I am also try to find out the answer in different articles and sites. In meanwhile I posted this question, since this group will give some clear picture.(for me many times)

Anyway thanks for your reply.
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
hi mark!!!
last week i was going through the book thinking in java by bruce eckkel

http://www.mindview.net/Books/TIJ/

its one of the best books available on java!!!
it contains alot of information on anonymous classes....
read!!!
i am trying to understand the concept clearly!!! i haven"t got it in the first shot
!!!
hope the link will buseful for u ..
if i get some thing about it i will defintly post it to you!!!
bye have nice time!!
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi Mark,

I tried the below pgm it is working:


Output:
In Outerclass - amethod()
In Innerclass - bmethod()
In Innerclass - amethod()
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Anonymous class simple example:


Advantage of anonymouse classes is, that you must not create the new class itself.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Thanks for the reply.

What about achieving inheritance without using the keyword "extends"" whether it is possible or not?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

interface parentinterface {

public class implementparentinterface implements parentinterface{
public void parentmethod(){
System.out.println("class insideparentinterface---parentmethod");
}
}
}

class childimplement implements parentinterface{

public static void main(String args[]){

implementparentinterface ipi = new implementparentinterface(){
public void parentmethod(){
System.out.println("class implementparentinterface --- mainmethod");
}
};
ipi.parentmethod();

}

}


See that there is no extends keyword used but still, the parentmethod() is inherited and overrridden.


If you are not laughing at yourself, then you just didn't get the joke.
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
How it is possible to have method definition inside the interface.
By default, all the methods in the interface are public and abstract.

we are achieving inheritance thru extends and implements. Whether the anonymous class will come under inheritance?


implementparentinterface ipi = new implementparentinterface()
{
public void parentmethod()
{
System.out.println("class implementparentinterface --- mainmethod");
}
};


Can you explain the above coding. Where the block starts? whether the block is for object ipi?
[ August 25, 2005: Message edited by: Mark Henryson ]
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Everybody,
Annonymous inner class does not have any name but the general syntax of such class is

new <<name of super class>>/<<name of interface implements>> ()/*empty or parameterizd constructor*/
{
//one or more method implementation

}//end of class

so you are achiving inheritance without extends keyword.

Regards,
Shrinivas
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Only methods which are directly under an interface cannot have method definition.
But a class inside an interface can have methods which are well formed.

Anonymous inner-class can extend/implement a class/interface (But only one at a time.i.e., It can either extend a class or implement an interface).

implementparentinterface ipi = new implementparentinterface(){
public void parentmethod()
{
System.out.println("class implementparentinterface --- mainmethod");
}
};




Block starts at this point:

implementparentinterface ipi = new implementparentinterface(){


and ends at this point:

};


By doing this you say to the JVM,
1.Please create an anonymous class of type implementparentinterface
2. Kindly override the method parentmethod() and
3. Have the variable ipi point to the anonymous class.

I suggest you to read some tutorial on inner classes.
You'll love it and probably give in to it.
[ August 25, 2005: Message edited by: Arun Kumarr ]
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200

interface parentinterface {

public class implementparentinterface implements parentinterface{
public void parentmethod(){
System.out.println("class insideparentinterface---parentmethod");
}
}
}

class childimplement implements parentinterface{

public static void main(String args[]){

implementparentinterface ipi = new implementparentinterface(){
public void parentmethod(){
System.out.println("class implementparentinterface --- mainmethod");
}
};
ipi.parentmethod();

}

}


The output for this program, iam getting as:
class implementparentinterface --- mainmethod

You are creating the object for the super class and calling the parentmethod thru' super class object. Then how we get the sub classs parentmethod.

Below is the prgoram I tried:


Output:
class superclass - parentmethod
phil shea
Greenhorn

Joined: Aug 24, 2005
Posts: 12
code:
--------------------------------------------------------------------------------

public class Outerclass
{
public void amethod()
{
System.out.println("In Outerclass - amethod()");
}
class Innerclass extends Outerclass
{
public void bmethod()
{
System.out.println("In Innerclass - bmethod()");
}
public void amethod()
{
System.out.println("In Innerclass - amethod()");
}
}
public static void main(String args[])
{
Outerclass o = new Outerclass();
Outerclass.Innerclass i = new Outerclass().new Innercla();
o.amethod();
i.bmethod();
i.amethod();
}
}


The above code has been posted in this thread. I tried compiling this code. But it's not compiling. Can anyone try and let me know.

-phil
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Try reading the error the compiler gives you.
It's pretty obvious what the problem is.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Achieving inheritance without extends