• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract........  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
pl. clear this
is it possible to create an object for abstract class
see the code
abstract class a{
a()
{System.out.println/("a constructor");
}
}
this works fine but creating constructor implies that object
created so can we create a object?
a1nswer pl .......
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is quite reasonable for an abstract class to provide a constructor to allow derived classes to initialise the non-abstract parts of the class. It still shouldn't let you instantiate one, though.
Consider this example:
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ur example is ok
but my question is an object is created for abstract class or not .i'm able to use this constructor so it means an object is created so i have problem here clear me pl...
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my example, only an object of class B can be created. If you wish, you may treat it as if it is an object of class A, but it is still really an object of class B.
This is very similar to the concept of interfaces. You can't ever create an interface object, but you can create an object of a class which implements that interface. You can then treat the object as if it is an interface object.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so u mean object is not created for the abstract class ...
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we got an OOP misunderstanding here.
If you have class B extends A, and then you do
A a = new B(); you have an instance of class B, that
you treat as class A.
So, you have abstract class A:
abstract class A {
A() { // do init }
abstract void dosome();
}
class B extends A {
void dosome() { // do some }
}
Now, we invoke new instance of class B :
B b = new B();
In this case we asked for B, but actual constructor called is A(). The idea is that we only call it A(), actually this code belongs to class B as well. So, no instance of A is created, we just used the code in A() to create B.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!