C does not include some features found in newer, more modern high-level languages, including object orientation and garbage collection.
From what I understand, C doesn't contain classes. Maybe you mean C++?
A class in Java - and presumably C# - is a collection of data (fields, parameters, and method variables) and logic (methods/functions and sometimes things like inner classes and other things you don't have to worry about yet) that describes the behavior of a particular type of object.
Let's say we have an application that schedules students in a university. A Student object would contain fields:
It would also contain methods (called functions in some languages). One method could print out all the data for a single Student object. The logic for that method would be included in the Student object itself. But why code this over and over again for every student? That requires work, and we are lazy programmers.
Hence we have classes.
In java, we would write a class like this:
public class Student
//Fields for objects of type Student
//Fields follow this convention:
//visibility_modifer return_type identifer;
public String name;
//^this is publicly visible, returns a string, and is identified as "name".
//public fields aren't a good idea because anyone can write a program to interact with it and change the state of this field.
private int age;
//^this is private (only visible within this class - only methods in this class can modify it or access it),
// it returns an int (a primitive, as opposed to an object), and it's called "age".
private String major;
//^note that String is an object in java. Objects can be told apart from primitives because primitives are always lowercase.
//Next we have the constructors, which are intructions on how to create an object of this type (the type being Student).
public Student()//Note that these parenthesis are empty - no parameters are passed to the constructor.
name = "";//Empty string is not null in java.
age = 0;
major = "";
//You can have multiple contructors in a single class provided they have different parameters.
public Student (String nameParameter, int ageParameter, String majorParameter) //note that we declare the types of these parameters -
//they are a sort of temporary variable.
name = nameParameter;
age = ageParameter;
major = majorParameter;
//Normally you wouldn't use such clunky names to identify the paramters; this is just to illustrate a point.
//now we need a way to modify the private Student variables, age and major. to do this we
//need 2 types of methods: accessor/get methods and mutator/set methods.
//Below are two accessor or get methods. They literally just return the value of an object variable. Their usage will be discussed in the main method.
public int getAge()
public String getMajor()
//Below are two mutator or set methods, so called because they mutate or set the state of the fields/variables in the object.
public void setAge(int newAge)
age = newAge;
public void setMajor(String newMajor)
major = newMajor;
public void printDetails()
System.out.println("Name: " + name);
System.out.println("Age: " + getAge());
System.out.println("Major: " + getMajor() + "\n");
//Now that we have everything done for our student class, we can create a main method to test it out. You can create a separate
//main class, or you can put a main method within another class. It really doesn't matter, just remember that your main method
//is your entry point for your program.
public static void main(String args)
Student xifanie = new Student("Xifanie", 22, "Computer Science");
Student nitwit = new Student("Nitwit", 25, "Computer Information Systems");
Student dummy = new Student();
//Hmm, this is kinda lame. Let's make it more interesting.
//Let's make an array of student objects.
Student cs160 = new Student;
//Let's add our three students to this array of Students. Arrays start counting at 0 in Java
cs160 = xifanie;
cs160 = nitwit;
cs160 = dummy;
//Now let's add some stuff to the dummy entry
cs160.name = "Ramza";
cs160.setMajor("l i t t l e m o n e y");
//Now let's actually use this array.
for (int i = 0; i < 3; i++)
The output of the main method is this:
Major: Computer Science
Major: Computer Information Systems
Major: l i t t l e m o n e y
Object oriented programming isn't as straight forward or as easy to learn as structured or procedural programming, but it can be easier to use once you learn how to use it. It's benefits lie in the simplicity of designing and implementing programs - you don't have to worry about naming conventions and really odd, obscure ways to get some function to work on all the members of a set of data, you only need to think about how they would actually work laid out as a series of interconnected and interacting objects.
Don't try to make sense of the code by itself, use an IDE or programmer's text editor with at least syntax highlighting.