-7

Edit2: I learn Java first, and I appreciate the value of Java. Then I learn swift, people said that it's about 'good programming practice' so I should put the superclass constructor at last. This doesn't buy me. I need a real reason, a real comparison. So I what I want to ask as stated below.


In swift: Constructor of superclass should be called last

In Java: Constructor of superclass should be called first

Originally I trust Java but now in swift there is a different rule. I want to know the pros/cons of each.

I really want to ask is: In the first link, they pointed out the problem when the constructor be called last.

So, this means that in Java the problem will occur, since in Java the rule is completely reverse. So how do people using Java solve the problem?

Community
  • 1
  • 1
Kindred
  • 1,183
  • 13
  • 35
  • 2
    Your rewording of the question helped; but even then, the question is borderline of being too broad. Now it more reads: "explain stuff to me". But thing is: this is a lot of stuff. Which is documented a zillion times out there. I will leave my answer for now; but I think you will not have much luck; even with your rephrased question. – GhostCat Apr 24 '17 at 08:39
  • 1
    *"I want to know the pros/cons of each."* I don't get that request. The first link tells you why you could/should do that in Swift and you have no other choice in Java than calling the super constructor first. So what is unclear? What is the point of this question? – Tom Apr 24 '17 at 08:48
  • 1
    Also when you ask try to articulate your exact problem a bit better. Besides being opinion based and too broad it is not exactly clear what you don't understand here. Different languages do things differently, like @GhostCat said understanding the differences is crucial for deciding what is better and why is something working the way it does. My best guess is that you have missed the point in Swift where compiler protects non-optional properties that must have value and in order to do that enforces their initialization before you can do anything else including calling super. – Dalija Prasnikar Apr 24 '17 at 10:04
  • Out of curiosity: what is the reason to un-accept an answer on a 3 year old answer? – GhostCat Nov 19 '20 at 07:56

1 Answers1

4

This answer invites for opinions, but the real answer is: you are asking the wrong question.

In programming, you use the tools you know to help you do the job you have to do.

Each and any tool has its own properties, pros and cons. As a professional; you simply know about these things; and you make sure you apply your knowledge accordingly.

Thus: it is a waste of time to ask "what is better". You might ask: what are the specific advantages; or problems with a certain approach. And coming from the Java side, the nice thing about running the constructor first is (for example): you can be assured that all these constructor calls took place before anything else happens.

In other words: the only thing that you can do there: study those two links (and probably additional tutorials, language specs, ... ) in detail to understand how those two different languages go about initializing new objects. Understand the differences; and then decide for yourself what matters to you in your context.

GhostCat
  • 133,361
  • 24
  • 165
  • 234