• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

When to extend and when to create object

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I am Chaitanya, I have a doubt regarding when to extend a class and when to create object for a class and use it.

Suppose I am having a billing application and there are many input fields asking the user to enter the values such as mobile number, price, name, quantity. Like these I am having many input windows. So what I did is, I created a method for each type of input filed.

One method for validating input fields such as mobile number, quantity. One for validating mobiles, one for validating price etc.

Whenever I want to validate these fields I am simply creating object for the class.

Can anyone tell me about this, thank you all in advance.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make all the validate method as static/utility methods .
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:Make all the validate method as static/utility methods .



Hi Venkataswamy, can you please tell me somewhat detailed.

Thank you in advance.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be like



 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi @ Nandini,

I dint't ask about static methods and how to use them, I asked why should I make it static, there are many classes in my application and each class will have input fields, so I create a class(Validate.java) which consists all these methods to validate different input fields, your code only works for the class which has main function or the main class

My question is should I extend the Validate.java class or should I create an object for it?

Thank you all in advance.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would not use a static approach because then you'll lose the polymorphic abilities and thus you're going to need a lot of classes. About the question "My question is should I extend the Validate.java class or should I create an object for it?" Those are 2 completely different things. Extending is adding or specializing behaviour and creating an Object is invoking an constructor which results in a new Object being created. I'm going to assume that you meant to say "create a new Class for it". I would go for extending provided that there is common ground (i.e. an Date validator should not extend a String validator). This way you can program to an interface/superclass and not specialized implementations.
 
Nandini Bhaduri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chaitanya, i just had the main method in it for testing. of course you can use it from any other class.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr.Wouter, I understood the concept you were explaining somewhat, I will describe you a scenario where I will extend and create object depending on the situation.

Suppose that I am having a class called Validate which consists the methods for validating a mobile number, email ID, price, user name.

I will create an object for the Validate class in the classes in which input fields need to be validated.


Now for suppose in one class I want to validate an email ID, but the domain names must be only gmail, yahoo, aol etc. So I can't use the validate email Id method of Validate class, so what I will do is I will create another class named ValidateExtended which extends Validate class

Now I ll create object for ValidateExtended class where I want to use the second type of validateEmail(String) method.

Is my approach right one?

Thank you all in advance.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there anyone? Is my approach right or wrong?
 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's wrong.

I say that because you named the class "ValidateExtended". If you can't think of a good name for the class, but you just have to say that it extends some other class, then it must be the case that you didn't have a good reason for extending it. Normally the reason for extending the class would provide a suitable name for the class.

In general the answer to the question "Should I extend a class" is "It depends". Followed by a discussion of the options available in the case you had in mind. In the case you described it's quite possible that extending the Validate class might be the correct option.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul, seems I am going in the right direction. :thumbup:
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic