File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes When to extend and when to create object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When to extend and when to create object" Watch "When to extend and when to create object" New topic
Author

When to extend and when to create object

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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.


Love all, trust a few, do wrong to none.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Make all the validate method as static/utility methods .
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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.
Nandini Bhaduri
Greenhorn

Joined: Oct 23, 2006
Posts: 11
It will be like



chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Nandini Bhaduri
Greenhorn

Joined: Oct 23, 2006
Posts: 11
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

Joined: Sep 15, 2009
Posts: 806

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

Joined: Sep 15, 2009
Posts: 806

Is there anyone? Is my approach right or wrong?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

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

Joined: Sep 15, 2009
Posts: 806

Thank you Paul, seems I am going in the right direction. :thumbup:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to extend and when to create object