Learning java trouble

spinnaker

Joined Oct 29, 2009
7,830
These videos teach the basic concepts of java like inheriting, polymorphism and overriding, etc.
The problem is I can't understand the purpose of these concepts and don't know how would I need them in a real operating program(e.i. I am missing the whole picture).
Need some help!
If you don't understand their purpose then how do you know if you would need them or not?

Well of course you don't need them. People have been programming without OOP for years and still do.

OOP is sort of like power windows in an automobile. You don't need power windows in an automobile but once you have them and learn to use them you will never want to do without them.
 

MrSoftware

Joined Oct 29, 2013
2,202
To get the understanding that you seek, you will need to write lots of code. As you write, and find yourself solving various problems along the way, the advanced features of the language will start to make sense.
 

MrAl

Joined Jun 17, 2014
11,496
Hi
I started watching tutorials to learn java from this channel https://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28
These videos teach the basic concepts of java like inheriting, polymorphism and overriding, etc.
The problem is I can't understand the purpose of these concepts and don't know how would I need them in a real operating program(e.i. I am missing the whole picture).
Need some help!
Hi,

Object oriented programming is helpful when programs start to increase in size. The main idea is encapsulation. The aim is to lump things together so that we can handle them as one kind of object. So now you have another term to deal with :)

If you look back at how we as a civilization started doing things in math, you'll find that we started counting number, like 1,2,3 and then added like 1+1=2 and so on, and then somewhere along the way we found variables like a,b,c and x,y,z, etc.

Sometime after that we started programming, and that meant we needed to define types. A type is a kind of variable that has a certain property that allows it to be used in a certain way. So we might have a,b,c be all of one type, and x,y,z of another type. Now if a,b,c are type A and x,y,z are of type B, we might have two expressions like this:
A a,b,c;
B x,y,z;

So we are declaring that we will have variables, but also what type they are. The types in real life may be integer or float for example.

We also end up with functions, that are life variables but they perform a certain task. We call one when we need something done, and that something almost always depends on private data as well as local data and possibly global data.

So we have functions, and the data that the functions operate on, and we have types that declare what kind of variables we are using.

One of the main points is that when we need a variable like x we must declare it for example:
integer x;

and what this means is that we can declare a whole bunch of variables, a through z for example.

The question then is, what comes next? We progressed from very basic things to more complex things, and each time it helped us in programming.

We ended up with functions and data, and the data itself is always associated with the function because that's what it is there for. So the logical way to progress is to combine the function with the data. That then becomes known as a Class.

So now we have variables that can be a given simple type like integer or they can be a whole class. So if we have a class called MyClassA now we can declare a function and it's data:
MyClassA x;

and now 'x' stands for a class that could have several functions and data associated with it.

After that we might find that we have to modify that class for some other purpose but still need the old class too, so we modify it and call it something else. That means that the new class is derived from the old class, and inherits properties of that old class as a result.

A function override is just when we have a function like MyFunc(integer x) and we find out that we want that same function to take a float and want it to handle that differently, so we override the function MyFunc with the addition definition MyFunc(float x). Now we can do these:
MyFunc(45);
MyFunc(3.14159);

and they both work but can also do slightly different things if needed.

Out of everything though the most important point is encapsulation. That allows us to handle complex pieces of code almost like we use variables. A class encapsulates functions and data so that we can use it more easily. In fact, a program written as a regular program is hard to incorporate into another program for use within that program, but a class is rather easy to use. For example, if you write a program for an analog clock that displays on the desktop, it could be very hard to use that in a new program that you write where you want a clock to appear in the upper right hand corner of the program window. If it is written as a class however, all you need to do is declare it:
Clock MyClock;

and of course there would be some variables to deal with to tell it where to display in the window (you would then call the constructor of the class), but that's the main idea.


Now maybe you started with a calendar program, then later decided to add a clock. You should be able to do this rather easily if you already have the clock class written. If the clock was written as a single C program however, it would take a lot of changes to get it into the calendar program, unless you followed a strict methodology when you wrote the clock program (which is possible but generally not as neat).
 
Top