Understanding basics of computer programming

In summary: Car! So, in Java, you might define a class like this:class Car{private String name;private int speed;private int location;private int fuel;private boolean started;public Car(String name, int speed, int location, int fuel, boolean started){this.name = name;this.speed = speed;this.location = location;this.fuel = fuel;this.started = started;}}This would create an object of the class whenever you tried to use one of its instance methods, like this:Car car = new Car("Jim", 50, 10
  • #1
Niaboc67
249
3
I am having some issues understanding the fundamentals of Java and OOP. In simplest terms possible with an example can you explain to me what an Object, Class and Method are? I am confused on what these actually are.

Thank you
 
Technology news on Phys.org
  • #2
I found the best way to do this is to have the definitions be done as if we were talking about people not computers. This is NOT rigorous. Think of it as maybe anthropocentric computing.

A class is a group of related java code (statements and declarations of data and data types) that handles something: example ask user a question. All of them are there there to help each other do a defined task: get you to answer one question for the computer in a way the computer wants it.

A method is a way to accomplish a very very dirt-simple sub-task. For the "class" above it might be thought of as "read a typed character" or move the cursor.

Object has a much broader abstract meaning. Objects have state. Objects have behavior.

State is something like "empty" or "full", or maybe yellow. Or a number like zero or one. State is composed of data and what the data means - an interpretation. Suppose the object asks you to enter a number: 1 or 0. When you enter 1 the state changes - the object no longer "wants" to read data from you. It likes the 1 and can use it. If you enter a "z" it will not take it.

Behavior is something that an object does - an action it can perform - like reading what you type and echoing it on the screen. It remembers what you entered, and checks that remembered data to see if you gave an acceptable answer.

So, a series of behaviors are closely linked to state. In fact behavior - what happens next kind of thing - is controlled by state and the code. Wrong state or awful code: program aborts or complains or displays angry faces. Programs that have unpredicted or undefined state are in serious trouble and the behavior is therefore completely unpredictable. Maybe the program will call the nearest pizza joint and order a giant anchovy pizza, just for you. More likely the program will display weird error messages from the operating system that have only a small chance of making sense to you.

Ov
 
Last edited:
  • #3
A class is (at least potentially) a set of objects which have the same sets of properties or atttributes (although usually with different values) and are capable of performing the same actions (represented by the class methods).

Consider a class named Car. All objects of this class might have a gas (petrol) tank, carrying some amount of fuel at a given moment; a speedometer showing how fast it is moving at that moment; an odometer showing how far it has traveled at that moment; a compass indicating which direction it is traveling; a GPS showing its current coordinates; and some descriptive information (make/model, color, year, owner, etc.). In C++ these properties are represented by the object's "member data." I don't know what term Java uses for them.

One car might be a 2013 Chevy Spark with ten gallons of fuel in its tank, moving at 50 mph east, etc.
Another car might be a 2010 Maserati with eight gallons in its tank, moving 90 mph south, etc.

They also can perform certain actions, or have certain actions done to them. For example, they can accelerate, turn through a certain angle left or right, cruise at constant speed for a certain period of time, etc. These are represented by methods which are associated with all objects of this class. (In C++, we call them "member functions.")

Methods can receive or send data from/to the "outside world", and may affect the object's member data. For example, a method named "Turn" might receive a number of degrees, +/- for right/left. myCar.Turn(+90) would change myCar's direction from east to south while keeping its speed the same. myCar.Compass() might return the current reading on its compass, which you can display as you like, or use in calculations.
 
  • Like
Likes FactChecker
  • #4
Adding to what jim mcnamara and jtbell have said, a class is a sort of template that describes the data (properties or attributes) of something, as well as the methods that can be used to alter the state of the thing -- i.e.set or modify or read its properties.

An object is an instance of a particular class.
 
  • #5
Mark44 said:
a class is a sort of template

Right; in C++ at least, when you define a class, something like this:

Code:
class Car
{
    // definitions of member data and member functions go here
};

you do not actually "create" any Cars. To do that, you have to declare variables of type Car, and usually provide initial values of some member data via a special member function called the "constructor." Then you can start doing things with it.

Code:
   Car mySpark ("Chevy", "Spark", 2013);
   Car yourMaserati ("Maserati", "somemodel", 2010);
   mySpark.addFuel (10);  // put 10 gallons in the tank
   mySpark.accelerate (60, 10);  // increase speed by 60 mph [presumably from 0 in this case] in 10 seconds
   yourMaserati.addFuel (8);
   yourMaserati.accelerate (60, 4);
 
  • #6
Basics of OOP for any OOP language.

A class is a description of something which has given properties (variables, states) which the class defines.
It is merely a description, not an actual existing 'thing'.
An object is an actual thing and is an 'instance' of such a class, (it exists in computer memory of course, it's not a physical instance of some thing).
Typically a program will process many objects which are instances of the same class.

Methods, otherwise called functions, are part of the description.
They describe in logic (code) what changes of state will occur given some specified input.
In programming these methods are 'called' upon, data is provided and they make those described changes of state happen to the object.
 
Last edited:
  • #7
@jtbell 's example of a class of cars is a very real-world example. If you were to do a simulation of cars driving through an intersection, you can do what he says. Define the class "car" and make a simulation of a lot of "objects", which are individual cars of different types that go through the intersection. Using class "methods", you define how a "car" can steer, brake, accelerate, see a traffic light, etc. Then every car object you create can use those methods to drive through the intersection. Object Oriented languages are set up to make all that easy and to allow the individual object cars to take care of themselves without getting confused with other object cars. And new car objects can be created and destroyed as the simulation goes on with them entering and leaving the intersection area.

There are many other situations where OOD is appropriate, but simulation is one where I think it fits very well.
 
  • #8
I'll build you up rather than trying to explain them all at once. I use C++, but it's about giving you the idea.

VARIABLE:
A variable is something that has actual value in memory. It can be something simple like just a byte, or a huge structure. All this actually does is provides a placeholder that tells the computer where in memory the item is stored, this is important later. A variable has to have a type and a name, below, the type is int and the value is 0.
Code:
int i = 0;

CLASS:
This is the simplest class that you can have, nothing in it. All this does is defines how a block of memory will look WHEN and IF it's instantiated.
Code:
class myclass {
};
It has no location, memory, nothing.

OBJECT:
This is how you create an instance of your class.
Code:
myclass myinstance;  //creates it in stack scope
myclass * myinstance = new myclass();  //creates it in heap
Now there is actually something in memory, you have a "myclass" in your memory. You'll notice that this is just like a variable, in fact, a variable can be an object, like you learned before a variable is just a location in memory.

MEMBER VARIABLE:
Okay, so now you have a class and can make an object, what good is that? Well the first iteration of heading towards OOP was simply called structs and they were what they sound like: structures of memory. A class can have lots of things associated with it, but they all have to fit inside of the object's memory. So when you access a variable inside of an object, your object gives you the location in memory of the object, and the member tells it where to get the particular field.
Code:
class myclass {
public:  //This means I can see the members on the outside
    int myintmember;
    bool myboolmember;
};

myclass myinstance;
myinstance.myintmember = 0;
myinstance.myboolmember = true;

CONSTRUCTOR / DESTRUCTOR:
Looks like a lot of work to have to first create the instance, then assign values to the members right? Well, you can do both at the same time. OOP languages have special functions called constructors and destructors that get called when you create and destroy an instance.
Code:
class myclass {
public:
    myclass() : myintmember(0), myboolmember(true){ }
    int myintmember;
    bool myboolmember;
}

FUNCTIONS:
Functions are also locations in memory, but they are used by the computer differently. This block of memory contains computer instructions. A function exists in it's own little world, outside of any classes and are usually accessed via code. You declare a function like this.
Code:
static bool myfunction(){
   return true;
}
and you call it like any other built in function
Code:
bool variable = myfunction();

FUNCTION VARIABLES:
You will not experience this in your first year or so of computer science, but it's important to know. A function has a location in memory where the instructions are stored, well, that's exactly like every other variables and as long as you know what type your function is, you can actually assign it to a variable.
Code:
typedef bool (*myfunctiontype)();
myfunctiontype myfunctionvariable = myfunction;  //create a variable pointing to the function
bool variable = myfunctionvariable();  //use the variable like a function

MEMBER FUNCTIONS:
You probably know that classes can have functions that allow them to do things that are specific to your class type.
Code:
class myclass {
public:
    myclass() : myintmember(0), myboolmember(true){ }
    void increaseInt(){ myintmember += 1; }
    int myintmember;
    bool myboolmember;
}

myclass myinstance;
myinstance.increaseInt();  //increases the int value for this object only
This member function is exactly the same as any other function, so how does it know what object it's working on? Does it copy the function for each object? Of course not, that'd be horribly inefficient, what's actually happening is the compiler is making an increaseInt function with one parameter, which is the object (inside the function, it's called this.)

INHERITENCE:
What happens when you want to classes to act almost the same, but have a special case for one of them or you want a few extra pieces for a particular subset of the class. You can have one class take all of the properties of another.
Code:
class myclass {
public:
    myclass() : myintmember(0), myboolmember(true){ }
    void increaseInt(){ myintmember += 1; }
    int myintmember;
    bool myboolmember;
}

class mysubclass : public myclass {
public:
    mysubclass() : myclass(), myfloatmember(1.f) {}
    float myfloatmember;
}

myclass myclassinstance;
mysubclass mysubclassinstance;
mysubclassinstance.myfloatmember = 2.f;  //This one has an extra member
mysubclassinstance.increaseInt();  //It also has all the properties of it's parent

POLYMORPHISM:
This is the thing that makes OOP so powerful. In the myclass example, myclass has a member function called increaseInt, what if I wanted that to do something slightly different for mysubclass? Well, remember, functions can be variables, you can change a variable (well, the compiler can, it's in a special structure called a vtable.)
Code:
class myclass {
public:
    myclass() : myintmember(0), myboolmember(true){ }
    virtual void increaseInt(){ myintmember += 1; }  //virtual means it's going to be a variable
    int myintmember;
    bool myboolmember;
}

class mysubclass : public myclass {
public:
    mysubclass() : myclass(), myfloatmember(1.f) {}
    virtual void increaseInt(){ //this replaces the increaseInt function variable for all objects of this type
        myintmember += 1;
        myfloatmember += 1.f;
    }
    float myfloatmember;
}

myclass myclassinstance;
mysubclass mysubclassinstance;
myclassinstance.increaseInt();  //Calls the parent's function
mysubclassinstance.increaseInt();  //Calls the subclass's function
 
Last edited:
  • Like
Likes WWGD and Mark44
  • #9
Wiki articles:

http://en.wikipedia.org/wiki/Object_(computer_science)

http://en.wikipedia.org/wiki/Class_(computer_programming)

http://en.wikipedia.org/wiki/Method_(computer_programming)

Sometimes you see statements about procedural versus object oriented languages, but the methods (functions) used in most object oriented languages are procedural, so the difference is mostly how functions get invoked. Many programs like Windows apps are event driven: they receive a stream of messages where each message contains an event type, and each event type can have it's own method (function).
 
  • #10
#!/usr/dev/bin/
$class = BlueprintOfHouse; $object = House; $method = DoorbellOnHouse
 
  • #11
Niaboc67 said:
I am having some issues understanding the fundamentals of Java and OOP. In simplest terms possible with an example can you explain to me what an Object, Class and Method are? I am confused on what these actually are.

Thank you

It's been a while, but...

A Class is a little software package with the interface to the package carefully specified. It may have some private data.

A Class has Methods, which are executable subroutines with there calling sequence specified in the Class.

An Object is an instantiation of a Class. That is, it is a piece of private data that can be accessed only through the Methods specified in the Class interface. An object can be created, copied, destroyed, saved to disc, read from disc, and possibly modified.
 
  • #12
Hornbein said:
It's been a while, but...

A Class is a little software package with the interface to the package carefully specified. It may have some private data.

A Class has Methods, which are executable subroutines with there calling sequence specified in the Class.
A class can also have data, AKA properties, that specify the state of a given class instance.
Hornbein said:
An Object is an instantiation of a Class. That is, it is a piece of private data that can be accessed only through the Methods specified in the Class interface.
The data does not have to be private -- public data (which is usually not a good idea) can be manipulated directly through an object, rather than by calling methods on the object.
Hornbein said:
An object can be created, copied, destroyed, saved to disc, read from disc, and possibly modified.
 
  • #13
Mark44 said:
A class can also have data, AKA properties, that specify the state of a given class instance.
The data does not have to be private -- public data (which is usually not a good idea) can be manipulated directly through an object, rather than by calling methods on the object.
Yah. I hate public data.
 
  • #14
Niaboc67 said:
I am having some issues understanding the fundamentals of Java and OOP. In simplest terms possible with an example can you explain to me what an Object, Class and Method are? I am confused on what these actually are.

Thank you
Most of the answers here seem pretty technical to me. Let me try a simpler version:
I like to think of a class as a pad of blank forms, like those pink pads that used to be common for taking phone messages. The form has spaces to write in information like Caller's Name, Phone Number, Message, Best Time To Call Back, and the Name Of The Person Who Took The Message. This pad of blank forms can be called the class named "PhoneMessage".

When a call comes in, the phone operator tears a blank sheet off this pad and creates a new instance of the class PhoneMessage by writing in the information in the provided spaces. This piece of paper is now an object, and there can be any number of them. The different information items like Caller's Name, Phone Number, etc. are called instance variables (or more generally, members). The values of these instance variables define the state of that specific object.

Unlike pink pieces of paper, however, classes (and the objects made from them) can also have behavior. Imagine that the phone message form was very high-tech and had little checkboxes on it labeled "Return Call" and "Pass The Buck" (followed by a space to write in the name of the unlucky person you are handing off the call to). Checking the first one would cause your phone to dial the caller's number. Checking the second one (after writing in your associate Joe's name) would cause the piece of paper to magically fly over and deposit itself on Joe's desk.

In OO terms, "Return Call" and "Pass The Buck" would be called methods, which define the available behavior of PhoneMessage objects.

One other term you'll run into: a constructor is simply a special routine you call that creates a new instance of the class. In the above example, in Java you could write:

PhoneMessage myNewMessage = new PhoneMessage( "Lisa Marks", "375-0901", "I've fallen and I can't get up", "3:00PM", "Joe");

to create a new PhoneMessage object with the specified instance variable values, and give it the name "myNewMessage".

Hope this helps!
 
  • #15
Try it in your programming language(see some examples ,play with them) . If didnt understand read and try again and again until you understand
If you keep trying to look for a good explanation you probably won't find it
 
  • #16
AliGh said:
If you keep trying to look for a good explanation you probably won't find it
The OP asked for an explanation of the terms object, class, and method. A number of people gave some good explanations right here in this thread.
 
  • #17
Mark44 said:
The OP asked for an explanation of the terms object, class, and method. A number of people gave some good explanations right here in this thread.
I meant an explanation that makes him comprehend it (meant no offense)
Your right my mistake i didn't think about it , i just said something based on my experience
I suggest the OP to keep reading until he can fully understand OOP programming and java
 
  • #18
AliGh said:
I meant an explanation that makes him comprehend it (meant no offense)
No problem -- I wasn't offended by your comment.
 

What is computer programming?

Computer programming is the process of creating computer software using a programming language. It involves writing code to instruct a computer to perform a specific task or set of tasks.

What are the basic concepts of computer programming?

The basic concepts of computer programming include variables, data types, control structures, functions, and algorithms. These concepts are essential for writing code that can be executed by a computer.

What is the difference between a programming language and a programming paradigm?

A programming language is a set of rules, symbols, and syntax used to write code. A programming paradigm, on the other hand, is a style or approach to programming that dictates how code should be written and organized. Some common programming paradigms include imperative, object-oriented, and functional programming.

How do I choose the right programming language for my project?

The right programming language for your project will depend on the type of project, your skill level, and the resources available. Some factors to consider when choosing a programming language include its compatibility with the platform you are using, its community support, and its learning curve.

What are some common mistakes to avoid when learning computer programming?

Some common mistakes to avoid when learning computer programming include not practicing enough, jumping into complex projects too quickly, and not understanding the fundamentals. It is important to start with simple programs and build upon your knowledge gradually. Additionally, don't be afraid to make mistakes and learn from them.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
24
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
2K
  • Programming and Computer Science
Replies
32
Views
5K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top