• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a new Constructor in Child.class which is not in Parent.class  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've a parent class with a argument constructor like below(a sample code)



Also I've child.class which extends Parent.class as shown below,


Now, I want create/modify the constructor which is in child, by taking "int i" as an input instead of "String name". How can I do that? Run time I want to execute child constructor not a parent constructor.

Condition is: With-out making any changes to the Parent class

Thanks:
Ramakrishna K.C
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramakrishna,

You can add new constructor in a child class which takes int as an argument.




 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:Now, I want create/modify the constructor which is in child, by taking "int i" as an input instead of "String name". How can I do that?

It depends entirely on what you want it to do. Do you, for example, want to store it in a field in the child class, or do you simply want to convert the integer to a String?

If the latter, then I'd question the wisdom; not because you can't do it, but because StringsAreBad.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Hi Ramakrishna,

You can add new constructor in a child class which takes int as an argument.





No you can't. The second constructor will have an implicit call to super() as its first line, but the parent class doesn't have a no-args constructor.
Your constructor would have to be something like
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tushar Goel Is your code execute without compilation error? Can you check and tell me?

@Winston Gutkowski Actually I've crawljax open-source code. I'm building top of the crawljax-core-3.4.jar. In that, They have class called CrawlerController.java which fires a constructor with lot of parameters. In those parameters, one of the parameter is "CrawlToolConsumer consumer". Actually "CrawlToolConsumer " this is another class. So, now I created a class called CrawlToolConsumerchild which extends its parent. i.e "CrawlToolConsumer ". In parent class(CrawlToolConsumer) they have only one constructor. In that they are receiving a reference of that class. i.e "CrawlToolConsumer"

Now, in my child, I want to build a constructor in such a way that, I want to receive the child reference i.e "CrawlToolConsumerchild". For that I'm asking, how to fire a constructor.


I think, I'm clear. If not, please tell me. I'll paste all code which is in open-source code.


Thanks:
Ramakrishna K.C
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:@Tushar Goel Is your code execute without compilation error? Can you check and tell me?

No it won't. See my post above.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am sorry guys. It will not executed..
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay fine. I'll post the source-code first.


see line number 2. I mentioned the parameter. That Provider<CrawlTaskConsumer> consumerFactory parameter giving me a reference. so, they are doing this.consumerFactory = consumerFactory; in line number 7. I extend the CrawlTaskConsumer class with my own class called CrawlTaskConsumerChild. For this I need to fire a constructor. So I want to something like this,

My current code.


Obviously the above code should give the compilation error. so it is giving compilation error "The constructor CrawlController(ExecutorService, Provider<CrawlTaskConsumerChild>, CrawljaxConfiguration, ExitNotifier, CrawlSessionProvider, Plugins) is undefined"

Now, how can I resolve this?

Thanks All:
Ramakrishna K.C

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do that. Provider<CrawlTaskConsumerChild> is not a subtype of Provider<CrawlTaskConsumer>, in the same way as List<Double> is not a subtype of List<Number> (here's why).

You simply must pass Provider<CrawlTaskConsumer> to the parent constructor. If the authors of the class wanted you to put in providers parametrized by subclasses, they should have specified Provider<? extends CrawlTaskConsumer> as the type in the constructor. It's impossible to say whether this is an omission or intention.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for expected and satisfied reply.

So, assuming that No change in the Original source code, I can't do this or I can't fire a constructor? Okay. Then, Is there any way to do like this other than what I proposed? I hope, you got my problem.

Thanks All:
Ramakrishna K.C
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The arguments passed to super(...) must match the types of the parameters required by one of the superclass' constructors.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct. But, here there is only one constructor. If possible, please download crawljax-3.4 open source code its a small project around 500 KB. In github they given sample code. Copy Paste Advanced Example. required jars POM.xml is



see the StateMachine.class. In that I want to change this code (Original code)



I want my code as



Thanks:
Ramakrishna K.C
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the CrawlController use the consumerFactory? I assume it just obtains CrawlTaskConsumer instances from it. In this case, declare your factory as Provider<CrawlTaskConsumer>, as it can return instances of CrawlTaskConsumerChild as well, they just will be "seen" as CrawlTaskConsumer.

If you want your CrawlControllerChild to only accept factories that provide CrawlTaskConsumerChild instances for some reason, you *might* just use a nasty trick to cast across the generic type via a raw type. I won't show you how, since I believe you should figure it yourself if you want to use it - it's potentially dangerous. It will (rightly) generate generic warnings though, which means that unless you know that this operation is safe (and you may know so only by inspecting all of the code that uses the factory), you might get some cast exceptions at runtime.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:see the StateMachine.class. In that I want to change this code (Original code)



I want my code as


Now that's a completely different question, isn't it?

You can't do that. It's a static method, it cannot even be overriden.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need constructors matching arguments and arguments matching constructors.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Martin Vajsar Nope, Its a connected. To call this static method you need to get hold of the instance. See other two params it comes from when you get hold of that instance. Its not different, its same. Its linked from one class to another, another class to another like that.

@Campbell Ritchie Dint get you.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:@Martin Vajsar Nope, Its a connected. To call this static method you need to get hold of the instance. See other two params it comes from when you get hold of that instance. Its not different, its same. Its linked from one class to another, another class to another like that.

I'm not sure I understand this completely. I also looked up the StateMachine class in the crawljax library, and didn't find any createIndex method. I'm afraid I'm too confused here to be able to help. Can you try to describe what you need in more detail?
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you simply run that crawljax with Advanced example. You'll get crawled page in specified path.

Copy this code and run this with all required jars.



When you go to this URL There is an button like LOAD MORE. If you click this you'll get some more reviews. So I want to do that programmatically. After the page crawled, in D:/zomatoPath/doms folder you'll get the page as index.htnl, state1.html, state2.html etc., So compare those two pages. You'll get my goal. But, I want that state.html renamed as url.html. If my URL contains regexp then remove that and then my url become like httpwww.zomato.combangalorechutney-chang-mg-road.html with all reviews. around 120-130 reviews are there. I want that in my crawled page. I think I'm clear.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is possible right?



I want explanation.

Thanks:
Ramakrishna K.C
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:This is possible right?
...
I want explanation.

Certainly, that is possible. You can call an existing constructor in a super class (the super keyword) or the current class (the this keyword), based on the types and number of parameters. General method overloading rules apply.

When you simply run that crawljax with Advanced example. You'll get crawled page in specified path. (...)

It sounds like quite some work, especially for someone who hasn't prior experience with the library. It might be better to ask a question about the crawljax library in the Other application frameworks, or perhaps Other open source projects forums. It's certainly too difficult for Beginning Java forum.

Did you try to find out whether the modification of the behavior of the library you're seeking to do is doable via some configuration? If I understand the change you want to make well, it looks like quite an ordinary requirement to me.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Martin Vajsar Yup.. I made the change, according to my requirement. In Crawljax they used Google Juice, that juice made magic and helped me to get the output what I want.

But, In open source most of the places they annotated @Inject above the parameterized constructor. Is there any simple explanation. How juice will inject parameters to the constructor?

Thanks:
Ramakrishna K.C
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!