This question already has answers here:
Can we instantiate an abstract class?
(16 answers)
Closed 7 years ago.
abstract class A {
public void disp() {
System.out.print("Abstract");
}
}
public class B {
public static void main(String args[]) {
A object = new A(){ };
object.disp();
}
}
I am aware that Abstract classes cannot be instantiated, but confused on this code.
Actually what this code mean ?
The subtlety here is in the "{}". It means you explicitly provide an anonymous implementation for the missing parts (the missing parts are abstract methods) of the abstract class A allowing you to instantiate it.
But there's no abstract method in A, therefore the anonymous implementation is empty.
Example showing the behaviour with at least one abstract method:
public abstract class A {
public abstract void bar();
public void disp() { System.out.print("Abstract"); }
}
public class B {
public static void main(String args[]) {
A object = new A() {
#Override public void bar() { System.out.print("bar"); }
};
object.disp(); //prints "Abstract"
object.bar(); //prints "bar"
}
}
This is called an anonymous inner class. You are not instantiating the abstract class, you are instantiating the concrete anonymous inner class which extends the abstract class. Of course, in order for this to be allowed, the anonymous inner class must provide implementations for all the abstract members of the abstract superclass … which it does in this case, because the abstract superclass has no abstract members.
Related
This question already has answers here:
Superclass reference not able to call subclass method in Java
(2 answers)
Closed 2 years ago.
abstract class superclass {
public abstract void method();
}
class subclass extends superclass {
public void method() {
//do something
}
public void newMethod() {
//do something
}
}
public class mainclass {
public static void main(String[]args) {
superclass abc = new subclass();
abc.method();
abc.newMethod(); //cannot find symbol error
}
}
In the above example, can new methods be not written in the derived class of an abstract class? If I do that, it raises an error.
When extending a Superclass you can in fact add more methods. Hoevery in this case you assign your new subclass() to a variable of the type superclass which means that you will only have access to the methods wich are a part of that type. In this case that's only method(). If you want to use both methods you should write instead:
subclass abc = new subclass();
or cast it on demand:
((subclass) abc).newMethod();
Here I instantiate objects from two abstract classes and an interface. I wonder why I can do that in three cases, especially the case of NotShape class where there is no abstract method. And the second question is that, What is the "super" when I instantiate the object of NotShape class? Is it belong to Object class or NotShape class itself? I thank you so much.
abstract class Shape{
String descrOfShape = "This is a shape";
abstract void draw();
}
abstract class NotShape {
String descrOfNotShape = "This is not a shape";
void printInfo() {
System.out.println("Can't be measured");
}
}
interface Test{
int ID = 10;
void showResult();
}
public class InstantiateObjects {
public static void main(String[] args) {
Shape s = new Shape() {
#Override
void draw() {
}
};
NotShape ns = new NotShape() {
#Override
void printInfo() {
super.printInfo(); /*What is the super? Is it belong to Object
class or NotShape class?*/
}
};
Test t = new Test() {
#Override
public void showResult() {
}
};
System.out.println(s.descrOfShape);
System.out.println(ns.descrOfNotShape);
System.out.println(t.ID);
}
}
You're not instantiating an abstract class or interface, you're instantiating a private anonymous extension/implementation of the abstract class/interface.
More reading: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Here You implementing interface or abstract class on your main method. You can not instantiate abstract class or interface never. But you can inherit then from another class or interface(depends on inheritance rules). Here you are instantiate a anonymus class which means you are implementing your interface or extending your abstruct classes. And your
#Override
void draw() {
}
and
#Override
void printInfo() {
super.printInfo(); /*What is the super? Ans: super means its call its parent class. Is it belong to Object class or NotShape class? Ans: Obviously its belongs to parent class NotShape*/
}
They are obviously overiding super classes(interfaces and abstruct classes) methods. You can try this then you can see its overiding your printinfo method
NotShape ns = new NotShape() {
#Override
void printInfo() {
super.printInfo(); /*What is the super? Is it belong to Object
class or NotShape class?*/
}
};
ns.printInfo();
That means you are calling your anonymus classes method. and then its calling your super classes because you are calling super.printInfo();
NB: take further study on highlighted terms. Thanks.
This question already has answers here:
Why would a static nested interface be used in Java?
(11 answers)
Closed 7 years ago.
For below code,
interface SuperInterface{
void f();
interface StaticNestedInterface{
}
}
class Sub implements SuperInterface{
public void f(){
}
}
public class Dummy {
public static void main(String[] args) {
Sub x = new Sub();
}
}
Compiler does not ask class Sub to implement interface StaticNestedInterface.
Is class Sub a valid java code?
Yes, its a valid Java code.
You are just declaring StaticNestedInterface in SuperInterface. One way to use nested interface would be
interface SuperInterface {
void f();
StaticNestedInterface sni();
interface StaticNestedInterface {
void a();
}
}
class Sub implements SuperInterface{
public void f(){
}
#Override
public StaticNestedInterface sni() {
return null;
}
}
Yes its a valid java code. Java compiler will create public static SuperInterface$StaticNestedInterface{}
To use the nested interface class Sub implements SuperInterface.StaticNestedInterface then compiler will ask Sub class to implement the method declared on StaticNestedInterface
interface StaticNestedInterface{
void a();
void b();
}
interface SuperInterface extends StaticNestedInterface{
void f();
}
class Sub implements SuperInterface{
//now compiler require all methods(a, b and f)
}
Wat does it mean by indirect Instantiation of abstract class ? how do
we achieve this ?
as i tried few times like .. it gives error has any one done something regarding this
abstract class hello //abstract class declaration
{
void leo() {}
}
abstract class test {} //2'nd abstract class
class dudu { //main class
public static void main(String args[])
{
hello d = new test() ; // tried here
}
}
We can't instantiate an abstract class .If we want than we have to extend it.
You can't instantiate an abstract class. The whole idea of Abstract class is to declare something which is common among subclasses and then extend it.
public abstract class Human {
// This class can't be instantiated, there can't be an object called Human
}
public Male extends Human {
// This class can be instantiated, getting common features through extension from Human class
}
public Female extends Human {
// This class can be instantiated, getting common features through extension from Human class
}
For more: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Wat does it mean my indirect instanciation of abstract class ? how do we achieve this ?
I'd need to see the context in which that phrase is used, but I expect that "indirect instantiation" means instantiation of a non-abstract class that extends your abstract class.
For example
public abstract class A {
private int a;
public A(int a) {
this.a = a;
}
...
}
public B extends A {
public B() {
super(42);
}
...
}
B b = new B(); // This is an indirect instantiation of A
// (sort of ....)
A a = new A(99); // This is a compilation error. You cannot
// instantiate an abstract class directly.
You can't create instance of abstract class, I think this is what you are trying to do.
abstract class hello //abstract class declaration
{
void leo() {}
}
class test extends hello
{
void leo() {} // Custom test's implementation of leo method
}
you cannot create object for Abstract class in java.
Refer this link-http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
This question already has answers here:
Can we instantiate an abstract class?
(16 answers)
Closed 9 years ago.
Couldy you clarify why this works:
public abstract class AbstractClassCreationTest {
public void hello(){
System.out.println("I'm the abstract class' instance!");
}
public static void main(String[] args) {
AbstractClassCreationTest acct = new AbstractClassCreationTest(){};
acct.hello();
}
}
I suppose it contradicts to the specification where we can find:
It is a compile-time error if an attempt is made to create an instance of an abstract
class using a class instance creation expression (§15.9).
You may have not noticed the difference:
new AbstractClassCreationTest(){};
versus
new AbstractClassCreationTest();
That extra {} is the body of a new, nameless class that extends the abstract class. You have created an instance of an anonymous class and not of an abstract class.
Now go ahead an declare an abstract method in the abstract class, watch how compiler forces you to implement it inside {} of anonymous class.
Notice the difference between:
AbstractClassCreationTest acct = new AbstractClassCreationTest(){};//case 1
NonAbstractClassCreationTest acct = new NonAbstractClassCreationTest();//case 2
case1 is an anonymous class definition. You are not instantiating an abstract class; instead you are instantiating a subType of said abstract class.
Here you are not creating the object of the AbstractClassCreationTest class , actually you are creating an object of anonymous inner class who extends the AbstractClassCreationTest class.This is because you have written new AbstractClassCreationTest(){} not new AbstractClassCreationTest()
You can know more about anonymous inner class from here
An abstract class cannot instantiated.
You must create an extension class extends an abstract class and so istantiated this new class.
public abstract class AbstractClassCreationTest {
public void hello(){
System.out.println("I'm the abstract class' instance!");
}
}
public class MyExtClass extends AbstractClassCreationTest() {
}
public static void main(String[] args) {
MyExtClass acct = new MyExtClass(){};
acct.hello();
}
I post this. Can be useful for you. Have a nice day