• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Variable Initialization and Access

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I was working on a simple program and I came across this scenario. My code is shown below



When it comes to callThis() method, 'c' throws a NullPointerException. My question here is how is a new 'class A' object created if I call its method from its subclass. I think I know the answer but I am not sure of it. So can you guys please help me out in figuring this out.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B is a separate object, you didn't call B's initialize method.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:B is a separate object, you didn't call B's initialize method.



B is a seperate class. B doesn't have an initialize method or you can say I don't want an initialize method in B
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:B is a seperate class.

True

Partheban Udayakumar wrote:B doesn't have an initialize method

False. B already has a initialize method Since it extends A

Partheban Udayakumar wrote:I don't want an initialize method in B

If you dont want it, maybe you need to set it as private instead of public.But even if you do so, you would still get a null pointer since you have to initialize c before calling its method.

What exactly here are you trying to achieve ?
You probably need to understand, a Class and an Object are different. Once a class is declared, you can create multiple objects of it. In your program, you created two objects. They wont share the same field variables (unless you bring static into picture here)

 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=salvin francis

Partheban Udayakumar wrote:B doesn't have an initialize method


False

B already has a initialize method Since it extends A


I meant B doesn't have its own initialize method but it has initialize from A .

So I need to call initialize in B and not in A. Is that correct?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

Partheban Udayakumar wrote:B doesn't have an initialize method


False

B already has a initialize method Since it extends A



I meant B doesn't have its own initialize method but it has initialize from A .

So I need to call initialize in B and not in A. Is that correct?
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:So I need to call initialize in B and not in A. Is that correct?


Partheban, it all depends on what you want to achieve. Based on your given scenario, if you want to avoid a NullPointerException, you would have to call the initialize it.

similarly,

However, in real life you need to ask your self :
  • Why should B extend A ?
  • Is B a specialized type of A ?
  • Why am i not initializing stuff in my constructors ?



  • Its all abstraction here until you provide a real world example
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    Spring AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:
    Its all abstraction here until you provide a real world example



    We are using selenium for data scrapping practice.

    salvin francis wrote:
    Why am i not initializing stuff in my constructors ?



    That is a good idea. But I need to pass a variable in the initialize method. That is why I didn't do it in constructor.

    salvin francis wrote:Why should B extend A ?
    Is B a specialized type of A ?



    The problem here is I have many classes implementing the same set of codes again and again. So I decided to make a parent class A with the set of repeated codes and call it from subclasses with parameters. As of now I have 5 sub classes and this may extend upto 25 - 30. So I can't be writing the same set of codes again and again. One more thing is that I am receiving an int (as said above) from user to determine which sub class to execute. So A is the class which is run first, from it branches out to all other classes and the sub classes access the methods in the parent class.

    Note: Now I feel something is wrong with my design, I should have kept the methods which are repeating in a different class rather than the parent class. Am I right?
     
    salvin francis
    Bartender
    Posts: 2908
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Partheban Udayakumar wrote:We are using selenium for data scrapping practice.


    I don't know much about this so I cant give info here.

    Partheban Udayakumar wrote:That is a good idea. But I need to pass a variable in the initialize method. That is why I didn't do it in constructor.


    I still am unclear as to why that is deterring you from using a constructor

    Partheban Udayakumar wrote:
    The problem here is I have many classes implementing the same set of codes again and again. So I decided to make a parent class A with the set of repeated codes and call it from subclasses with parameters. As of now I have 5 sub classes and this may extend upto 25 - 30. So I can't be writing the same set of codes again and again. One more thing is that I am receiving an int (as said above) from user to determine which sub class to execute. So A is the class which is run first, from it branches out to all other classes and the sub classes access the methods in the parent class.
    Note: Now I feel something is wrong with my design, I should have kept the methods which are repeating in a different class rather than the parent class. Am I right?



    Since I do not know your exact requirements, here are a few points from my end:

  • Don't use inheritance when you don't need it. For example, don't link Cabbages and Cars just because they both have "getColor()" method
  • Separate stateless methods into a utility class. For example, Lib.getShortDateFormat();
  • Design your structure for future modifications. Make it meaningful from inheritance point of view.


  •  
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    Spring AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:Design your structure for future modifications. Make it meaningful from inheritance point of view.





    salvin francis wrote:I still am unclear as to why that is deterring you from using a constructor



    If I use a variable in a constructor, the implementing classes should send the same variable again in super call. That is



    This is getting the value from parent class and then sending the value to it again from child class. That is why I didn't do that.

    salvin francis wrote:Since I do not know your exact requirements



    I will give you the scenario,

    1) I have 25-30 individual classes. I have around 4-5 methods which are repeating in the all the 30 codes.
    2) I get input from the users and based on the input I execute one of the 30 classes.

    How would you design this?

     
    salvin francis
    Bartender
    Posts: 2908
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Partheban Udayakumar wrote:
    1) I have 25-30 individual classes. I have around 4-5 methods which are repeating in the all the 30 codes.



    What do you mean by repeating ? Does it mean same code or does it mean that the method name is the same but its implementation differs from class to class ?


    Partheban Udayakumar wrote:
    2) I get input from the users and based on the input I execute one of the 30 classes.
    How would you design this?



    Your requirement is similar to a Factory design pattern

     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    Spring AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:What do you mean by repeating ? Does it mean same code or does it mean that the method name is the same but its implementation differs from class to class ?



    Methods will have the same signature, just the values passed to it differ.

    salvin francis wrote:Your requirement is similar to a Factory design pattern


    Ya. you are right. I too am noticing this now. This is not the same but something similar to it.
     
    Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic