sealed class DestinationScreen(val route:String){
object Signup: DestinationScreen(route = "signup")
}
Now I am developing navigation screen above.
I don't understand this statement.
object Signup: DestinationScreen(route = "signup")
I think Signup is property. So to set it, should we write this below?
object Signup = DestinationScreen(route = "signup")
Why does not using = issue the instance and set the Signup property?
Please teach me. Thank you.
Nope. Signup is not a property. It's basically a class which extends DestinationScreen except it's a special class object which acts as a singleton and is initiated at the same point it's described. That's why you write it like that.
Why it looks like a property to you is you happen to declare it in another class (which makes it an inner class). But you can declare it outside of the class too.
More about Kotlin objects https://kotlinlang.org/docs/object-declarations.html
Sealed classes represent a class with a fixed number of subclasses. At first, you declare the parent class, for example, a class that describes Screen of your app. Then, you declare all children of this class. For example, HomeScreen and LoginScreen:
sealed class Screen
class HomeScreen : Screen()
class LoginScreen : Screen()
All subclasses can be written outside of the parent class (but must be located in the same file due to compiler limitations).
You can use the object keyword instead of class modifier in case of a class has no properties. It means that the object keyword declares a singleton class.
Because you are using inheritance, not an assigment.
A sealed class is a class which subtypes are known by the compiler so it allows you to create flow controls by type:
sealed class Result {
data class Success(val data...): Result()
data class Error(val exception...): Result()
}
So you can do:
when(val result = ...) {
is Success -> result.data
is Error -> result.error
}
Whith normal inheritance like on interfaces, open classes or abstract classes you dont know the typed thar inherit from the super type.
Related
I'm developing the app by using Kotlin.
sealed class DestinationScreen(val route:String){
object Signup: DestinationScreen(route = "signup")
}
#Composable
fun InstagramApp(){
val navController = rememberNavController()
NavHost(navController = navController, startDestination = DestinationScreen.Signup.route){
composable(DestinationScreen.Signup.route){
SignupScreen(navController = navController)
}
}
}
I don't know Why Signup singleton class can have the property "route" using argv?
I understand it inherits DestinationScreen. So it also has route property.
But Destination class doesn't have concrete the property route.
If Destination class is data class, make sense it doesn't need to declare the property.
No need for{}. And data class has the property not declareing it by using argv.
So I mean DestinationScreen should has concrete property route, if Signup inherit different property's value, it should override.
Why can this codes above work? Does this feature have seal class or object class?
Please teach me. Thank you.
But Destination class doesn't have concrete the property route
Yes, it does. The route property is declared right there in its constructor.
if Signup inherit different property's value, it should override
Not sure what you mean by this, but Signup doesn't need to override the property. It already inherits the property. By passing a value to the super-class's constructor, the existing property gets an initial value as passed by the sub-class without overriding it.
You mention sealed and data class types, but they are irrelevant to this discussion. Inheritance works the same way with sealed and non-sealed classes.
Any time a class extends another class, it also is a type of that class and inherits all of its properties and functions, no overriding needed.
I was checking Equatable package "how to use" examples and they pass values to the super class, and this subject like a blind spot for me, and I see it everywhere:
import 'package:equatable/equatable.dart';
class Person extends Equatable {
final String name;
// why? what's happening behind the scene here?
Person(this.name) : super([name]);
}
another example
#immutable
abstract class MyEvent extends Equatable {
MyEvent([List configs = const []]) : super(configs);
}
1- why we do that? why to pass values for abstract thing? for a blueprint? what's the use of this?
2- why sometimes developers pass values for abstract class like this?
3- what's happening behind the scene here?
4- what is the use cases for such a code?
thanks a lot.
A class being abstract doesn't exclude that it has some concrete members in Dart.
Take the following class as an example:
abstract class Foo {
Foo(this.value);
final int value;
#override
String toString() => value.toString();
}
While it is abstract, it has a concrete implementation of the property value, that is initialized via a custom constructor.
And since the parameter is required, the subclass must call the super constructor like so:
class Subclass extends Foo {
Subclass(): super(42);
}
abstract class MyClass() {
protected static foo: Array<number>;
protected static doWorkOnFoo(): void {
let x: number = 0;
for (let f of | what goes here? this? self?|.foo) {
x = x + foo;
}
}
}
When implementing an abstract class, and wanting derived classes to have a static property and a static method that operates on those properties, how would one access those in the abstract class so that the derived class can just use that method?
I know this can be worked around by just setting a default value on the static property and using this, but this sparked my interested and I'm curious to know if there's some way to access generic derived class or something from an abstract class in TS.
Thanks in advance!
EDIT:
While I wasn't able to find exactly what I was looking for (see comments), a workable solution is to change the signature of the doWorkOnFoo() method to the following:
protected static doWorkOnFoo(): (typeof MyClass) => void;
Since it is already an abstract class it can take a derived class as an argument and then reference the derived class's static properties.
My understanding is that we can't override the member variable but when i am running the below program, i am getting unexpected o/p
class Parent {
String message = "parent";
void say() {
System.out.println(message);
}
}
class Child extends Parent {
String message = "child";
}
public class Test {
public static void main(String[] args) {
new Child().say();
}
}
In the o/p i am getting "parent" While we are calling the say method using child object and even there is no Parent reference.
Can anybody help me to understand it.
Thanks
The "say" method is on the parent class, not on the child. So when it call for the "message" member it looks at his own, not at the child's one. The fact that the call is make through a child class has nothing to do in it.
Indeed, the member variable is not overriden here. This is expected behavior.
EDIT :
The Java Language Specification says that "If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class."
"Inherit" does not mean "copy". When you call the "say" method on a child instance, is not a "code copy" of the method that is called, but the method of the parent class, as it is defined in the parent class. And the parent class know nothing about the child variable member.
your child class is extend parent class features! so child class has say method by default and it's value (parent) because parent class say method will called when you call say method from child object. if you want to print "child" instead, you need override your say method in the child class and change that default feature, which extends from parent class.
As all private and public attributes and methods are inherited into a child class from its parent class then why would constructors and destructors be inherited into a child class?
Is there a real life scnario?
In most programming languages constructors and descructors are not inherited automatically. Usually base class can provide one set of constructors and child class can provide another set of constructors.
I think that in most cases abstract derived class should provide the same set of constructor as base class do (i.e. "inherit" constructors from the base class), but concrete derived class can resolve some of base class's constructor arguments and provide more usable set of constructors:
Consider following case. Let suppose we have a base class called BaseWCFProxy that requires string as endpoint name:
abstract class BaseWCFProxy
{
public BaseWCFProxy(string endpointName)
{}
}
class ConcreteProxy : BaseWCFProxy
{
public ConcreteProxy() : base("ConcreteProxyEndPoint") {}
}
But you decide to add additional abstract class between BaseProxy and ConcreteProxy than you should provide the same set of constructors as base class:
class DualChannelBaseProxy : BaseWCFProxy
{
public DualChannelBaseProxy(string enpointName) : base(endpointName) {}
}
So the rule of thumb is: if you write a abstract child you should consider to "inherit" all base classes constructors. If you write a concrete child you can provide separate set of constructors that would be appropriate for your clients.
P.S. We don't have the same issue with destructors because there is no such notion like destructors overloading. And they're inherited by default: i.e. descendant can provide some additional logic but it definitely should call base version.