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.
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?
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.
Joined: Oct 23, 2006
chaitanya, i just had the main method in it for testing. of course you can use it from any other class.
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.
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.