0

How to define class in Som languages?

class ClassName(){ private type data1 =default value private type data2 = default value public type setData1(type dataToSet){data1 = data1ToSet} public type getData(){return data1} }; Is it allowed like this one: class ClassName(type data1, type data2) { }; I havent seen before this model of declaration except in Python and Kotlin. Please help me.

24th May 2023, 12:54 AM
Oliver Pasaribu
8 Answers
+ 4
Oliver Pasaribu This doesn't work in Python. Where did you see it? Can you share examples? Also, why would a class take data arguments?
24th May 2023, 3:18 AM
Emerson Prado
Emerson Prado - avatar
+ 2
The syntax for declaring classes, is different for each language. Usually, the class header contains information of inheritance, about the parent class and interfaces. When we do not specify a parent class, there is still an implicit parent, for example in Python it's "object", in Kotlin: "Any", in Java: "Object". This already contains some default method implementation like how to represent an instance of the class as string. Your question is not clear. Are you trying to compare OOP in different languages? Or are you interested in a particular piece of the syntax? The essence of OOP is the same everywhere: inheritance, encapsulation, polymorphism. I recommend to focus on one particular language and try to understand the core concept once. Then you will be in a better position to compare different languages.
24th May 2023, 4:49 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The arguments after the class name, is actually syntax sugar for the primary constructor. You can also overload additional constructors. And in case of inheritance, some properties can be delegated to the constructor of the superclass. Kotlin doc: https://kotlinlang.org/docs/classes.html Also if you check the Sololearn Kotlin lesson about OOP inheritance, I have put some more detailed explanation there how this works. https://code.sololearn.com/cf78kUN8wnaV/?ref=app
24th May 2023, 7:30 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The Kotlin official documentation is very good. Go ahead and look at the link I gave before, and this one too: https://kotlinlang.org/docs/inheritance.html
24th May 2023, 7:47 AM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa Yes. Oop is inheritance and polymorphism in wahatever languages that support oop. That's the problem. I understandwell in Java or C++, and still learn continually in Python. I find this syntax in kotlin lesson at sololearn at part constructor and multiple or overloaded constructor. class className(var param1:type=default1, var param2:type=default2,...var param-n:type=default) { } main(){ var b = className(param1Value, param2Value,...param_NValue) println(param1) println(param2) . . . println(param_N).
24th May 2023, 7:09 AM
Oliver Pasaribu
0
This is in Kotlin. I forget to tell you all.
24th May 2023, 7:19 AM
Oliver Pasaribu
0
This is the example of code written in Kotlin. Please explain is it allowed include parameter or argument when declaring class? This pass sololearn test Cases. https://code.sololearn.com/c60cLt3KxpPf/?ref=app
24th May 2023, 7:21 AM
Oliver Pasaribu
0
This pretty much like Java. This is not explained in sololearn. Tibor Santa, do you know the Kotlin book that explain this clearly?
24th May 2023, 7:44 AM
Oliver Pasaribu