Can an abstract class have a constructor?
If so, how it can be used and for what purposes?
|
So a abstract class can only be used as a base class which is extended by some other class, right?
So the constructor(s) of an abstract class can have the usual access ... |
Why do we need constructors and private members in the abstract class? It is not like we are ever going to create an instance of that class.
|
I feel like I'm missing something here; can someone point out what I'm misunderstanding?!
I've got two classes, an Abstract and a Concrete, as follows:
public abstract class Abstract
{
protected ...
|
Possible Duplicate:
Abstract class constructor in Java
When we can't create an instance of abstract class, what is the purpose of a constructor?
|
I would like to know what purpose a constructor for an abstract class serves; as we do not instantiate abstract classes, why would we ever need such a constructor?
|
Why does an abstract class in Java have a constructor? What is it constructing, as we can't instantiate an abstract class?
|
|
public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
...
|
I created the following abstract class for job scheduler in red5:
package com.demogames.jobs;
import com.demogames.demofacebook.MysqlDb;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.scheduling.IScheduledJob;
import org.red5.server.api.so.ISharedObject;
import org.apache.log4j.Logger;
import org.red5.server.api.Red5;
/**
*
* @author ufk
*/
abstract public class DemoJob implements IScheduledJob {
protected ...
|
I have something like this:
public abstract class Menu {
public Menu() {
init();
}
...
|
Why can an abstract class have a constructor?
As we can't create an object of it why would we need a constructor in an abstract class?
|
Possible Duplicate:
What is the use of creating a constructor for an abstract class in Java?
Example:
abstract public class AbstractDemo
{
private ...
|
Even if an abstract class cannot be directly instantiated it must still be initialized and have a constructor executed when a subclass is instantiated. This is precisely why any class without a visible constructor cannot be subclassed. In most cases this translates to "a class without a non-private constructor cannot be subclassed", however there's an exception and that is when the ... |
You can define say, an Employee class, as abstract, but still have a constructor in it so subclasses can do a super() call. Although this makes sense that sub-classes would need to set up the parent class, it seems a little strange that an abstract class could have a constructor given that it can't be instantiated. What's really going on behind ... |
Hi Friends, I want to know why we can't use static at class level. Ex: static class A{} Because it doesn't have any meaning. Something is static in the context of something else. For example, static methods are static in the context of the class that contains them. But what is the context in case of a top-level class? What would ... |
First use is for classes that are not instantiable and are really just collections of static utility methods. A well-designed Java program won't have a lot of such classes (they aren't very OO), but occasionally they are useful. A second use might be a class that has a static factory method returning instances of subclasses of itself. The subclasses would have ... |
Someday, somewhere, someone is going to actually extend that abstract class and make a concrete class. Then it will be possible to actually create an instance of that concrete class. And of course when creating an instance the first thing that happens is that the inheritance structure is climbed all the way to the top, and a constructor for each super ... |
Abstract classes are meant to be subclassed, when an instance of a subclass is being created, the constructor of its superclass would be called firstly, so, you can think the superclass's constructor is a partial constructor of its subclasses. Constructors are to initialise instances, if you find you are repeately doing same things of initialization in the subclasses of a superclass, ... |
we know when we instantiate a class,via the constructors we get all of the instance variables of that class and those of superclass and those of superclass's superclass initialized,untill reach the root class Object. Since abstract class cannot be instantiated, in this case, constructors' functionalities is lost, so the only logical that seem to be reasonable for putting constructor in an ... |
Hello Bit od a design question here. I have an abstract class, called Person. A person has firstName, middleName, surname, all populated within the abstract class. Abstract methods get data of birth (allowing for subclass specific date formats etc) Person is expected to be sublassed to Applicants, Customers, Employees etc, all providing own concrete implementations. Is there any benefit to 'Person' ... |
As mentioned, a constructor is required in an abstract class, to allow the concrete subclass to be instantiated. However, not all possible access modifiers make sense for the constructor of an abstract class. The "public" modifier doesn't in practice offer any more access than "protected". The "private" modifier makes instantiation of a concrete subclass impossible; this only makes sense for a ... |
Hello, Q1 . Do we write a constructor for an abstract class in the real world ?? Since, we cannot create any object for the abstract class, so at one some point the above statement yields NO. But again we can call an abstract constructor from the subclass which extends this abstract class. Eg-- abstract class ( void fun(); } class ... |
|
Originally posted by debabrata das: why we can have/write constructor of an abstract class, if we can't instantiate. why it is so? It is unnecessary. It is not unnecessary. You can regard an abstract class as an incomplete class: some parts of it are implemented, but other parts are left for a concrete subclass to implement. When you create an instance ... |
Hello All, Today i was interviewed and didnt get selected, however i took this interview as a learning curve for myself. One of the tricker question being asked to me at the interview was "In an abstract class, do you have a constructor ? If yes, since we cannot instantiate Abstract class, wont that code be unreachable?" I got confused at ... |
|
an abstract class can contains attributes that will be inherited by all its sub classes for example. So, declaring a constructor enables you to initialise all these attributes in the super class, and not for each constructors of your sub classes. public class MyAbstractClass { protected int att1; protected String att2; public MyAbstractClass(int a1, String a2){ this.att1 = a1; this.att2 = ... |
Abstract class is designed to be subclassed. when you instantiate the subclass. the subclass constructor access the super class constructor to initialize the instance variable of super class which might be used into your subclass. for an example: abstract class Super { final int initialize; Super() { //needs to run to initialize initialize = 100; } } class Sub extends Super ... |
|
|
what will happen if you call abstract method inside a constructor ? Same as if you call any method anywhere. The method gets executed. In general, however, inside a constructor, it's not a good idea to call non-final methods on the object being constructed. If a child class overrides the method, that version will be invoked. A child should be able ... |
Hi All, Say I have an abstract class and subclass that extends this abstract class. As far as I understand the first statement in the subclass constructor has to be super. if the subclass constructor doesn't call a superclass constructor explicity then the default constructor of the superclass is invoked. But what if I don't define any default constructor in the ... |
|
Excellent point regarding prudent use of interfaces; thank you. If you read this, I'd appreciate any comment you have on how conservative programmers need to be in designing an interface. It seems to me that if I'm uncertain whether or not to include a certain member in the interface, I should omit it or include it elsewhere (perhaps in an interface ... |
|
I liked it because there are lots of ways to approach the problem. You can get an idea of how comfortable someone is with SQL versus Java. You can get an idea of whether they say one idea, then realize another might be better, etc. Some are clearly more or less efficient than others. And I was truly shocked how many ... |
The question is: "Is that really a valid statement? I accept that the constructor does nothing and I accept that it's an abstract class, but I have a hard time accepting that the constructor does nothing because it is an abstract class. Why couldn't a constructor of an abstract class do something to initialize the object, like getting a context?" |
|
|
|
|
|
|
can any body tell me, if we cannot instantiate an abstract class, then why there is a need of constructor for abstract class. one more thing is that an interface not have an internal state, but abstract class can have, so any relation here for solution of my problem? explain me how? |
|