Heh, this is like taking the red pill. It's true, OO is something that most people don't really get, even working in the industry for a long time, so don't worry that it's gonna take a while to sink in.
Function vs Method:
The difference between a method and a function is small, but significant. A function is a piece of code that can be called by name to perform some task. It may be passed data on which to operate (i.e. void foo(int x)) and it may return data (i.e. int foo(int x)). All data for the function to operate is passed into the function.
A method is also a chunk of code that is called by name, but it is a function that is associated with an object, a method is part of an object's identity and behaviour. This is important because in addition to the information passed into a method, the method has implicit access to all the rest of the data and methods of that object.
In C there are only functions, no methods. In Java (and C# I think, but i'm not sure) there are only methods, no functions. In C++ there are both.
How does this relate to OO? In a non OO world, if you wanted to have a register function for various animals, you might have to do this:
Code:
Dog dog = new Dog();
Cat cat = new Cat();
register(cat);
register(dog);
But your register method would have to know how to register both dogs and cats, and if you added a Bird type you'd have to change your register method again, the register code would have a bunch of conditional statements and be brittle and difficult to maintain.
In OO, you would instead have a register method:
Code:
Dog dog = new Dog();
Cat cat = new Cat();
cat.register();
dog.register();
So the cat and dog would know how to register themselves.
Class vs Object:
A class is a definition of an object, an object is an instance of a class.
A class is the code that defines the methods, properties, interfaces, etc of whatever type it is you are creating. So I would have a Cat class file that would define the register method, as well as the cat's name, age, date of birth, colour, whatever else it needed.
Code:
class Cat
{
string name;
public void speak()
{
Console.WriteLine("Meow");
}
public void register()
{
// do stuff
}
}
An object is an actual instance of that class.
Code:
Cat cat = new Cat();
cat.register();
cat.speak();
The variable cat contains a reference to an object in memory that is constructed based on the Cat class.
Public / Private, this gets into scope, and scope can be complex and different for different languages, but from a high level as you say private means private within the scope, and public means accessible to scopes outside.
So if in our example the register method was public, I could call the register method as I did above. If the speak method was private though, I could not, it would not be visible. I could only call the method from within the class itself. This is useful if you want to have utility methods inside the class that aren't part of the class's interface with the rest of the world.
Static means that the thing it's talking about is visible at compile time rather than runtime, or rather that it's at a class level rather than an object level.
What does that mean? In order to give a cat a name, first I have to instantiate a cat object, and then assign a name to it. Every cat object can have a different name, and the idea of a name at the class level doesn't make any sense, since a name is a property of a cat object.
But sometimes you want to define something at a class level, something that is common to all cats.
Code:
class Cat
{
private static final string species = "Felis Catus";
private string name;
public string getName()
{
return name;
}
public static string getSpecies()
{
return species;
}
}
So now I put in a species property that is public, accessible by anyone, and is static meaning I can access it without instantiating an object. I can't get the name without first creating a cat object and assigning it a name, but I can get the species.
Code:
Console.WriteLine("Species: " + Cat.getSpecies());
Cat cat = new Cat();
Console.WriteLine("Name: " + cat.getName());
Notice I called the getSpecies method with a cat with a capital C, meaning I'm calling the method on the class, not on an object.
void as you say just means no type.. if you declare a method as void it means the method won't be returning anything when it finishes.
Ignore the case conventions in my code, I'm a long time removed from C# and my case conventions are mostly from Java.