Why Parent class is allowing to Child class to make Parent class's methods as abstract in Child class? - scala

class Parent
{
def m1()
{
System.out.println("m1 method");
}
}
abstract class Child extends Parent
{
def m1()
}
The above code compiles succesfully, and my question is:
Why does the Parent class allow the Child class to make the m1() method as an abstract method?
Where would we use this kind of scenario?

Now it can so happen that you want to create multiple variation of the parent class. Now Parent class being a concrete class it is very hard to achieve that. Because you can either try to make the parent as abstract and then provide implementation. But if the concrete class is used in several places of your big code base you have little choice but to go as follows.
Hence the strategy is to create abstract child it goes as follows
abstract class Child extends Parent
{
def m1()
}
class SpecialChild extends Child {
//.. some implementation of m1
}
Now we can still use the original child
child = new SpecialChild();
Hope this makes sense.

Related

Akka Typed and having base class for MessageAdapters

I am trying something with Akka Typed and Scala, actually something very simple as concept but I could not make it work so may be you can help me.
All my Actors will have one common Signal, so I try to place it to a base class and let my all Actors share it but it the compiler refuse it in the MessageAdapter....
So my code looks like following....
object ContractActor {
sealed trait ContractEvent extends BaseEvent
final case class onApprove(payload: Payload) extends ContractEvent
}
class ContractActor(ctx: ActorContext[ContractEvent]) extends BaseActor {
val listingAdapter = : ActorRef[Receptionist.Listing] = ctx.
messageAdapter(
listing => onAddRelatedEvent(listing)
}
and base actor
object BaseActor {
trait BaseEvent;
final case class onAddRelatedEvent(listing: Receptionist.Listing) extends BaseEvent
}
The compiler complains about onAddRelatedEvent is not known on ContractEvent which surprise me because ContractEvent extends BaseEvent....
What am I missing here....
Class ContractActor extending BaseActor does not automatically bring BaseActor's companion object into scope. To bring it into scope, just import it inside class ContractActor:
import BaseActor._
Alternatively, you could move the inner trait/case class into BaseActor's companion class.

Can we inherit Ktable from parent class to child class?

We have a requirement to reuse KTable(Gender) in multiple classes by inheriting from parent class but when we create inheritance linkage then it shows an error Topic has already been registered by another source
I am passing builder parameter from singleton object.
How could it can be possible in seperate two classes?
object app{
def main{
builder properties .....
new Parent(builder).test()
new child(builder).testing()
}
}
Class Parent(builder : StreamBuilder) {
val gender : KTable = ........
}
Class child(builder : StreamBuilder) extends Parent(builder){
gender.tostream.peek((k,v) => println(v))
}

Create a child class's instance in parent class's method

abstract class Parent {
def filter(p: Parent => Boolean): Parent = filterAcc(p, new Child)
}
class Child extends Parent {
// ...
}
I am working on Scala tutorial and wondering how the following can be possible.
There are two classes Parent and Child. The Parent class creates an instance of child in the method filter.
How can a parent class refer to a child class which inherits the parent class?
That is no contradiction. If parent and child are defined within the same compilation unit, then the parent can refer to its sub-class, both symbols/types are known to each other.

Ambiguous reference to overloaded definition with inherited inner class, scala

So here is the problematic code:
trait World {
type State
def dynamics(s: State): State
// ...
}
trait GridWorld extends World {
class State {...} // concrete
def dynamics(s: State) = s // concrete
// some other staff still abstract
}
trait SimpleGridWorld extends GridWorld {
class State extends super.State {...} // concrete
def foo {dynamics(new State)} // compiler error
}
The compiler says that, dynamics in both World and GridWorld match the signature. However, in World it is abstract and then implemented in GridWorld, so it seems to me that it is clear that I am calling GridWorld.this.dynamics.
Another thing I noticed is that, if I remove the extends super.State in SimpleGridWorld, everything works fine (which I don't understand why, and I do need the functionalities defined in GridWorld.State here). Any explanations? Thanks!
UPDATE
Anyway I am seeing my design pattern quite weird, since if State in SimpleGridWorld does not inherit GridWorld.this.State, dynamics would refer to the unimplemented one defined in the root trait World (which makes sense because the implementation in GridWorld may use the functionalities of GridWorld.this.State which may not exist in SimpleGridWorld.this.State). But what I want is:
XXXWorld.this.State must inherit its super.State (or just use it)
dynamics always refers to super.dynamics if implemented in the super trait/class unless overrided here.
How can I do this? I think it is not a totally irrelevant question, and probably the answer to the previous one would tell me how to redesign my pattern.
How about:
trait World {
type State
def dynamics(s: State): State
}
trait GridWorld extends World {
type State = MyState
class MyState {} // concrete
def dynamics(s: State) = s // concrete
}
trait SimpleGridWorld extends GridWorld {
class MyState extends super.MyState {} // concrete
def foo {dynamics(new MyState)} // compiler error; ok
}

Seamless weaving of trait

I would like to automatically weave the definition of a new function say introduced by an extending trait Ext into an abstract class A:
class Base {
abstract class A
class B extends A
case class C extends A
}
trait Ext extends Base {
trait A extends super.A {
def say = "hello"
}
}
object Test extends Base with Ext {
val b = new B
b.say
}
However, I obtain the following error:
<console>:12: error: value say is not a member of Test.B
b.say
Any way of doing it?
It seems you are trying to use virtual classes, which is a feature not available in Scala.
Once A and B are defined they can't be redefined (like method overriding).
abstract class A
class B extends A
On the other hand, given your example, your objective could be achieved by a simple mixin. Here it is with few rewrites:
class Base {
abstract class A
class B extends A
case class C extends A
}
trait Ext extends Base {
trait CanSay extends A {
def say = "hello"
}
}
object Test extends Base with Ext {
val b = new B with CanSay
def apply = b.say
}
Test.apply
No sure it will really help, but at least will help you understand what is going on.
Okay, as I said in a comment, it's not entirely clear what you're trying to do here, so I can't really try to suggest ways to do it. However, the approach you're using at the moment will not work.
Consider the class Hierarchy in this situation. At the base, we have A, which is then subclassed with B (in Base) and with Ext.A. These are not related save by their shared supertype, so you'll never find a say method on an instance of B.
The confusion possibly arises through the use of the word abstract. An abstract modifier on a class (even an inner class) does not make it an abstract member of the parent class, but denotes that it itself may have abstract members. There are ways of giving a class an abstract class member - through type parameters or type members. Unfortunately, you cannot derive from these AFAIK.