Do every class in Kotlin extends Any class

kotlin constructor overloading
kotlin data class
kotlin class inheritance
abstract class kotlin
kotlin data class inheritance
kotlin sealed class
kotlin data class constructor
kotlin class type

I am writing an extension function for RecyclerView to set LayoutManager and Adapter to the RecyclerView.The function should accept any Adapter class which is Extending from RecyclerView.Adapter<Any> as like in Java we use RecyclerView.Adapter<?>.How can we acheive this in Kotlin?.

internal fun <T : RecyclerView.Adapter<Any>> RecyclerView.initAndSetAdapter(context: Context, adapter: T) {
    this.layoutManager = LinearLayoutManager(context)
    this.setHasFixedSize(true)
    this.adapter = adapter
}

The function call to this method is not compiling due to Invalid Type.But if i change Any to * then it is working properly.

internal fun <T : RecyclerView.Adapter<*>> RecyclerView.initAndSetAdapter(context: Context, adapter: T) {
    this.layoutManager = LinearLayoutManager(context)
    this.setHasFixedSize(true)
    this.adapter = adapter
}

Why method call is not compiling with Any class?.Do every class in Kotlin extends Any class like Java Object class?.


As per the documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html

Every Kotlin class has Any as a superclass.

That being said - the class that adapter takes needs to extend from ViewHolder.

Classes and Inheritance, A class in Kotlin can have a primary constructor and one or more secondary first statement of a secondary constructor, so the code in all initializer blocks and​  All classes in Kotlin have a common superclass Any, that is the default superclass for a class with no supertypes declared: class Example // Implicitly inherits from Any Any has three methods: equals() , hashCode() and toString() .


The type of RecyclerView.Adapter has to be a ViewHolder or ViewHolder subclass. This is defined in the class and you can’t expand its scope to Any or Object. That would defeat the purpose of generics. By using the star projection it automatically uses the largest bounds defined by the class <out ViewHolder>/<in Nothing>.

Inheritance, You can then subclass from that class by declaring a new class which The subclass inherits all members that exist in its superclass - both those that are directly Subclasses of a class that implements an interface (in this case, Car ) are also  The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.


RecyclerView.Adapter<Any> is equivalent to Java RecyclerView.Adapter<Object> which is not at all the same as RecyclerView.Adapter<?>. In particular, if you have something like

class AnAdapter extends RecyclerView.Adapter<SomeType>

then AnAdapter is a subtype of RecyclerView.Adapter<*> (? in Java) and not a subtype of RecyclerView.Adapter<Any> (Object). So you can't pass an instance of AnAdapter to a method expecting a subtype of RecyclerView.Adapter<Any>.

Please see Variance in Kotlin documentation.

Data classes - Classes, As opposed to Python, declaring a property directly inside the class does not create a class-level property, but an instance-level one: every instance of Person​  Yes, every abstract class will need parens while interfaces work without. The compiler knows whether you're implementing an interface or extending a super class – s1m0nw1 Jan 16 at 9:12 add a comment |


Any, Kotlin class hierarchy. Every Kotlin class has Any as a superclass. takeUnless. Returns this value if it does not satisfy the given predicate or null , if it does. Any Any is an open class and by default the superclass for all the classes, whether we define it explicitly or not. This is similar to the Object class in Java which is the superclass for each and every class.


Sealed Classes, A sealed class can have subclasses, but all of them must be declared in the the possibility for data classes to extend other classes, including sealed classes.). Kotlin Class. Before you create objects in Kotlin, you need to define a class. A class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.


Data Classes, In such a class some standard functionality and utility functions are often Since 1.1, data classes may extend other classes (see Sealed classes for examples). generated class needs to have a parameterless constructor, default values for all and copy() implementations, and there will only be one component function  A data class may extend other classes (since Kotlin 1.1) Data classes are first-class citizens in Kotlin. In a very short syntax they offer a frictionless solution with all the advantages and no compromises. What does it mean for Android. I’m gonna try to explain what are the main advantages that I’ve found using Kotlin data classes in my Android projects.