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

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.

Related

Scala: Extend base class that contains 'apply' method

I have a base class in the 'common' module that looks like this:
class BaseClass(args: Seq[String] = Seq()) extends Serializable {
private val argMap: Map[String, String] =
// <More code here...>
object BaseClass {
def apply(args: Seq[String] = Seq()): BaseClass = new BaseClass(args)
}
Now I want to extend this BaseClass in my 'module' so I am trying this...
class MyNewClass(args: Seq[String] = Seq()) extends com.xyz.BaseClass {
// Add additional code here
}
object MyNewClass extends com.xyz.BaseClass {
def apply(args: Seq[String] = Seq()): MyNewClass = new com.xyz.MyNewClass(args)
}
My understanding is, when I instantiate MyNewClass it will automatically instantiate & call the 'apply' method of the base class but that's not happening. What is a proper way to extend the BaseClass in a way that all its variables & methods can be accessed via the Child class?
My understanding is, when I instantiate MyNewClass it will automatically instantiate & call the 'apply' method of the base class...
Your understanding isn't quite on.
extends com.xyz.BaseClass means that this class inherits from the base class, not the singleton object.
And new com.xyz.MyNewClass(args) creates a new instance of the specified class, bypassing the apply() method in any companion object.
What is a proper way to extend the BaseClass in a way that all its variables & methods can be accessed via the Child class?
The current code does exactly that. MyNewClass, and its companion object, inherits all members from BaseClass. Nothing is inherited from the BaseClass companion object because you can't extend an object, and you don't inherit the access permissions from BasseClass so while a BaseClass instance can access private members of the BaseClass companion object, a MyNewClass instance cannot.

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))
}

How to specify "child of [T]" as a type in a Scala case class

Let's say I have a Report class and two child classes
class Report {
}
class SubReport extends Report {
}
class SubReport2 extends Report {
}
and then I have a case class, where in one of the properties (reportType), I want to use to specify one of the SubReport types
case class SubReportClient(
reportType: Report //Using parent class
)
My question is, is there a keyword or construct where I can specify a
something like ChildOf(Report) as a type in the case class, and it would have the type checking accept any child classes.
I have tried instantiating the case class like the below, but failed.
SubReportClient(
classOf[SubReport]
)
case class SubReportClient(reportType: Report) is actually correct, it will accept all subclasses of Report, and nothing else. Is that not what you want?
If you want access to the actual type inside the class, you could make it generic:
case class SubReportClient[T <: Report)(reportType: T)
But this way SubReportClient(new SubReport) and SubReportClient(new SubReport2) are instances of two different classes.

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

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.

Writing a Python method to reference the Class attribute in the derived class, rather than base class

As someone who worked more in Java, I am having a bit of difficulty wrapping my head around polymorphic references to class attributes in Python.
What I would like to do is have a method in the base class which modifies a "static" variable (aka class attribute) of the base class, but when calling the method from the derived class, for the method to modify the class attribute of the derived class, not the base class. Note, I am NOT overriding the method in the derived class.
For example, I have something like:
class BaseClass:
itemList = []
def addItem(thing):
BaseClass.itemList.append(thing)
class Child1(BaseClass):
pass
class Child2(BaseClass):
pass
...
Child1.addItem("foo")
Child2.addItem("bar")
print(str(Child1.itemList))
I'd like: "foo"
I get: "foo, bar"
Now, I understand that because of "BaseClass.itemList.append(thing)", it will reference the class attribute of the base class.
Put another way, is there a way to avoid saying "BaseClass.itemList", but keep it static, or do I need to instead override the method in each of the child classes?
You can have a "static" class variable that can be changed by every instance of the class:
class BaseClass:
itemList = []
def addItem(self, thing):
self.itemList.append(thing)
class Child1(BaseClass):
itemList = []
class Child2(BaseClass):
itemList = []
# each class has its own "itemList"
# now we can instantiate each class and use the different itemLists:
c1 = Child1()
c1.addItem("foo")
c2 = Child2()
c2.addItem("bar")
c3 = Child1()
c3.addItem("foo2")
print(str(Child1.itemList)) # prints: ['foo', 'foo2']
print(str(Child2.itemList)) # prints: ['bar']