logo
down
shadow

In Kotlin, how do I extend a class that has multiple constructors?


In Kotlin, how do I extend a class that has multiple constructors?

By : dsmeb
Date : November 18 2020, 01:01 AM
I think the issue was by ths following , Update: Since M11 (0.11.*), you can use secondary constructors to solve this problem:
code :
class PhotoLibException : RuntimeException {
    constructor(message: String, ex: Exception?): super(message, ex) {}
    constructor(message: String): super(message) {}
    constructor(ex: Exception): super(ex) {}
}


Share : facebook icon twitter icon
Is it possible to extend a class with no constructors in Java?

Is it possible to extend a class with no constructors in Java?


By : Clogg
Date : March 29 2020, 07:55 AM
Hope this helps A class with no constructors has an implicit public no-argument constructor and yes, as long as it's not final, it can be sub-classed.
If the class has only private constructors then no, it can't.
Why can I not extend a class with ambiguous constructors in Java?

Why can I not extend a class with ambiguous constructors in Java?


By : Ödlan Örjan
Date : March 29 2020, 07:55 AM
To fix the issue you can do You have varargs parameters (SomeType... arg), which is syntactic sugar for an array of SomeType. When you call such a method, the type is inferred from the parameter, but when you don't provide parameters the type can't be inferred, so both methods are equally a match.
The reason you are "calling" the constructor is because when you don't define a constructor, one is implicitly defined for you with no arguments, - the "default constructor" ie:
code :
public BogusSub() {
}
public BogusSub() {
     super(); // oops, can't tell which one you want
}
public class BogusSub extends BogusSuper {
    public BogusSuper(Collection<String>... a) {
        super(a); // typed to Collection<String>...
    }
    public BogusSuper(String... a) {
        super(a); // typed to String...
    }
}
Kotlin equivalent of class properties, constructors, empty parameter constructors, getters and setters

Kotlin equivalent of class properties, constructors, empty parameter constructors, getters and setters


By : Richard
Date : March 29 2020, 07:55 AM
this one helps. I am currently practicing in developing kotlin and as of now I seem to get confused with kotlin's class structure. , This is the equivalent Kotlin code for your Java code:
code :
class SampleClass(var `var`: String? = null)
Why to put val or var in kotlin class constructors

Why to put val or var in kotlin class constructors


By : Louis Botha
Date : March 29 2020, 07:55 AM
this one helps. If you omit val or var in in a constructor, then the only places that can access these parameters are initialization statements that are evaluated at construction time. See https://kotlinlang.org/docs/reference/classes.html
This is useful when you want to do something with a value before storing it. In Java you would put that code a constructor body
code :
class Person(firstName: String, lastName: String) {
    // directly in val / var declarations
    val firstName = firstName.capitalize()
    val lastName = lastName

    // in init blocks
    val fullName: String
    init {
        fullName = "$firstName $lastName"
    }

    // secondary constructors can only see their own parameters
    // and nothing else can access those
    constructor(fullName: String) : this("", fullName)
}
interface Named {
    fun getName(): String
}
class Human(private val fname: String, private val lname: String) : Named {
    override fun getName() = "$fname + $lname" // functions need val since
                                               // value is resolved after construction
}
class Person2(firstName: String, lastName: String) : Named by Human(firstName, lastName)

class Person3(human: Human) : Named by human {
    constructor(firstName: String, lastName: String): this(Human(firstName, lastName))
}
class Person4(firstName: String, lastName: String) {
    val fullName: String by lazy { "$firstName $lastName" }
}
How do primary constructors, secondary constructors and class members interact with each other in Kotlin?

How do primary constructors, secondary constructors and class members interact with each other in Kotlin?


By : Lajos
Date : March 29 2020, 07:55 AM
around this issue
the class members rely on the arguments in the primary constructor to be implemented
code :
<init>(initialCapacity: Int)
<init>(elements: Collection<E>)
Related Posts Related Posts :
  • What is Object dictionary in CANOpen?
  • Example of orbBasic Indexed User Variables
  • convert to ABSOLUTE in logback
  • How to conditionally download file using p:fileDownload
  • Error on pod install
  • Set HTTP GET Parameters in Finagle
  • different attack that uses sql injection
  • How can I change my xampp username not as 'root'
  • AMQP Content header payload structure
  • Apache POI formula evaluation not working for Excel IF
  • How can I trace RESTEasy's dispatch?
  • Map Freezes on iOS 7 with Google Maps SDK 1.4
  • Comparing lists, is the subset list within the first list
  • Non-ascii character highlight in Sublime Text 2
  • Installing Magit in Aquamacs
  • Receiving error - System.Net.Mail.SmtpException: 4.3.2 try again later
  • Coreaudio render callback in monotouch
  • The command 'yarn --v' also initiates 'yarn install' and installs packages automatically. Why is this happening?
  • save multiple matches in a list (grep or awk)
  • Can a number register be used in a groff request?
  • Mapping FAQ with RASA for large dataset (2000+)
  • Fragment not receiving LiveData updates after remove + add
  • FitText.js makes text bigger rather than smaller
  • ARM - Implementing stack with load/store multiple register values
  • How to check if a ChromeCast Session is already in progress
  • ngForm inside a Carousel Slide in UI Bootstrap not working
  • Clearing attributes in Tritium
  • "vagrant up" failing: Vagrant VM failed to remain in the running state
  • ftsearch returning empty docs
  • What are the advantages of setting "hive.exec.parallel" to false in Hive ?
  • Creating a root certificate in FiddlerCore
  • How to access app.config in a blueprint?
  • DB2 RECORDSET table name converted to uppercase
  • Resizing the superview according to the subviews
  • IExpress - Disable Compression
  • Getting InvalidProtocolBufferException while running oozie job
  • What are the differences between Play run and start?
  • How can I share props in ReasonReact?
  • Task.Delay is skipped
  • Parsley.js Password Confirm doesn‘t work
  • How to get all registred 'browser:resource' in Plone
  • Overriding page_list controller inside a package in Concrete5.6.1.2
  • Robolectric 2.x - dependent jars are downloading while running the tests
  • Setting Flyout to Main Frame Navigation(Windows 8.1 app store)
  • Build project - Nuget Error
  • How to recover admin password for SonarQube
  • perforce Tagging and labelling files
  • How to pass data from one window to another in Titanium?
  • TeamCity CI - Make custom build output folder
  • Multi-tenant ServiceStack API, same deployment to respond to requests on different hostnames?
  • How to show downshift + popper on top of material-ui dialog?
  • jQuery file upload and RequireJS configuration
  • How to send the result of a select query to a message body of a mail in oracle 10G
  • Worklight common build failing with "Failed to update main HTML file"
  • pg_listening_channels() is not returning the channels name
  • Asset management in ZF2
  • Does the Firefox add-on sdk allow direct modification of the http response byte stream?
  • How to remove menu hardware key from your android app
  • Identifying programming language
  • Use shell commands to find Makefile.am in configure.ac
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org