Why do we need to have classes in OOP

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Hi everyone, I want to know about object orientated programming. I don't understand what is class in OOP. What does class do in programming. When do we need to have class in OOP. I am looking for simple example. Can anybody give the little explanations.
 

Papabravo

Joined Feb 24, 2006
21,225
Hi everyone, I want to know about object orientated programming. I don't understand what is class in OOP. What does class do in programming. When do we need to have class in OOP. I am looking for simple example. Can anybody give the little explanations.
It is not really the sort of thing you can do in a forum post. The internet makes it possible to teach yourself lots of things; give it a try and you'll be pleased with the results.

Are you familiar with some typical data structures used in programming? Do you know the difference between an array and a 'struct', or a record in some languages. That is a good place to start.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,058
Hi everyone, I want to know about object orientated programming. I don't understand what is class in OOP. What does class do in programming. When do we need to have class in OOP. I am looking for simple example. Can anybody give the little explanations.
I'd recommend hopping on any of a number of free tutorial websites, such as TutorialsPoint or CodeCademy , and start working through the lessons on Java or Python (although I think Java might be a better choice for what you are trying to learn about here).

But, in a nutshell, a 'class' is the combination of a data structure and the functions that operate on that data structure. The idea is to marry them so strongly together that it becomes difficult to use a function with the wrong type of data (something that happens all-too-often in non-OOP programming).

If you are familiar with C (or one of the many related languages), when you print a string you pass the printf() function a pointer to a memory address where, hopefully, a character string is stored. But at the end of the day you are just passing it an address and anything could be stored at that address. The printf() function has no way to detect whether or not it is actually a properly formatted character string. But in an OOP language, you would have a data type, say String, that has a print() function that is part of that class. If you have a variable of type String called fred, if you use fred.print() the compiler knows to use the print() function associated with the type of variable that fred is. Every class can now have its own print() function defined and whenever you call it from a variable of a type that does have it defined, it will always use the correct function because the function is fundamentally tied to the object's class.

There are other big reasons for using classes. Learn about data abstraction, inheritance, and polymorphism, which are generally considered the big three.
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Are you familiar with some typical data structures used in programming? Do you know the difference between an array and a 'struct', or a record in some languages. That is a good place to start.
An array is used to store a set of data elements with the same data type For example, if we want an array of five integers

A structure is a collection of one or more variables that store different type of data
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
There are other big reasons for using classes. Learn about data abstraction, inheritance, and polymorphism, which are generally considered the big three.
Java is programming language used to develop web application and making web sites such as ACC forum website. if i do google it gives me a lot off result and many confusen. I want to make it simple.

let me know if I am writing java code to develop web site. What happen if i use class Which part of website, class is responsible .What would be affect on website if i don't use class
 

WBahn

Joined Mar 31, 2012
30,058
An array is used to store a set of data elements with the same data type For example, if we want an array of five integers

A structure is a collection of one or more variables that store different type of data
That's the quick and dirty distinction. But now imagine defining a structure that holds all of the data needed to describe, say, a picture and you call this structure type PICTURE.

Now you define a bunch of functions that take a variable of type PICTURE as their first argument. One might be called save(), another clear(), another drawLine(), another getPixelGrayValue(), and another display().

When you use one of these functions, you have to pass it, as the first argument, an instance of a structure that has all of the data associated with a picture and then that function can perform the task it is supposed to perform on the data in that structure.

That's essentially what OOP is. It's just that an OOP language has the syntax to automate a lot of the bookkeeping and allow you to write the code in a way that strongly ties the functionality to the data instead of the normal non-OOP way in which we feed data to the functions. In non-OOP, the functions are the focus, in OOP, the data is the focus.
 

WBahn

Joined Mar 31, 2012
30,058
Java is programming language used to develop web application and making web sites such as ACC forum website. if i do google it gives me a lot off result and many confusen. I want to make it simple.

let me know if I am writing java code to develop web site. What happen if i use class Which part of website, class is responsible .What would be affect on website if i don't use class
You are needlessly trying to confuse two very different things. Java is an object oriented programming language. It is used for LOTS of things. It CAN be used to write web applications, but so can just about any other programming language. It can also be used for embedded programming, scientific applications, games, or almost anything else, but so can just about any other programming language.

The notion of a 'class' has NOTHING to do with a website (except to the degree that the person developing the applications chooses to design their classes to match their way of working with a website).
 

MrSoftware

Joined Oct 29, 2013
2,200
<...snip....> Java is an object oriented programming language. It is used for LOTS of things. <...snip...>
Just not things that run fast! :rolleyes: Sorry, I couldn't resist..

Think of "class" as a blueprint, and "object" as the house. You design the blueprint once, then use the "new" operator to create a copy of the house. If you want 10 separate houses of the same design you don't copy/paste your house class 10 times, instead you just call "new" 10 times. Now you have 10 identical, but completely separate house "objects". There's a lot more to it than that, but this is the most basic distinction I could think of.
 

WBahn

Joined Mar 31, 2012
30,058
Just not things that run fast! :rolleyes: Sorry, I couldn't resist..
That's a point I make to my students at several junctures in the course. I talk about the processing overhead associated with OOP (which, admittedly, has gotten a LOT better over time) and remark that the fact that they are using Java to begin with means that they've already accepted that performance isn't anywhere in their top-10 list of goals for that application. Fortunately, given the brute force available with today's machines, that's usually (not always -- another point I stress) a good tradeoff for all the other things that OOP brings to the table.

What amazed me when I first learned it was that the original application space for Java was specifically embedded systems. What were they smoking?! We are now seeing embedded-Java apps coming into their own, but it again required the availability of brute power in embedded processors well beyond what was available at the time they developed Java. I'm glad that it found an early home in web apps.
 
Top