Why is this valid boolean b = new A() {} instanceof A; if A is an abstract class? [on hold]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have trouble understanding why this boolean b = new A() {} instanceof A; is a valid statement and not boolean b = new A() instanceof A; and why the former is true knowing that A is an abstract class.
java object-oriented
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    put on hold as off-topic by gnat, Bart van Ingen Schenau, David Arno, amon, Deduplicator 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, amon, Deduplicator
 
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have trouble understanding why this boolean b = new A() {} instanceof A; is a valid statement and not boolean b = new A() instanceof A; and why the former is true knowing that A is an abstract class.
java object-oriented
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    put on hold as off-topic by gnat, Bart van Ingen Schenau, David Arno, amon, Deduplicator 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, amon, Deduplicator
 
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have trouble understanding why this boolean b = new A() {} instanceof A; is a valid statement and not boolean b = new A() instanceof A; and why the former is true knowing that A is an abstract class.
java object-oriented
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have trouble understanding why this boolean b = new A() {} instanceof A; is a valid statement and not boolean b = new A() instanceof A; and why the former is true knowing that A is an abstract class.
java object-oriented
java object-oriented
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Apr 5 at 1:19
Dr.StoneDr.Stone
232
232
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Dr.Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    put on hold as off-topic by gnat, Bart van Ingen Schenau, David Arno, amon, Deduplicator 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, amon, Deduplicator
 
If this question can be reworded to fit the rules in the help center, please edit the question.
                    put on hold as off-topic by gnat, Bart van Ingen Schenau, David Arno, amon, Deduplicator 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, amon, Deduplicator
 
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
                                1 Answer
                            1
                        
active
oldest
votes
The instanceof A has nothing to do with whether the statement is valid or not, so your question is simpler if you leave it off from both statements:
A a = new A() {};
A a = new A();
The second statement isn't valid because A is abstract and you can't call new on any abstract class. If you could instantiate it, it would be an instance of A, but you can't.
The first statement is valid because it creates an anonymous class that extends A. This anonymous class isn't abstract (assuming A doesn't define any abstract methods), and so it can be instantiated.  It's an instance of A because all subclasses are instances of their parent classes, abstract or not. It's basically a shortcut for saying:
public class AnonymousNameIDontCareAbout extends A {}
A a = new AnonymousNameIDontCareAbout();
add a comment |
                                1 Answer
                            1
                        
active
oldest
votes
                                1 Answer
                            1
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
The instanceof A has nothing to do with whether the statement is valid or not, so your question is simpler if you leave it off from both statements:
A a = new A() {};
A a = new A();
The second statement isn't valid because A is abstract and you can't call new on any abstract class. If you could instantiate it, it would be an instance of A, but you can't.
The first statement is valid because it creates an anonymous class that extends A. This anonymous class isn't abstract (assuming A doesn't define any abstract methods), and so it can be instantiated.  It's an instance of A because all subclasses are instances of their parent classes, abstract or not. It's basically a shortcut for saying:
public class AnonymousNameIDontCareAbout extends A {}
A a = new AnonymousNameIDontCareAbout();
add a comment |
The instanceof A has nothing to do with whether the statement is valid or not, so your question is simpler if you leave it off from both statements:
A a = new A() {};
A a = new A();
The second statement isn't valid because A is abstract and you can't call new on any abstract class. If you could instantiate it, it would be an instance of A, but you can't.
The first statement is valid because it creates an anonymous class that extends A. This anonymous class isn't abstract (assuming A doesn't define any abstract methods), and so it can be instantiated.  It's an instance of A because all subclasses are instances of their parent classes, abstract or not. It's basically a shortcut for saying:
public class AnonymousNameIDontCareAbout extends A {}
A a = new AnonymousNameIDontCareAbout();
add a comment |
The instanceof A has nothing to do with whether the statement is valid or not, so your question is simpler if you leave it off from both statements:
A a = new A() {};
A a = new A();
The second statement isn't valid because A is abstract and you can't call new on any abstract class. If you could instantiate it, it would be an instance of A, but you can't.
The first statement is valid because it creates an anonymous class that extends A. This anonymous class isn't abstract (assuming A doesn't define any abstract methods), and so it can be instantiated.  It's an instance of A because all subclasses are instances of their parent classes, abstract or not. It's basically a shortcut for saying:
public class AnonymousNameIDontCareAbout extends A {}
A a = new AnonymousNameIDontCareAbout();
The instanceof A has nothing to do with whether the statement is valid or not, so your question is simpler if you leave it off from both statements:
A a = new A() {};
A a = new A();
The second statement isn't valid because A is abstract and you can't call new on any abstract class. If you could instantiate it, it would be an instance of A, but you can't.
The first statement is valid because it creates an anonymous class that extends A. This anonymous class isn't abstract (assuming A doesn't define any abstract methods), and so it can be instantiated.  It's an instance of A because all subclasses are instances of their parent classes, abstract or not. It's basically a shortcut for saying:
public class AnonymousNameIDontCareAbout extends A {}
A a = new AnonymousNameIDontCareAbout();
answered Apr 5 at 3:36
Karl BielefeldtKarl Bielefeldt
121k32217415
121k32217415
add a comment |
add a comment |