Calling method that exists in child classes but not in parent classB extends A, and in a list of As, some indexes are instanceof B. Why can't I then call a method with index.Bmethod()?Java - calling a method by passing child instance to a function with parameter of parent class typeHow can I access the method of the child class from parent reference?What's the difference between a method and a function?How do I call one constructor from another in Java?Getting the class name of an instance?Understanding Python super() with __init__() methodsInterface vs Abstract Class (general OO)Naming Classes - How to avoid calling everything a “<WhatEver>Manager”?What is the difference between an interface and abstract class?Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Reference - What does this error mean in PHP?Why not inherit from List<T>?
How can I show that the speed of light in vacuum is the same in all reference frames?
What kind of curve (or model) should I fit to my percentage data?
What do Unicorns want?
My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?
Does switching on an old games console without a cartridge damage it?
Count the identical pairs in two lists
Book in which the "mountain" in the distance was a hole in the flat world
Killing a star safely
is FIND WORDS in P?
How can I disable a reserved profile?
Can a creature sustain itself by eating its own severed body parts?
Is there an English word to describe when a sound "protrudes"?
Strange LED behavior
Why is the UH-60 tail rotor canted?
How should I handle a question regarding my regrets during an interview?
Editing specific portions of DEM using ArcGIS Desktop?
How much did NASA help with the making of "First Man"?
Is it OK to accept a job opportunity while planning on not taking it?
Importance of moon phases for Apollo missions
Can "Taking algebraic closure" be made into a functor?
Why was Quirrell said to be in the Black Forest if Voldemort was actually in Albania?
MITM on HTTPS traffic in Kazakhstan 2019
How to handle not being able to attend as often as I'd like
Cargo capacity of a kayak
Calling method that exists in child classes but not in parent class
B extends A, and in a list of As, some indexes are instanceof B. Why can't I then call a method with index.Bmethod()?Java - calling a method by passing child instance to a function with parameter of parent class typeHow can I access the method of the child class from parent reference?What's the difference between a method and a function?How do I call one constructor from another in Java?Getting the class name of an instance?Understanding Python super() with __init__() methodsInterface vs Abstract Class (general OO)Naming Classes - How to avoid calling everything a “<WhatEver>Manager”?What is the difference between an interface and abstract class?Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Reference - What does this error mean in PHP?Why not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
public class Parent
....
public class Child1 extends Parent
....
public void foo()
....
public class Child2 extends Parent
....
public void foo()
....
Here method foo()
only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo()
method on obj
which is Parent
class's reference then I need to use intanceof
with multiple if..else
which I want to avoid.
Parent obj = ...// Object of one of the child classes
obj.foo();
EDIT: I Need to use type of obj
as Parent
only. Else I will not be able to call methods on obj which exists in Parent class.
My Solution: The approach that I am thinking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj
to that interface and call foo() method like this:
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
Is there a better approach ? Or any improvement to this one?
java oop class-design instanceof
add a comment |
public class Parent
....
public class Child1 extends Parent
....
public void foo()
....
public class Child2 extends Parent
....
public void foo()
....
Here method foo()
only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo()
method on obj
which is Parent
class's reference then I need to use intanceof
with multiple if..else
which I want to avoid.
Parent obj = ...// Object of one of the child classes
obj.foo();
EDIT: I Need to use type of obj
as Parent
only. Else I will not be able to call methods on obj which exists in Parent class.
My Solution: The approach that I am thinking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj
to that interface and call foo() method like this:
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
Is there a better approach ? Or any improvement to this one?
java oop class-design instanceof
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
I CAN NOT changeParent
class, as I do not manage it.
– Learner
May 6 '14 at 9:15
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Yes, I can do that.
– Learner
May 6 '14 at 14:37
add a comment |
public class Parent
....
public class Child1 extends Parent
....
public void foo()
....
public class Child2 extends Parent
....
public void foo()
....
Here method foo()
only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo()
method on obj
which is Parent
class's reference then I need to use intanceof
with multiple if..else
which I want to avoid.
Parent obj = ...// Object of one of the child classes
obj.foo();
EDIT: I Need to use type of obj
as Parent
only. Else I will not be able to call methods on obj which exists in Parent class.
My Solution: The approach that I am thinking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj
to that interface and call foo() method like this:
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
Is there a better approach ? Or any improvement to this one?
java oop class-design instanceof
public class Parent
....
public class Child1 extends Parent
....
public void foo()
....
public class Child2 extends Parent
....
public void foo()
....
Here method foo()
only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo()
method on obj
which is Parent
class's reference then I need to use intanceof
with multiple if..else
which I want to avoid.
Parent obj = ...// Object of one of the child classes
obj.foo();
EDIT: I Need to use type of obj
as Parent
only. Else I will not be able to call methods on obj which exists in Parent class.
My Solution: The approach that I am thinking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj
to that interface and call foo() method like this:
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
Is there a better approach ? Or any improvement to this one?
java oop class-design instanceof
java oop class-design instanceof
edited May 6 '14 at 10:34
Learner
asked May 6 '14 at 8:55
LearnerLearner
5442 gold badges10 silver badges26 bronze badges
5442 gold badges10 silver badges26 bronze badges
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
I CAN NOT changeParent
class, as I do not manage it.
– Learner
May 6 '14 at 9:15
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Yes, I can do that.
– Learner
May 6 '14 at 14:37
add a comment |
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
I CAN NOT changeParent
class, as I do not manage it.
– Learner
May 6 '14 at 9:15
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Yes, I can do that.
– Learner
May 6 '14 at 14:37
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
I CAN NOT change
Parent
class, as I do not manage it.– Learner
May 6 '14 at 9:15
I CAN NOT change
Parent
class, as I do not manage it.– Learner
May 6 '14 at 9:15
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Yes, I can do that.
– Learner
May 6 '14 at 14:37
Yes, I can do that.
– Learner
May 6 '14 at 14:37
add a comment |
5 Answers
5
active
oldest
votes
The polymorphism is applied on object reference, not a type. When you call
FooInterface obj = ...// Object of one of the child classes
obj.foo();
the child class method foo()
is called.
1
Look at the OP question again.Parent
doesn't have anyfoo()
method.
– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
@RomanC, With this solution I will not be able to call methods onobj
which exists inParent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modifyParent
class.
– Learner
May 6 '14 at 9:21
|
show 9 more comments
You can't do it with parent object reference until an unless method is declared in parent class/interface itself.
You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.
Here contract
means abstract methods
.
you can try in this way where there is no need to put a check it.
FooInterface sc =new Child1();
sc.foo();
...
interface FooInterface
void foo();
public class Parent
public class Child1 extends Parent implements FooInterface
public void foo()
public class Child2 extends Parent implements FooInterface
public void foo()
With this solution I will not be able to call methods onsc
which exists in Parent class. I edited my question to mention this. Thanks.
– Learner
May 6 '14 at 11:35
have you madeChild1
implementsFooInterface
same forChild2
?
– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of typeParent
and thus I need to useinstanceof
once to see if it is of typeFooInterface
.
– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
add a comment |
The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
add a comment |
If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example
public class HelloWorld
public static void main(String args[]) throws FileNotFoundException
SuperClass sc =new Child1();
if(sc instanceof Child1)//Do same for Child2
((Child1)sc).foo();
class SuperClass
class Child1 extends SuperClass
public void foo()
System.out.println("From child1");
class Child2 extends SuperClass
public void foo()
System.out.println("From child2");
Output :
From child1
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
add a comment |
You could implement an AbstractChild
inheriting from Parent
and then extend this class instead of Parent
:
public class Parent
....
public abstract class AbstractChild extends Parent
public abstract void foo();
public class Child1 extends AbstractChild
....
public void foo()
....
public class Child2 extends AbstractChild
....
public void foo()
....
So you need to only check if your instance is instanceof AbstractChild
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23490151%2fcalling-method-that-exists-in-child-classes-but-not-in-parent-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
The polymorphism is applied on object reference, not a type. When you call
FooInterface obj = ...// Object of one of the child classes
obj.foo();
the child class method foo()
is called.
1
Look at the OP question again.Parent
doesn't have anyfoo()
method.
– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
@RomanC, With this solution I will not be able to call methods onobj
which exists inParent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modifyParent
class.
– Learner
May 6 '14 at 9:21
|
show 9 more comments
The polymorphism is applied on object reference, not a type. When you call
FooInterface obj = ...// Object of one of the child classes
obj.foo();
the child class method foo()
is called.
1
Look at the OP question again.Parent
doesn't have anyfoo()
method.
– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
@RomanC, With this solution I will not be able to call methods onobj
which exists inParent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modifyParent
class.
– Learner
May 6 '14 at 9:21
|
show 9 more comments
The polymorphism is applied on object reference, not a type. When you call
FooInterface obj = ...// Object of one of the child classes
obj.foo();
the child class method foo()
is called.
The polymorphism is applied on object reference, not a type. When you call
FooInterface obj = ...// Object of one of the child classes
obj.foo();
the child class method foo()
is called.
edited May 6 '14 at 9:08
answered May 6 '14 at 9:03
Roman CRoman C
44.5k15 gold badges47 silver badges118 bronze badges
44.5k15 gold badges47 silver badges118 bronze badges
1
Look at the OP question again.Parent
doesn't have anyfoo()
method.
– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
@RomanC, With this solution I will not be able to call methods onobj
which exists inParent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modifyParent
class.
– Learner
May 6 '14 at 9:21
|
show 9 more comments
1
Look at the OP question again.Parent
doesn't have anyfoo()
method.
– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
@RomanC, With this solution I will not be able to call methods onobj
which exists inParent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modifyParent
class.
– Learner
May 6 '14 at 9:21
1
1
Look at the OP question again.
Parent
doesn't have any foo()
method.– Braj
May 6 '14 at 9:05
Look at the OP question again.
Parent
doesn't have any foo()
method.– Braj
May 6 '14 at 9:05
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
@braj Why doesn't?
– Roman C
May 6 '14 at 9:09
1
1
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
How can you call a method on the reference of interface where interface has not declared that method? Look at my post.
– Braj
May 6 '14 at 9:10
2
2
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
@Learner One more comment to you, when designing your classes consider coding to interfaces, not classes.
– Roman C
May 6 '14 at 9:20
1
1
@RomanC, With this solution I will not be able to call methods on
obj
which exists in Parent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modify Parent
class.– Learner
May 6 '14 at 9:21
@RomanC, With this solution I will not be able to call methods on
obj
which exists in Parent
class. Yes, I understand advantage of code to interface but this is legacy code and I CAN NOT modify Parent
class.– Learner
May 6 '14 at 9:21
|
show 9 more comments
You can't do it with parent object reference until an unless method is declared in parent class/interface itself.
You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.
Here contract
means abstract methods
.
you can try in this way where there is no need to put a check it.
FooInterface sc =new Child1();
sc.foo();
...
interface FooInterface
void foo();
public class Parent
public class Child1 extends Parent implements FooInterface
public void foo()
public class Child2 extends Parent implements FooInterface
public void foo()
With this solution I will not be able to call methods onsc
which exists in Parent class. I edited my question to mention this. Thanks.
– Learner
May 6 '14 at 11:35
have you madeChild1
implementsFooInterface
same forChild2
?
– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of typeParent
and thus I need to useinstanceof
once to see if it is of typeFooInterface
.
– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
add a comment |
You can't do it with parent object reference until an unless method is declared in parent class/interface itself.
You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.
Here contract
means abstract methods
.
you can try in this way where there is no need to put a check it.
FooInterface sc =new Child1();
sc.foo();
...
interface FooInterface
void foo();
public class Parent
public class Child1 extends Parent implements FooInterface
public void foo()
public class Child2 extends Parent implements FooInterface
public void foo()
With this solution I will not be able to call methods onsc
which exists in Parent class. I edited my question to mention this. Thanks.
– Learner
May 6 '14 at 11:35
have you madeChild1
implementsFooInterface
same forChild2
?
– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of typeParent
and thus I need to useinstanceof
once to see if it is of typeFooInterface
.
– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
add a comment |
You can't do it with parent object reference until an unless method is declared in parent class/interface itself.
You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.
Here contract
means abstract methods
.
you can try in this way where there is no need to put a check it.
FooInterface sc =new Child1();
sc.foo();
...
interface FooInterface
void foo();
public class Parent
public class Child1 extends Parent implements FooInterface
public void foo()
public class Child2 extends Parent implements FooInterface
public void foo()
You can't do it with parent object reference until an unless method is declared in parent class/interface itself.
You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.
Here contract
means abstract methods
.
you can try in this way where there is no need to put a check it.
FooInterface sc =new Child1();
sc.foo();
...
interface FooInterface
void foo();
public class Parent
public class Child1 extends Parent implements FooInterface
public void foo()
public class Child2 extends Parent implements FooInterface
public void foo()
edited May 6 '14 at 9:19
answered May 6 '14 at 9:07
BrajBraj
41.3k5 gold badges42 silver badges63 bronze badges
41.3k5 gold badges42 silver badges63 bronze badges
With this solution I will not be able to call methods onsc
which exists in Parent class. I edited my question to mention this. Thanks.
– Learner
May 6 '14 at 11:35
have you madeChild1
implementsFooInterface
same forChild2
?
– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of typeParent
and thus I need to useinstanceof
once to see if it is of typeFooInterface
.
– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
add a comment |
With this solution I will not be able to call methods onsc
which exists in Parent class. I edited my question to mention this. Thanks.
– Learner
May 6 '14 at 11:35
have you madeChild1
implementsFooInterface
same forChild2
?
– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of typeParent
and thus I need to useinstanceof
once to see if it is of typeFooInterface
.
– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
With this solution I will not be able to call methods on
sc
which exists in Parent class. I edited my question to mention this. Thanks.– Learner
May 6 '14 at 11:35
With this solution I will not be able to call methods on
sc
which exists in Parent class. I edited my question to mention this. Thanks.– Learner
May 6 '14 at 11:35
have you made
Child1
implements FooInterface
same for Child2
?– Braj
May 6 '14 at 11:49
have you made
Child1
implements FooInterface
same for Child2
?– Braj
May 6 '14 at 11:49
This is what I mentioned in My Solution, only difference is that in my solution, reference is of type
Parent
and thus I need to use instanceof
once to see if it is of type FooInterface
.– Learner
May 6 '14 at 12:56
This is what I mentioned in My Solution, only difference is that in my solution, reference is of type
Parent
and thus I need to use instanceof
once to see if it is of type FooInterface
.– Learner
May 6 '14 at 12:56
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
so you don't want to change your design then its not possible as I told you parent class/interface doesn't have any knowledge about the child class. how would parent class/interface know that child has created x methods?
– Braj
May 6 '14 at 13:00
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
How about my solution that I had mentioned in the question? it does solve my problem. But is good approach is what I want to know?
– Learner
May 6 '14 at 14:32
add a comment |
The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
add a comment |
The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
add a comment |
The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface)
((FooInterface)obj).foo();
answered May 19 '14 at 5:17
LearnerLearner
5442 gold badges10 silver badges26 bronze badges
5442 gold badges10 silver badges26 bronze badges
add a comment |
add a comment |
If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example
public class HelloWorld
public static void main(String args[]) throws FileNotFoundException
SuperClass sc =new Child1();
if(sc instanceof Child1)//Do same for Child2
((Child1)sc).foo();
class SuperClass
class Child1 extends SuperClass
public void foo()
System.out.println("From child1");
class Child2 extends SuperClass
public void foo()
System.out.println("From child2");
Output :
From child1
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
add a comment |
If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example
public class HelloWorld
public static void main(String args[]) throws FileNotFoundException
SuperClass sc =new Child1();
if(sc instanceof Child1)//Do same for Child2
((Child1)sc).foo();
class SuperClass
class Child1 extends SuperClass
public void foo()
System.out.println("From child1");
class Child2 extends SuperClass
public void foo()
System.out.println("From child2");
Output :
From child1
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
add a comment |
If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example
public class HelloWorld
public static void main(String args[]) throws FileNotFoundException
SuperClass sc =new Child1();
if(sc instanceof Child1)//Do same for Child2
((Child1)sc).foo();
class SuperClass
class Child1 extends SuperClass
public void foo()
System.out.println("From child1");
class Child2 extends SuperClass
public void foo()
System.out.println("From child2");
Output :
From child1
If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example
public class HelloWorld
public static void main(String args[]) throws FileNotFoundException
SuperClass sc =new Child1();
if(sc instanceof Child1)//Do same for Child2
((Child1)sc).foo();
class SuperClass
class Child1 extends SuperClass
public void foo()
System.out.println("From child1");
class Child2 extends SuperClass
public void foo()
System.out.println("From child2");
Output :
From child1
answered May 6 '14 at 9:03
Aniket ThakurAniket Thakur
44.8k28 gold badges202 silver badges225 bronze badges
44.8k28 gold badges202 silver badges225 bronze badges
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
add a comment |
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
Well, that's what I think is not a good approach and hence don't want to use it. If there are multiple child classes then it would be ugly. Isn't it ?
– Learner
May 6 '14 at 9:10
add a comment |
You could implement an AbstractChild
inheriting from Parent
and then extend this class instead of Parent
:
public class Parent
....
public abstract class AbstractChild extends Parent
public abstract void foo();
public class Child1 extends AbstractChild
....
public void foo()
....
public class Child2 extends AbstractChild
....
public void foo()
....
So you need to only check if your instance is instanceof AbstractChild
.
add a comment |
You could implement an AbstractChild
inheriting from Parent
and then extend this class instead of Parent
:
public class Parent
....
public abstract class AbstractChild extends Parent
public abstract void foo();
public class Child1 extends AbstractChild
....
public void foo()
....
public class Child2 extends AbstractChild
....
public void foo()
....
So you need to only check if your instance is instanceof AbstractChild
.
add a comment |
You could implement an AbstractChild
inheriting from Parent
and then extend this class instead of Parent
:
public class Parent
....
public abstract class AbstractChild extends Parent
public abstract void foo();
public class Child1 extends AbstractChild
....
public void foo()
....
public class Child2 extends AbstractChild
....
public void foo()
....
So you need to only check if your instance is instanceof AbstractChild
.
You could implement an AbstractChild
inheriting from Parent
and then extend this class instead of Parent
:
public class Parent
....
public abstract class AbstractChild extends Parent
public abstract void foo();
public class Child1 extends AbstractChild
....
public void foo()
....
public class Child2 extends AbstractChild
....
public void foo()
....
So you need to only check if your instance is instanceof AbstractChild
.
edited May 6 '14 at 13:27
answered May 6 '14 at 13:08
daviooohdavioooh
11.1k26 gold badges104 silver badges205 bronze badges
11.1k26 gold badges104 silver badges205 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23490151%2fcalling-method-that-exists-in-child-classes-but-not-in-parent-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Whenever a method is common in the child, it should be place in parent. Why u r not putting that method in abstract class??
– Shoaib Chikate
May 6 '14 at 9:00
I CAN NOT change
Parent
class, as I do not manage it.– Learner
May 6 '14 at 9:15
Then use interface which is stated in the solution given below in the answers.
– Shoaib Chikate
May 6 '14 at 9:30
Can you introduce an intermediary class between Parent and Child?
– vikingsteve
May 6 '14 at 13:29
Yes, I can do that.
– Learner
May 6 '14 at 14:37