Help coderanch get a
new server
by contributing to the fundraiser

aj Haasan

Greenhorn
+ Follow
since Mar 23, 2023
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
13
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by aj Haasan

I'm going through some concepts in a book and author has explanation through below example:



f an abstract class cannot be instantiated, the how is that this class has constructors? Why are constructors here and how are they used if not to create objects?

11 months ago
Hello,

If a class has abstract properties or functions, then class MUST be abstract. Isn't it?

But Enum class allowing abstract properties/function that overidden in each enum instance. How is this possible without making enum abstract?

11 months ago
I figured it out. Thank you. Below is my modified code. Also by creating just plain kotlin file instead of class, in the ide, sometimes, the file is not treated as kotlin file and after renaming the file with extension of .kt solved some issues for me.

Below is my modified code.


1 year ago
I'm reading Head first Kotlin and I got fully confused reading through 9 chapters. So I started from beginning again. Below is the program I wrote to rock, paper, scissors game and its not compiling.



My idea is to separate the Game logic and user implement as start to program.

In the same file, the rockpaperscissors is unable o be accessed. Please point to mistake/s and help me compile this program
1 year ago
I'm reading book, Head first kotlin. Book says as below

"The setter updates the value of the weight property by means of the
field identifier. field refers to the property’s backing field, which
you can think of as being a reference to the underlying value of the
property. Using field in your getters and setters in place of the
property name is important, as it stops you getting stuck in an endless
loop. When the following setter code runs, for example, the system tries
to update the weight property, which results in the setter being called
again... and again... and again:"





Question 1: I couldn't understand why it gets stuck in a loop. Could you please help understand this?



Question 2: in the above code, I get error for variable3 saying it needs to be initialized. But not for variable2 which is declared as val type. Why initialization error different incase of "var" but not "val"?
Question3: Also in same code above, for var variable3, when I add lateInit var variable3, it says,
                   'lateinit' modifier is not allowed on properties of primitive types
                  'lateinit' modifier is not allowed on properties with a custom getter or setter
In kotlin, I think there are no primitive types. The data types are all objects. Why it's saying not allowed on properties of primitive types?
Why lateinit is not allowed when there are custom getter, setter?
1 year ago

Campbell Ritchie wrote:There is a problem with many kinds of inheritance; if the method/function isn't to be found in the type the variable is declared with, there is no guarantee that it will be available in all objects. What about a starfish, which definitely roams in seach of oystes to eat, but never makes any noise. In order to get the makeNoise() method, you might want to cast the object to Animal, which does have that method, but that is dubious design and risks errors if the cast doesn't work. That problem occurs in several languages.



Thank you for replying. I'm get my head around this. Its very confusing. Animal is implementing Roamable interface which means Animal object inherits everything of Roamable.

Hippo inherits Animal which means Hippo object will have access to abstract functions/properties of Animal and also Roamable.

According to below code, from what book says, Hippo object assigned to roamable type. When we roamable.makeNoise(), it wont call inherited Animal's function as it's roamable type that doesn't know what makeNoise() function is. Say for understanding changing Roamable to Abstract.



Now after change we have:



Now, if I write roamable.makeNoise(), will it be able to access Animal's makeNoise() function?


1 year ago

Junilu Lacar wrote:Hippo() creates a Hippo object which is an implementation of Roamable via its superclass, Animal. Specifically, it's a Hippo object but conceptually, it's also a Roamable object.

Wolf() creates a Wolf object which is an implementation of Roamable via its superclass, Canine. Specifically, it's Wolf object but conceptually, it's also a Roamable object.

I think you're getting hung up on the semantics of the shortcut statement "creates a Roamable object" -- all that really means is "creates an object that implements the Roamable interface".



Thank you for replying. English is not my first language. Looks like author may have called Roamable object as shortcut and I may not got it right. However, everywhere I search for topics on this, authors when mentioned object, it's usually derived from class instantiation which calls class's constructor to create object. So when they say Roamable object, I understood as an object created from Roamable interface.
1 year ago
Hello Junilu Lacar,

Thank you for reply, Please see the attached image. In that author specifically saying  creating Roamable object. I think there cannot be an object from Roamable interface.

1 year ago
Thank you for reply. Because only subclasses that can be instantiated can access parent abstract super class, we dont need worry about calling on parent anstract object. correct? Which mean an object is never created from abstract parent class as abstract class cant be instatiated? Is my understanding correct?
1 year ago
I'm reading head first kotlin and am totally lost when author started explanation saying roamable objects where Roamable is an interface. Below I pasted only class/interface signature. They have body which I didnt post as they are not needed for my question.


Author did this:




Interface doesn't have constructor and with that I dont think objects can be created out of interface. Why is author mentioning Roamable oble objects? Does this mean, there can interface objects without constructor in interface?
1 year ago
I studied in kotlin inheritance, sub classes with inherit from super class, the instance of super class is created.
In abstraction, the instantiation can be avoided when "abstract" keyword is used instead of "open". While abstract properties/functions in abstract class doesn't have to be instatiated, an abstract class can have non abstract properties/functions with body.

Question 1: If class is not instiantiated, how are the non-abstract properties/functions of abstract class are valid / accessed?
Question 2: If abstract class cannot be instantiated, why does it even need a constructor? Why not remove constructor like interface doesn't have?
1 year ago

Mike Simmons wrote:Inside the setter

You have actually declared a new "num" variable, which is the argument of the setter method.  (As opposed to "field" which refers to the value of the actual field.). Inside the setter, you can refer to "num", and it's the value passed as a argument to the setter method.

Inside the getter, you have not declared a new "num" variable.  Which is as it should be - there's no argument to a getter method, and therefore no need to give it a name.  But, can "num" refer to anything else in the class?  Well, it could have referred to the "num" declared in the constructor... but since you didn't use "val" or "var", that value is not visible in the rest of the class.  So without that, there is no "num" in the class that you could be referring to inside the getter.



Thank you very much for explanation. I'm reading Head First Kotlin and I think book should have give more explanation on this to avoid confusion. I have another question.

Book says as below

"The setter updates the value of the weight property by means of the
field identifier. field refers to the property’s backing field, which
you can think of as being a reference to the underlying value of the
property. Using field in your getters and setters in place of the
property name is important, as it stops you getting stuck in an endless
loop. When the following setter code runs, for example, the system tries
to update the weight property, which results in the setter being called
again... and again... and again:"



Question 1: I couldn't understand why it gets stuck in a loop. Could you please help understand this?



Question 2: in the above code, I get error for variable3 saying it needs to be initialized. But not for variable2 which is declared as val type. Why initialization error different incase of "var" but not "val"?
Question3: Also in same code above, for var variable3, when I add lateInit var variable3, it says,
                    'lateinit' modifier is not allowed on properties of primitive types
                   'lateinit' modifier is not allowed on properties with a custom getter or setter
In kotlin, I think there are no primitive types. The data types are all objects. Why it's saying not allowed on properties of primitive types?
Why lateinit is not allowed when there are custom getter, setter?
1 year ago

Mike Simmons wrote:

aj Haasan wrote:(ii) num not recognized. (We are having this as parameter in class input values. Why is it not being recognized?)


You didn't declare that "num" to be a val or var, so its value is not available from elsewhere in the class - only from the primary constructor body.  If you declare with val, it works.



Hello Mike. Thank you very much for answering my question. In the case of num,



We didn't get error in the above code for "num". But below code is throwing error for "num"



What is the difference?
1 year ago
Hello @Stephan van Hulst, will you be able to help here in understanding what I'm doing wrong?
1 year ago
Hello Below is my code:



I'm expecting setter value output for variable3 i.e 3 in above case. But below is my output
This is variable1: 1
This is variable2: 0
This is variable3: 1

Question1: Why is my setter not being reflected?
Question2: In coment1 of my code, if you uncomment it, you will see error for value. It says "val cannot be reassigned". But I declared variable3 as "var". Why this error?
Question3: In Comment2 of my code, if you this code for variable3. I see 2 errors. (i) variable3 not initialized. (isn't it supposed to set value from setter/getter? ) (ii) num not recognized. (We are having this as parameter in class input values. Why is it not being recognized?)
1 year ago