Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Achieving inheritance without extends

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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!
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I tried the below pgm it is working:


Output:
In Outerclass - amethod()
In Innerclass - bmethod()
In Innerclass - amethod()
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous class simple example:


Advantage of anonymouse classes is, that you must not create the new class itself.
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

What about achieving inheritance without using the keyword "extends"" whether it is possible or not?
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading the error the compiler gives you.
It's pretty obvious what the problem is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic