Learn Object Oriented Programming with Udemy

learn object oriented programmingBack in the earlier days of computer programming, code was written structurally or linearly. Languages that were used to write those blocks of code were called structural or linear programming languages. C is one of the prime examples of a structural language where all the code is written linearly without much support for code reusability and organization. Structural languages were good enough to support basic programming applications and with some effort they could be extended to program complex applications as well. With the passage of time, software applications grew more and more complex and programmers realized that linear programming languages were not alone sufficient to implement the desired functionality. For this purpose, object-oriented programming paradigm was introduced.

To learn more about OOP Java tutorials, check out a course at Udemy.com.

What is Object Oriented Programming?

Object-oriented programming (OOP) is a computer programming paradigm where a software application is developed by modeling real world objects into software modules called classes. Consider a simple point of sale system that keeps record of products purchased from whole-sale dealers and the products sold to the customer. An on object-oriented language would implement these requirements by creating a Product class, a Customer class, a Dealer class and an Order class. All of these classes would interact together to deliver the required functionality where each class would be concerned with storing its own data and performing its own functions. This is the basic idea of object-oriented programming or also called OOP.

Advantages of OOP

Some of the advantages offered by object oriented languages have been enlisted below:

  • OOP improves code reusability and organization using inheritance.
  • OOP supports increased data security using encapsulation.
  • Object oriented code is more logically packaged owing to its support for polymorphic objects.
  • Code readability is much improved in case of OOP when compared to older programming paradigms.

Basic OOP Features

Basic features of object oriented programming include:

  • Inheritance
  • Encapsulation
  • Polymorphism.

Important OOP Terms

This section includes some of the most basic terms used in OOP.

  • A Class

In OOP, a class refers to anything that has some properties and can perform some functions. For instance, in the aforementioned scenario of point of sale system, a customer is a candidate for being implemented as class. A customer can have properties like social security number, name, DOB, address etc. And a customer can buy products and can also pay a bill. Therefore,  a “Customer” can be implemented as a class. The following example demonstrates how to declare a class. The first code explains the process of creating a class in Java and the second code demonstrates a C# class. (Both Java and C# are fully object oriented languages).

Java Code:

public class Customer {

int SSN;

String name;

String city;

}

 

C# Code:

class Customer

{

int SSN;

string name;

string city;

}

 

 

You can see from the above two code snippets that the process of creating a class is almost similar in Java and C#. In fact, most of the programming languages on the market today have adopted this syntax for defining a class in a program.

Interested in learning OOP in C#? Take a Udemy.com course.

  • Methods/ Functions

Methods, also known as functions in some languages are the functionality that a class can perform. In the aforementioned definitions of classes in Java and C#, there were only attributes, also known as properties. But classes are much more complex than that. The real power of classes comes with their ability to perform functionality. This functionality is encapsulated in the form of methods or functions. The following example demonstrates how a method is declared inside a class body. The Customer class defined in the last section has been extended to include buyItem and payBill methods. Have a look at the following Java code snippet:

public class Customer {

int SSN;

String name;

String city;

public void buyItem()

{

System.out.println("Customer has bought a product");

}

public void payBill()

{

System.out.println("Customer has paid the bill");

}

}

Two methods, namely buyItem and payBill, have been included in the class. A method definition entails defining three things: the name of the method, a scope that specifies who can call the method, the value returned by a method that is void in both of the included methods, which means that methods do not return anything to the calling function.

To call a method from a function, instantiate the class whose method has to be called and append the method name to the class object separated by a dot.

Instantiating a Class

A class only provides a blue print and takes no space in computer memory. Rather, the objects of the class are what take actual space in the memory. The space taken by an object in the memory is roughly equal to some of the space occupied by all the member variables of the class. Creating an object of a class, also called instantiating a class, is done by calling the constructor of the class. A constructor is basically a method with the same name to that of the class and a constructor that has no return type.  The following example demonstrates how a class is instantiated in Java:

Customer c = new Customer();

Customer c2 = new Customer();

To create an object, the class name followed by object name, is assigned a value by using “new” keyword followed by the constructor of the class. The process of creating a class object is similar to Java. Keyword “new” basically tells the compiler that a new memory has to be created for the object. One class can have virtually unlimited objects.

Calling Methods

Calling a method is extremely simple process in both Java and C#. The instance of a class is used followed by a dot operator and the method to be called. For instance, to call payBill method of the Customer class defined above, the following code snippet can be used:

Customer c = new Customer();

c.payBill();

Methods might not look like much use at the moment since they are doing nothing except displaying a string message on the screen. However, in real world scenarios, methods encapsulate much complex scenarios and are extremely helpful.

To learn more about object-oriented programming in Java, take an online course at Udemy.com.