In java abstract classes cannot instantiate objects but they can have references?
J
2009-09-13 01:26:14 UTC
my question is then how can we define constructors for abstract classes ?
Four answers:
Badajoz
2009-09-13 01:38:53 UTC
Abstract classes are similar to concrete classes(non abstract) when it comes to constructor definition. The only difference is that in order to create a new instance of an Abstract class, you have to give an implementation to the methods that were defined as abstract whereas in concrete classes you can instantiate it right away provided that the constructor modifier is accessible.
Fudge
2009-09-13 01:34:41 UTC
how can we define constructors for abstract classes :
I'd assume u r asking ' why' , and the answer is that's because abstract classes are meant to be SUPER classes for other classes. So when objects of a derived class is created FIRST the constructor of the SUPER abstract class is called in order to initialize the fields of the SUPER class, hence the need for defining constructor for abstract classes .
eg :
abstract class Foo
{
public Foo() {}
}
class Bar extends Foo
{
//here the constructors for super class is called "behind the scene"
public Bar()
{
}
ps: an abstract class can have concert ( NON abstract) methods but concert classes cannot have abstract methods
} //end class Bar
2016-12-11 08:19:47 UTC
Java's precis classes can't be quickly instantiated. From the code you have, it would not look such as you elect for for the Clock type to be an precis type. merely delete the "precis" and it is going to paintings wonderful.
pat
2009-09-13 01:28:51 UTC
dssd
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.