Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help understanding OOP

Status
Not open for further replies.
Hey folks, I am attempting to learn Java after having a fairly decent grasp of BASIC, and the object-oriented concept is not very natural to me.

Can anyone point me to some resources that explain it well? Not YouTube please, (unless it's short), my bandwidth is limited.

Thanks
 
Microsoft basic? If you have toyed with VB3 and up.. The concept of OOP should be starting to take shape.

I think people that want to learn about objects, should code in visual basic for about a month... Once you start thinking that an object is the same as any other variable, you'll be well on your way.
I think books that try to explain OOP are a bit too intense. Back then, when I was learning, people would talk complete gibberish ( OOP jargon ) which completely baffled me..

Microsoft are the worst in the tutorial - learning department... I mean, in the first instance (no pun intended) they declare an object using uppercase THEN just to confuse.. they then call in instance the same name but in lower case..

Code:
CWINDOW cwindow;

People fresh in the field look at this declaration and go HUH???

Then they go farther

Code:
cwindow = new CWINDOW(window);

HUH??? The explanation isn't very heplful

The above is me having a bit of a rant, but it is a bit hard on newbies..

The best way is to start low.... Using types ( the basis of all OOP ) in "C" it moves up to structures and unions... then C++ it moves into objects...

I always tell people to think of an object

ie.. Lets take an LCD 16x2 as an everyday object... Think what it can do, what you need from it and what you need to do with it...

variables within LCD

Pins 14 to 20
backlight on/off
colour yellow/blue
size 16x2/20x2/20x4
etc..

you would write functions to enable all the variables to be changed ( correctly I should say, the whole idea of OOP )
You write an initialise function to start everything off..

There you have it all these above.. Shoved into a box that has inputs and outputs and control... Called LCD

To use it you declare (if you are microsoft )

Code:
LCD lcd = new LCD ( pins, backlight, colour, size);

But remember one thing..... OOP really was designed for security... Making code behave, If you pass 26 hours to a clock object it should ensure that its 24 + 2 thus updating the day and so on...
In the embedded world OOP is an overhead that really isn't necessary.

In your case JAVA.. Well, unfortunately, you have to use OOP... You could always have a play with C#, the syntax helpers are excellent in C# and its VERY much like JAVA.

This is my opinion and is open for discussion.
 
Hi,

OOP just takes the natural way of grouping things one step further and makes the code more reusable and easier to maintain.

When we have a class for example, we have a mini program that can do something useful, and that groups functions and data together.
For example, say we have the Button class available. This wraps all of the code needed to service button requests such as:
Button:FaceColor()
Button:SetText()
Button:TextColor()
as well as special create functions that help quickly create a new Button object such as:
hButton1=Button:Create("Start",Blue,White,10,10,50,30,"Courier New",16)
which creates a blue button with white lettering that reads "Start" and places it in the client area at 10,10 and makes it 50 pixels wide and 30 pixels high and uses the Courier New font with height of 16 pixels to write the word "Start" on the button face.
This class would also have an event such as "onButtonPress" where once it is clicked with the mouse the event routine is called and the button is serviced, which means it probably initiates some procedure that does something useful for the user.

One of the benefits is that each class can have the same function name that does the same thing in general for every class but because it is referenced as a class the particular class function is called. For example:
Window:Create(...)
Button:Create(...)
SingleLineEdit:Create(..)

All different classes yet all have a create function which creates that particular object.

Those objects were all graphical objects, but other types of objects are also possible such as memory objects...
Array1=Array:Create(1000) //create an Array object 1000 units long and call it Array1
Array1:SetValue(23, 3.14) //set a value in the array at position 23
y=Array1:GetValue(23) //retrieve a value in the array from position 23 (y will equal 3.14 if this code was executed)
Array1:Clear() //clear the whole array
Array1: Destroy() //remove it from memory

So the main idea is that you have 'objects' that you use in your program where those objects do something useful.
Once the class is written you can use it over and over again to declare those class objects. Recall that to declare a
variable you use something like this (C or C++ here):
long x;

and what that means is that you want to declare a variable x to be of type long and that means it stores a number in
a certain way taking up a certain number of bytes. Well OOP offers a class for other kinds of things too that of a much
higher level abstraction that makes it faster for a human to use such as the button above:
Button B1;

and now you have a button declared called B1 (although you usually have to set other parameters too using the
class functions). Note the similarity to declaring a variable above. And there can be many types of classes which
you could use:
Car C1,C2;
Truck T1,T2,T3,MyTruck;

and with that you are not only declaring objects of type Car or Truck but you also get a set of functions that go with it
such as:
Car Car1; //declare an object of type Car
Car1:SetColor(blue); //set the color to blue
color=Car1:GetColor(); //later, get the color to see what it is

Car Car2;
Car2:SetColor(red);

Sometimes you can find classes already written so all you have to do is learn how their functions work. For example a compiler made by Microsoft had what they called MFC or Microsoft Foundation Classes which gave you quite a bit of functionality without even writing any code.

One of the most useful classes i ever created myself was a string array class, which provides all of the interface functions needed to work with string arrays in a more human like form. This was in C++ but i've also done an entire class library in another more uncommon language.

In general OOP is made for large programs where maintaining the code gets much more difficult then with little simple programs.
 
Last edited:
Very nice insight MrAl... I suppose OOP has got it's place.... I love C# and Java, I find C++ is a tad over the top, especially where multiple inheritance is used.

But as I said... In the world of embedded systems, I wouldn't use it... Even if I program an Arduino I stick to C syntax..
 
Thanks for this, guys... I think what tripped me up the most was the idea of class objects... how can you treat a set of commands like a variable? The idea of storing and treating a sub program like a variable instead of a function. I'm sure I'll figure it out.

Yes, Ian, I was referring to MS BASIC. Pretty fun language, I learned it when I was in the 9-15 range, about 7 years ago I guess. I do understand the idea of sub programs, and user defined data types, but storing and treating them like variables in foreign to me.

And now that you guys mention it, the more I learn about Java, the less suited it seems to be for embedded applications. Yet I still see it being used for these applications on occasion.

Maybe I shouldn't even be learning Java, the main reason I am interested in it is because I really should learn a more modern and useful language than MS BASIC (lol) and I have a really old book on it. It seemed a decent fit, but only because I didn't feel like buying a book on C.
 
Hi there Res,

There is an intermediate language called "Euphoria" which allows simple programming like QBasic did way back when, but with advanced features like the ability to load a dll and call function within it. That means you can do Windows programming with that too which really opens up the world of programming at least on the PC. As far as microcontrollers go i dont think it is as suitable and i use plain old asm for that.
 
To learn OOP I suggest you study OOP theory without regards to any specific language. Once you understand the theory you can look at some language like C#. I do not recommend C++;

To get started with the theory follow this link
https://www.stanford.edu/class/cs193i/handouts2000/28OOPConcepts.pdf

Feel free to post questions regarding the various concepts.
 
Last edited:
Awesome, thanks 3v0... I will look through it.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top