how can i implement a function ex: 'add item' without using oop ?!

Recommended Answers

All 6 Replies

how exactly do you mean?
without using oop, you can have something like this:

public class Test{

public static void main(String args[]){
 addItem(1);
}

public static void addItem(int item){

}
}

but I doubt this is what you're looking for. normally, I expect an Item to be an Object, so that would be using OOP :)
also, I assume adding an Item would be adding it to an array, or a collection of some sort, which are ... objects :)

It doesn't help that there is no single universal definition of OOP, but I would say that if you just have static methods and don't declare any classes (other than the one the main method is in) and never use the "new" keyword, then your code has nothing "Object Oriented" about it.
Just to be safe, you should restrict yourself to primitive types (int, char etc) because these, by definition, are not Objects. I wouldn't worry about arrays - every non-OO language from C upwards has arrays, so I don't think they are necessarily "objects".

all i need is to implement an add item function without using oop

hmm ... if I recall correctly, arrays in Java are objects. could be wrong, but I'm pretty sure about it.

Absolutely right. The JLS chapter 10 begins "In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array."
My point was that even if Java treats arrays as objects, the fact that every non-OO language has arrays means that arrays in general are not necessarily objects, and using arrays isn't necessarily Object-Oriented. Plus, at a practical level, if you can't use arrays then what will the "add" method add to?

Absolutely right. The JLS chapter 10 begins "In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array."
My point was that even if Java treats arrays as objects, the fact that every non-OO language has arrays means that arrays in general are not necessarily objects, and using arrays isn't necessarily Object-Oriented. Plus, at a practical level, if you can't use arrays then what will the "add" method add to?

ah, ok :)
yes, I also had that thought, how to add an item ... without an array it would modify the current value instead of saving an additional new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.