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))
}
Related
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.
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.
In my project I have bunch of animal objects, for example:
some of them have dependency injection:
class Monkey #Inject() (wsClient: WSClient, configuration: Configuration) extends Animal {
...
}
and some not:
class Giraffe extends Animal {
...
}
In my AnimalsService class I need a list of all the animal objects instances,
Currently my service is getting the list of people as a dependency injection:
class AnimalsService #Inject() (animals: List[Animal]) {
// here I can use animals as my desire
}
and then I have a binding class that bind it:
class Bindings extends AbstractModule {
override def configure(): Unit = {
bind(classOf[AnimalsService]).toProvider(classOf[AnimalServiceProvider])
}
}
object Bindings {
class AnimalServiceProvider #Inject () (giraffe: Giraffe, monkey: Monkey ...) extends Provider[AnimalsService] {
override def get: AnimalsService = {
new AnimalsService(List(giraffe,monkey...))
}
}
}
This works perfectly, but what I would prefer is to have somehow to add the list to my application context as the app loads so I don't need to do it this way....
This current solution also means I need to add new animals to AnimalServiceProvider constructor and to here new AnimalsService(List(giraffe,monkey...)) every time I need a new animal, and that will be happened constantly...
What will be the best way of handling this kind of situation?
I thought maybe using #Named annotation of guice but not sure if its the right way or how to name a list of objects this way...
I would do a Singleton bean that has the list and a method to add definitions
class AnimalsService #Inject() () {
val animals: List[Animal]
def addAnimal(animal: Animal) {
....
}
// here I can use animals as my desire
}
Then in each module that needs an animal would need an extra singleton with Eager bindings
class MonkeyConfigurator #Inject() (animalsService: AnimalsService) extends Animal {
animalsService.add(this) //Or anything
// here I can use animals as my desire
}
I have a class foo which extends actor
class foo extends Actor{
def receive:PartialFunction[Any, Unit] = ???
protected def fooFunctionIWant = { print("hello")}
}
And I have another class "bar" which I want to extend foo so it will inherit the fooFunctionIWant
But when I try:
object bar extends foo {
def barFunc = {
fooFunctionIWant
}
}
and then:
bar.barFunc
I get :
Caused by: akka.actor.ActorInitializationException: You cannot create an instance of [...] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
at akka.actor.ActorInitializationException$.apply(Actor.scala:173) ~[akka-actor_2.11-2.4.1.jar:na]
at akka.actor.Actor$class.$init$(Actor.scala:436) ~[akka-actor_2.11-2.4.1.jar:na]
The only correct way to create an actor instance is by using one of the actorOf methods. Using new Actor or object Actor will fail during runtime.
You can't call any methods within the actor directly, you can only send messages to it and receive messages from it. Otherwise, if that would be possible it would break Actor encapsulation.
If you want to share code between an actor and a regular class you can put that shared code in a trait and inherit from it. However, your actor will be able to use that code internally only and will not expose any methods, its instance type returned from actorOf will still be Actor only.
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.