1
public class Shape {
     public static void main (String[] args) {
    

        Circle c = new Circle(5);
        System.out.println (c.getArea());
        c.setColour("Green");
        System.out.println (c.getColour());

  
    }
}

    interface Colour {
        String getColour();
        void setColour (String colour);
    }

    abstract class Shapes implements Colour {
        abstract double getArea();
        private String colour = "Red";

    public String getColour() {
        return colour;
    }
    public void setColour(String colour) {
        this.colour = colour;
    }

}
class Circle extends Shapes {
    private int radius;

    public Circle(int radius) {
    this.radius = radius;
}



    public double getArea() {
        return Math.PI * radius * radius;
    }
}

I have attempted a Swift Solution the code is below

import Cocoa

protocol Colour {
    var colour: String { get set }

    func getColour() -> (String)
    func setColour(colour: String)

}

class Shape: Colour {
    var colour: String = "Red"

    init(colour: String) {
        self.colour = colour
    }

    func getColour() -> (String) {
        return colour
    }

    func setColour(colour: String) {
        self.colour = colour
    }

    func getArea() -> Double {
        return 0.0
    }



}

class Circle: Shape {
    let radius: Double


    init(radius: Double) {
        self.radius = radius
    }

    override func getArea() -> Double {
        return radius * radius * Double.pi
    }


   }

I have written code to create an interface 'colour', a super class 'shape' and a subclass 'Circle'. The aim is to learn more about inheritance and multiple inheritance as a new programmer. The java code works as intended.

But i am getting an error " 'super.init' isn't called on all paths before returning from initializer" with the above Swift code. As I am newer to swift and multiple inheritance I am not sure how to correct this error.

How I can correct this?

2 Answers 2

2

Neither Swift nor Java support multiple inheritance.

C++ does, and a few others.

In Swift you can use protocols to accomplish very similar things, but conforming to a protcol is not the same thing as inheriting from a parent class.

Your problem is that your initializer needs to call its parent class' initializer:

init(radius: Double, color: String) {
    self.radius = radius
    super.init(color: color)
}

(Circle needs to call super.init, or in this case, init(colour:) from its shape superclass.)

Note that your circle shape's initializer should probably take a color as well as a radius:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I have done this but am now getting 2 more errors "Property 'self.radius' not initialized at super.init call" and "Immutable value 'self.radius' may only be initialized once". The only thing I have changed is my init function which is now exactly the same as the code you posed. Thanks
Sorry, I got those 2 lines in the wrong order. You should set your instance variables before calling self.init. See the edit to my answer.
If my answer solved your problem you should accept it.
2

Your error is related to the initialization of superclasses. Whenever you inherit the superclass, to initialize it you must write super.init() on the first line of the Circle class's initializer.

//Inside of Circle class

 init(radius: Double) {
//I've initialized shape using the color Red, but you can use any string.
        super.init(color: "Red")
        self.radius = radius
    }

1 Comment

You hit submit faster than me. You win.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.