Java inheritance behaviour with instance of
I have the following
Class A extends B
B class is an abstract class
What will be returned if I do:
A a = new A();
a instance of B?
If it returns false, which solution I could use for having true as result?
java
add a comment |
I have the following
Class A extends B
B class is an abstract class
What will be returned if I do:
A a = new A();
a instance of B?
If it returns false, which solution I could use for having true as result?
java
1
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
1
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
1
@Rohlex32, it's the other way around,A extends B
.
– Mick Mnemonic
Nov 14 '18 at 19:23
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29
add a comment |
I have the following
Class A extends B
B class is an abstract class
What will be returned if I do:
A a = new A();
a instance of B?
If it returns false, which solution I could use for having true as result?
java
I have the following
Class A extends B
B class is an abstract class
What will be returned if I do:
A a = new A();
a instance of B?
If it returns false, which solution I could use for having true as result?
java
java
edited Nov 14 '18 at 19:29
Noname
asked Nov 14 '18 at 19:14
NonameNoname
2516
2516
1
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
1
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
1
@Rohlex32, it's the other way around,A extends B
.
– Mick Mnemonic
Nov 14 '18 at 19:23
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29
add a comment |
1
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
1
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
1
@Rohlex32, it's the other way around,A extends B
.
– Mick Mnemonic
Nov 14 '18 at 19:23
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29
1
1
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
1
1
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
1
1
@Rohlex32, it's the other way around,
A extends B
.– Mick Mnemonic
Nov 14 '18 at 19:23
@Rohlex32, it's the other way around,
A extends B
.– Mick Mnemonic
Nov 14 '18 at 19:23
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29
add a comment |
2 Answers
2
active
oldest
votes
//Define these classes.
class A {} //It can be an abstract class as well.
class B extends A {}
//Main code.
A b = new B();
System.out.println(b instanceof A); //prints true.
If you try B instanceof A // you will get a compilation error because B is not an object, but it is a class name.
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
add a comment |
"x instanceof y" returns true if "object" x is instance of class "y". The word instance is used for an object. Since A and B are both classes the code should return a compilation error
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%2f53307274%2fjava-inheritance-behaviour-with-instance-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
//Define these classes.
class A {} //It can be an abstract class as well.
class B extends A {}
//Main code.
A b = new B();
System.out.println(b instanceof A); //prints true.
If you try B instanceof A // you will get a compilation error because B is not an object, but it is a class name.
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
add a comment |
//Define these classes.
class A {} //It can be an abstract class as well.
class B extends A {}
//Main code.
A b = new B();
System.out.println(b instanceof A); //prints true.
If you try B instanceof A // you will get a compilation error because B is not an object, but it is a class name.
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
add a comment |
//Define these classes.
class A {} //It can be an abstract class as well.
class B extends A {}
//Main code.
A b = new B();
System.out.println(b instanceof A); //prints true.
If you try B instanceof A // you will get a compilation error because B is not an object, but it is a class name.
//Define these classes.
class A {} //It can be an abstract class as well.
class B extends A {}
//Main code.
A b = new B();
System.out.println(b instanceof A); //prints true.
If you try B instanceof A // you will get a compilation error because B is not an object, but it is a class name.
edited Nov 14 '18 at 19:36
answered Nov 14 '18 at 19:32
Yan KhonskiYan Khonski
3,82762351
3,82762351
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
add a comment |
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
And if class A is abstract ?
– Noname
Nov 14 '18 at 19:35
1
1
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
It will also work. You should run this code yourself in any IDE.
– Yan Khonski
Nov 14 '18 at 19:36
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
I don't think it matters @Capheda if A is abstract or not. The answer is the same.
– markspace
Nov 14 '18 at 19:37
add a comment |
"x instanceof y" returns true if "object" x is instance of class "y". The word instance is used for an object. Since A and B are both classes the code should return a compilation error
add a comment |
"x instanceof y" returns true if "object" x is instance of class "y". The word instance is used for an object. Since A and B are both classes the code should return a compilation error
add a comment |
"x instanceof y" returns true if "object" x is instance of class "y". The word instance is used for an object. Since A and B are both classes the code should return a compilation error
"x instanceof y" returns true if "object" x is instance of class "y". The word instance is used for an object. Since A and B are both classes the code should return a compilation error
answered Nov 14 '18 at 19:21
Sameer PandeSameer Pande
11
11
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%2f53307274%2fjava-inheritance-behaviour-with-instance-of%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
1
This isn't really possible. Since B inherits from A, A is never an instance of B. It's the same reason why you can cast B as type A but you can't cast A as type B
– Rohlex32
Nov 14 '18 at 19:16
1
Sounds like an XY Problem. Please tell us what you are actually trying to do, working against the language design like you are doing here is never going to turn out well.
– markspace
Nov 14 '18 at 19:20
1
@Rohlex32, it's the other way around,
A extends B
.– Mick Mnemonic
Nov 14 '18 at 19:23
@MickMnemonic Whoops! Misread!
– Rohlex32
Nov 14 '18 at 19:29