Abstract classes: Why do they need constructors?

As we cannot instantiate an abstract class. Why does an abstract class need a constructor?.can anyone please help me out..Thnq in advance
OBELISK

import java.util.*;
import static java.lang.System.out;
interface LineItem{
    public int getCost();
abstract class AbstractLineItem implements LineItem{
  protected String name;
  AbstractLineItem(String name){
     this.name = name;
  public String toString(){
    return this.name+" "+getCost();
  public abstract int getCost();
class ProductItem extends AbstractLineItem{
   private int quantity, peritem;
   ProductItem(String name, int quantity, int peritem){
     super(quantity+" "+name);
     this.quantity = quantity;
     this.peritem = peritem;
   public int getCost(){
     return this.quantity*this.peritem;
class FlatFeeItem extends AbstractLineItem{
   private int fee;
   FlatFeeItem(String name, int fee){
     super(name);
     this.fee = fee;
   public int getCost(){
     return this.fee;
public class Test{
  public static void main(String ... args){
    List<LineItem> items = new ArrayList<LineItem>();
    items.add(new ProductItem("Banana",6,1));
    items.add(new FlatFeeItem("Some boring thing or another",35));
    items.add(new ProductItem("Kiwi",3,3));
    for(LineItem item : items)
      out.println(item);
}

Similar Messages

  • I called apple tech support cause my iphone 5 charger just stopped working. Aplle tech support said they can send me a new charger but why do they need credit card information if i have only had the the charger for 2 months and i have 1 year warranty?

    I called apple support because my iphone 5 charger stopped working and they said they would send me a new charger but why do they need credit card information if every apple product has 1 year warranty?

    Hi CryyCorr33
    Sound like they did what is called an express replacment. What apple does is they put a hold on your Credit Card for the amount of the cord because they are sending you the cord first. Then, once you receive the new cord in about 2-3 days, you'll put the defective cord in the box and ship it back out and when they receieve the defective one, they'll be able to remove that hold from you card.
    I believe the reason behind the hold on your card is incase you don't return your defective one to them so they can find out the cause of the issue.
    An Apple Retail Store could swap them out for you in store incase you happen to be near one. This requires no hold.
    Hope this helps

  • Why Do We Need Constructor With Arguments?

    I understand that constructor is used to create instances.
    We can have constructors with 0 argument or with one/multiple arguments. Why do we need a contructor with arguments? What is the purpose of doing it?
    Thank you for your help.

    There are three general ways to provide constructors, like you said:
    1) Default constructor (no arguments)
    2) One or more constructors with arguments
    3) One or more constructors with arguments as well as a default constructor
    From a design standpoint, the idea is to give as much information needed for constructing an object up front. Look at the standard Java API for some examples (and because I couldn't find a standard class with just a default constructor, one made up):
    public class Foo {
      private long createTime;
      public Foo() {
        createTime = System.currentTimeMillis();
      public String toString() {
        return "This object was created at " + createTime;
    }This code has no reason to take any arguments. It does all construction on its own and already has all the information it needs. On the other hand, look at BufferedReader. There's no possible way this object can ever be constructed without an argument. What's it buffering? It doesn't make sense to ever instantiate new BufferedReader(). It'd be useless. So BufferedReader has no default (no-arg) constructor.
    Sometimes (very often in Java) you want to do both. Let something be instantiated with no arguments, but also provide the option of creating something with more specific details. A good example is ArrayList, which has three constructors:
    public ArrayList() --> Construct an empty ArrayList with the default initial capacity;
    public ArrayList(Collection c) --> Construct an ArrayList populated with the contents of another collection;
    public ArrayList(int capacity) --> Construct an empty ArrayList with a specific initial capacity.
    Sometimes it makes sense just to use the default. Sometimes you want more control over the behavior. Those are the times to use multiple constructors.
    Hope this helps!

  • HT201303 If an PP is free,why do they need your billing info if it's free? It should download automatically if it's free.

    If an App is free,why do you need my billing info if it's free?
    It should download automatically if it's free.

    No one here can help you. You'll have to contact iTunes support to see what's wrong with your account & how to fix it:
    http://www.apple.com/support/itunes/

  • ODAC, ODT why do they need server side install?

    I am a DBA who was recently asked to do some research on Oracle development tools for Visual Studio .Net 2008.
    I downloaded the ODAC and ODT (ODTwithODAC1020221.exe) from OTN. I am under the impression that these development tools are installed on the client workstation. Is this correct? If yes, then I cannot explain why the installation of ODAC and ODT fails immediately complaining that it needs Oracle development extension for .Net installed in the Oracle home.
    I go to the Oracle development extensions for .Net and it is a server side install. Can someone please tell me what exactly needs to be done to get the Oracle development tools for VS .Net working?
    I have read the documentation and it does not say anything about a server side install. Sorry if I am not very clear. This is unknown territory for me.
    Database version: 10.2.0.3
    Visual Studio .Net 2008
    Oracle client version 10.2.0.3
    Thanks

    Thanks for the reply. As I said, this is totally new to me so can you please point me to a download link?
    As far as I know, I tried to install ODT on the client. I tried both with 10g and 11g. The installation stop at the third screen complaining it cannot find Oracle Database Extension for .Net in the Oracle home where I am trying to install. I am trying to install in c:\oracle\product\10.2.0\client_1.
    I downloaded from:
    http://www.oracle.com/technology/software/tech/windows/odpnet/index.html
    I tried the following downloads:
    ODAC 11.1.0.6.20 with Oracle Developer Tools for Visual Studio
    Oracle Developer Tools for Visual Studio .NET with ODAC 10.2.0.2.21
    Both the downloads clearly say that these are ODT but they fail to install. Please help me understand what do I need to download.
    Thanks

  • SAP_HR & EA-HR:  why do they need to be synchronized?

    Over the summer, we upgraded to ECC 6.0 SR3.  We included numerous support packages, including SAP_HR ...43.  However, not knowing what EA-HR was, we did not apply any support packages to EA-HR.  Now, we need to plan for year-end, and the HRSP link discusses SAP_HR in tandem with EA-HR.
    Although several threads in this forum mention the following link:
    http://help.sap.com/saphelp_erp60/helpdata/EN/44/4541e0bc511193e10000000a155369/frameset.htm
    as an explanation of this topic, I must be saphelp-impaired, because I just don't see exactly what EA-HR is used for when going to this link.  However, there must be SOME significance to EA-HR in particular, if it is mentioned in tandem with SAP_HR.
    So, what I really want to know is whether I need to keep EA-HR up-to-date with support packages when I update SAP_HR with support packages.  Naturally, I would like to know why (and what the impact is if I don't keep EA-HR up-to-date).
       Tom K.

    So, is there a SAP Note or some SAP guide/document that spells out what (sub)components are used for what functions?  Do the EA-xxx components merely add functionality in the portal/web environment?  When we were on 4.6C, we had perhaps a dozen components (e.g., SAP_BASIS, SAP_ABA, SAP_APPL, SAP_HR, IS-H).  Now, we have over 70 (sub)components, with all of the old ones, plus new NetWeaver components (e.g., SAP_BW, SAP_AP, FINBASIS), plus new mystery components (e.g., EA-IPPE, EA-APPL, EA-HR, EA-RETAIL), along with a myriad of HR subcomponents (e.g., SAP_HRCFR, SAP_HRCFI, SAP_HRCDK).  So, am I expected to keep all 70+ components and subcomponents updated every time that I update support packages?  With no disrespect to other industries, why would I care if I keep IS-OIL up-to-date when we don't use it?  Similarly, an oil company would not care about updating IS-H, unless they ran a hospital!
    I'm just not catching on to the strategy.  I'm guessing that all of us now running ECC 6.0 have been bloated with programs that we will never use.  Are we to spend our time and energy and disk space updating scores of components that we will never be in our business plan?  Is there a document with the SAP strategy for this?
    Please forgive my frustration.  I have yet to see how this has simplified anything.

  • Why do they need credit card security code to download load a free app

    I have the iBook app and have been notified of an update, when I selected install I was asked fro my credit card security number, which after the recent press were hundreds of apple members personnel details had been stolen this is a bit naughty until they prove they are trust worthy, I feel that as they already have the information from when I purchased the iPad and many other apps a free upgrade does not need such high security. Unless I'm wrong, please tell me why?

    I've missed the press reports about stolen details but I have heard of fraudulent purchases being made on accounts where the Apple ID & Password have become compromised. Not so long ago I found myself having to a one-time revalidation of my credit card and also tighten up my password. You might also be prompted for details if your credit card on file expires. I'm sure you would prefer that you and you alone got access to your account so I would suggest you perform the account validation and then remove your credit card from file - i.e. switch your payment method to none. You should be able to obtain free updates thereafter, at least until the next major tweak to security demands we all revalidate ourselves.
    (Actually, I think you might be asked for some additional info. each time you use a new device to try to access your account which again would be a positive anti-fraud strategy)
    tt2

  • Name change on my account... Why do they need my SSN?

    I called to change the name on my account to my married name.  Easy enough, right?  After one hour and three attempts, I gave up.  The rep confirmed my date of birth.  Connected to a tech person who asked my date of birth, ssn, and cell phone number.  Already unsure about giving out my ssn, I had to repeat it several times. The tech person said that all my info was wrong, and that the rep would have to start over with a new order.  So, we did everything all over again.  Connected to the same tech, repeated all my info again several times, everything was wrong again!  So, we try a third time.  Connect with a different tech, repeated my info again including my ssn three times!  Again it was wrong.  By this time I just got frustrated and hung up.  Now my concern is that my ssn is out there, or it's attached to somebody else's account.  Should I be worried???

    Hi DMP11,
    Your issue has been escalated to a Verizon agent. Before the agent can begin assisting you, they will need to collect further information from you. Please go to your profile page for the forum and look at the top of the middle column where you will find an area titled "My Support Cases". You can reach your profile page by clicking on your name beside your post, or at the top left of this page underneath the title of the board.
    Under "My Support Cases" you will find a link to the private board where you and the agent may exchange information. The title of your post is the link. This should be checked on a frequent basis, as the agent may be waiting for information from you before they can proceed with any actions. To ensure you know when they have responded to you, at the top of your support case there is a drop down menu for support case options. Open that and choose "subscribe". Please keep all correspondence regarding your issue in the private support portal.

  • Abstract class, a question

    i have an abstract class called shape with default constructor (double y1, double y2);
    with an abstract method area.
    now i have a circle class which extends it, but i want its constructor to have ONE radius (as its pi radius*radius)
    at the moment this is how my circle looks like, and as you can see that there is really no need for second radius how can i modify circle class without modifying shape class?
    class circle extends Shape{
    circle(double radius, double radius1) {
         super(radius, radius);
    double area() {
    return (3.14*x1*x1);
    }

    the answer from the test class in above case will be
    0!Yes, because 3.14 * 0 * 0 is 0.
    but if i change the x3 above to x1, it would work, You honestly don't see why? It's staring you right in the face. What does area() use? How does that field get set?
    the
    reason i decided to use x3 is so that ppl dont get
    confuse! Here's what's confusing:
    * field names x1, x2, x3. What do those mean? They sound like 3 x coordinates.
    * two fields that are used by certain subclasses, but not others, and a third field that's used only by the subclasses that don't use the first two.
    * that third field meaning "the single value that applies to what both of the other two fields mean" instead of just setting both fields to the same value.
    * having any of those fields in there at all in the first place when they're not used by the abstract class and not used consistently by subclasses.
    but shouldnt it work because i have done the
    same thing i did to x1 and x2??

  • Why aren't inherited constructors returned?

    Let's say I have an abstract class that defines a public constructor, and a different class that extends this abstract class. I'd like to use reflection to instantiate an object of the subclass using the constructor of the parent class. If this is possible to do in code, why is it not possible to do at run-time? (Or am I hopefully missing something, and there is in fact a way to do this?) If Class.getMethods() will return inherited methods, why won't Class.getConstructors() return inherited constructors?

    Let's say I have an abstract class that defines a
    public constructor, and a different class that extends
    this abstract class. I'd like to use reflection to
    instantiate an object of the subclass using the
    constructor of the parent class. If this is possible
    to do in code, It's not possible to do it "in code" (by which I assume you mean the "normal" way, without reflection). As the other poster said, constructors are not inherited.
    If you're specifically interested in reflection, the following may not be particularly applicable to your current problem. However, you'll need to understand these rules anyway...
    Here are the rules for constructors--"ctors" because I'm lazy. Also, because I'm lazy, "super(...)" and "this(...)" mean any super or this call, regardless of how many args it takes, including those that take no args.
    1) Every class has at least one ctor.
    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...} if you want one.
    1.3) Constructors are not inherited.
    2) The first statement in the body of any ctor is either a call to a superclass ctor super(...) or a call to another ctor of this class this(...) 2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super() as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

  • Abstract class costructor

    I came to know that even abstract classes have constructors,
    which get called when concrete subclass is instantiated.
    In order to create an instance of a class we need a constructor so if we have constructor of abstract classes why can't we create object of abstract classes?

    hi,
    Constructors do not create the instance of any class.They are used to initialize the instance variables of the class.In other words ctor are a part while the class is being instantiated.....In case of an abstract class ,the subclass which extend this parent class will be extending the methods and variables in the parent class..so when the child class is instantiated the call to ctor of parent class is either made implicitly (as super()) or by explicitly defining the ctor of that class.
    So in order to inherit the variables of the parent class they are initialized by the constructor of the parent class.....

  • A Singleton variable in an Abstract Class

    Hello there people
    I have a quick question. I have made an abstract class in which I want to put an identifier (ID) int to be incremented whenever I make any of the concrete class instances that extend this abstract class.
    whould that declairation be as follows?:
    public static int IDAdditionally, I have a quick question about abstract constructors. If constructors are defined in the concrete classes, are their declairations needed in the abstract class?
    and finally, if I wanted to use the int ID (as mentioned above) then would I have to use constructor from the abstract class?
    thanks in advance for the help

    I think the OP would want a constructor in the abstract class to update that variable. Otherwise, he'd have to remember to update it in the constructor for all concrete classes. But, you don't declare constructors for the concrete class in the abstract class. You only declare constructors for the abstract class in the abstract class.

  • Question on abstract classes

    I just need to make sure that I have a concept right. I know that interface declarations can have fields, but they must be initialized and constant. In an abstract class though, can they also have fields, since it can acept both abstract and concrete methods? If so, do they have the same requirements as an interface? Thanks!

    Yes, abstract classes can have fields.
    No, it does not have the same requirements as an interface.
    It is a class, and so can do everything a concrete class can do, except be instantiated.
    It depends on the visibility of the field you define.
    Defining a field private means that it can't even be used by its subclasses. Sometimes you might want that, sometimes not.

  • HT2534 why do you need my cc?????

    The article on itunes is incorrect.  I do not get an option to click "none" when creating an apple ID...I only want to get some artwork, not planning to purchase anything, so why do they need my cc info?

    dablenda wrote:
    I think this is ridiculous...why would I put that information out there if I am not buying anything?
    Setting up an account without a credit card does appear to be slightly "involved", but since you have stated that you don't intend to buy anything and that you simply want to find the artwork, perhaps you should concentrate on getting the artwork using the method I suggested.
    I never get artwork for any of my albums from the iTunes Store. Since the Store only has artwork for the albums it sells, several of my CDs will not be found in the Store anyway.
    There is another method too if you are using your CDs as the source for your music: if you have a scanner bed, or a printer with a scanner facility, scan the CD cover and paste that into your artwork box.
    By the way, note that tt2's post states that you cannot change an existing AppleID to the "no credit card" option. So if you simply tried to change your existing id, then that could be the problem.

  • Abstract class method polymorphically using constructors?

    how can i have a method defined in an abstract superclass call a constructor of the actual class running the method?
    abstract class A {
    public List getMultple() {
    List l = new ArrayList();
    for (short i=0;i<4;i++) {
    l.add(this());//<obviously this breaks
    return l
    or something like that. A won't run this method, but its children will...and they can call their constructors, but what do i put here to do that?
    i've tried a call back. an abstract method getOne() in the superclass forces each child to define that method and in each of those i return the results of a constructor. that works fine.
    the problem is i want to abstract this method out of each of these children classes cause its the exact same in each one, just using a different constructor to get multiple of each in a list. so if i use this callback method, then i am not saving the number of methods in each class, so why bother at all?
    any ideas?

    I still say you are coming at it from the wrong angle. A super class is not the way to go. What you are doing sounds like something very similar to something I did not too long ago.
    My requirement was that I had tab delimited text files filed with data that I had to parse. Each line would be used to instantiate one object, so a particular file could be used to instantiate, for example, a thousand objects of the same class. There were different types of files corresponding to different classes to instantiate instances of.
    Here is the design I ended up using.
    An object of class DataTextFileReader is instantiated to parse the text file and generate objects. It includes code for going line by line, handling bad lines and generating objects and reports. The constructor:
    public DataTextFileReader(File inputFile, LineParser<T> theLineParser)LineParser is an interface with one method:
    public T read(String line);When you call a load() method of the DataTextFileReader, it does its thing with the aid of the LineParser's read method, to which each line is passed, and stores the generated objects in an ArrayList. This can be returned by using another method. There are other methods for getting the reports, etc.
    Obviously, the LineParser chosen needs to have code appropriate for parsing the lines in question, so you have to choose and instantiate the right one.
    I find this design to work well. I arrived at it after spending hours giving myself headaches trying to come up with a design where there was a superclass roughly equivalent to the DataTextFileReader mentioned above, and classes extending this that fulfilled the duty of the LineParsers mentioned above... rather like what you are trying to do now.
    I did not care for the solution at first because it did not give me the "Ah, I am clever!" sensation I was expecting when I finally cracked the problem using inheritance, but I quickly came to think that it was much better OOD anyway.
    The LineParsers mentioned above are essentially embodiments of the Factory pattern, and I would recommend you do something similar in your case. Obviously your "constructors" all have to be different, so you should make a separate class for each of those. Then you can put the code that performs the query and loops to create loads of objects in another class called something like DatabaseDepopulator, using appropriate generics as in my example. Really it is the same problem, now that I look at it.
    This will also result in better separation of concepts, if you ask me. Why should the class constructor know how to parse a database result query, much less perform the query? It has nothing to do with databases (I presume). That is the job of an interpreter object.
    As a final note, remember... 95% of the time you feel like the language won't let you do what you want, it is because you shouldn't anyway.
    Drake

Maybe you are looking for

  • Viewing gallery on older Mac

    My brother and sister-in-law have an older iMac, probably running an earlier version of OS X and iPhoto. We published a web gallery of some of our photos, but they were having trouble accessing it. Are there system requirements for viewing the photo

  • Connecting a Dell B22 FEX to Nexus 6001 and storage

    Hi Folks, We have a setup where we are running a Dell B22 FEX in Blade enviornment and want to connect the B22 FEX to a cisco Nexus 6001 switch. As per NX-OS release note, B22 FEX and 6001 Nexus connectivity is supported. Now after connecting to Nexu

  • Oracle RAC nodeapp 启动报错-vip:IP:192.168.2.200 is already up in the network

    Linux redhat 5 Oracle RAC 10.2.0.5环境 启动nodeapp报错如下: [oracle@rac1 ~]$ srvctl start nodeapps -n rac1 CRS-0210: Could not find resource ora.rac1.gsd. rac1:ora.rac1.vip:IP:192.168.2.200 is already up in the network (host=rac1) rac1:ora.rac1.vip:IP:192.16

  • How to iterate the opened pdf document?

    How to iterate the opened pdf document?

  • Unable to open photoshop 11 after installing

    I installed my purchased copy of PhotoShop Elements 11 on my work computer (PC unfortunately running VISTA OS).  When I open the program I get a license agreement page for Organizer.  I click Accept and it opens the same page.  It continues to open t