Calling superclass from a subclass constructor in Java

Related searches

I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using

public crisps(String flavour, int quantity) {
    this.flavour = super.getFlavour();
    this.quantity = quantity;
}

In the superclass I have initialised the field with

private String flavour;

and I have an accessor method

public String getFlavour() {
    return flavour;
}

I am getting an error "flavour has private access in the superclass", but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?


What you should do:

Add a constructor to your super class:

public Superclass {
    public SuperClass(String flavour) {
       // super class constructor
       this.flavour = flavour;
    }
}

In the Crisps class:

public Crisps(String flavour, int quantity) {
    super(flavour); // send flavour to the super class constructor
    this.quantity = quantity;
}

 

Comments

Some comments to your question:

"In the superclass I have initialised the field with "

private String flavour;

This is not an initialization, it is a declaration. An initialization is when you set a value.

"I am getting an error " flavour has private access in the superclass" but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?"

When you call a accessor (aka getter), it is ok - depends on the getter visibility. The problem in you code is the:

this.flavour = 

because flavour is not a field declared on Crisps class, but on the supper class, so you can't do a direct access like that. you should use my suggestion or declare a setter on the super class:

public void setFlavour(String flavour) {
    this.flavour = flavour;
}

Then you can use it on the child class:

public Crisps(String flavour, int quantity) {
    this.quantity = quantity;
    super.setFlavour(flavour);
}

Using the Keyword super (The Java™ Tutorials > Learning the Java , 9.2 Calling the Superclass Constructor. A subclass can have its own private data members, so a subclass can also have its own constructors. The constructors of� How to call the constructor of a superclass from a constructor in java? Java 8 Object Oriented Programming Programming Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.


flavour is private. Although you're reading it from the public method, you're assigning it to a private field, and you likely didn't declare it in this class.

You could set flavour to protected in the parent class or define a setter for it

Ultimately your code doesn't really make sense though. Even if it did compile, it would be more or less: flavour = flavour. Perhaps you should rethink what you're trying to do a little bit

I think you may need a tighter grasp on Java and Object Oriented Programming.

http://docs.oracle.com/javase/tutorial/java/concepts/

You should start here.

Calling the Super class Constructor, We could access maxSpeed of base class in subclass using super a superclass constructor, the Java compiler automatically inserts a call to� Thus, when a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass. To call a superclass constructor the super keyword is used. The following example programs demonstrate use of super keyword. (Rectangle.java) /** * This class holds data of a Rectangle.


public crisps(String flavour, int quantity)
{
    super(flavour);
    this.quantity = quantity;
}

This should work as see Docs

Super Keyword in Java, What you should do: Add a constructor to your super class: public Superclass { public SuperClass(String flavour) { // super class constructor� The fact that the subclass constructor has parameters of the superclass type is basically irrelevant as far as Java is concerned - it's just a coincidence. You need to follow all the normal rules of Java, which in this case means chaining to a superclass constructor.


make

    private String flavour;

public,otherwise your subclasses won't have access to this String. Your superclass doesn't know about existence of any subclass. According to Java documentation, "private" makes any variable and method available within that class,where private variable or method was declared, no any class has access to it,even subclasses. Once you chance your access modifier, you won't get any errors.

Calling superclass from a subclass constructor in Java, Whenever you inherit/extend a class, a copy of superclass's members is created in the subclass object and thus, using the subclass object you� Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is. super (); or: super (parameter list); With super (), the superclass no-argument constructor is called.


How to call the constructor of a superclass from a constructor in java?, To invoke the superclass constructor, our constructor calls super(). super is a reserved word in Java. One of its uses is to invoke the constructor method of a� In constructor, you need to call super() as a first statement (if you call it explicitly). On regular methods you call it wherever you want depending on your app logic needs. For example at the beginning if you want to add extra steps after call or at the end if you add extra check. – Michał Šrajer Jul 1 '17 at 14:00


Subclasses and Inheritance (Java in a Nutshell), There is an implicit call to super () with no arguments for all classes that have a parent - which is every user defined class in Java - so calling it explicitly is usually not required. However, you may use the call to super () with arguments if the parent's constructor takes parameters, and you wish to specify them.


To make sure the Person is properly constructed, the compiler adds an implicit call to super() in the Employee constructor: class Employee extends Person { Employee(int id) { super(); // implicitly added by the compiler.