fuddlepixelFuddlepixel coz tech is fun();

Fundamentals of object oriented programming in a simple way!

Java is a pure object oriented programming language. Understanding fundamentals of object oriented programming is necessary to take any further step.

By Shubham Ramdeo
19 July 2018 · 6 mins read
# cpp  java  oop 

Fundamentals of object oriented programming in a simple way!

Java is a pure object oriented programming language. Before we begin installing and writing Java, understanding fundamentals of object oriented programming is necessary to take any further step.

In this article, we will go through a brief introduction about object oriented concepts and fundamentals. These concepts are commonly applicable to any other object oriented programming language.

Languages like C are imperative languages. With each instruction, through some restricted control flow, they change the state of variables in one or the other way, as we command. They are more about how a computer thinks like.

Object oriented concept takes a different approach. They try to implement how a human thinks like. The object oriented model is based on how we can create entities called objects which are similar to day-to-day objects and their behavior in real life.

Object

Let us consider a very simple object, a car.

First of all, it has certain properties.

  • Red
  • Ford
  • Vehicle number 12345

Now, it can also have certain actions.

  • start
  • stop
  • playhorn(for 2 seconds)

So an object seems to have two things, properties, and methods.

We can have other objects too.

Let’s have another car,

  • Blue
  • Tesla
  • Vehicle number 56789

performing same things

  • start
  • stop
  • playhorn(for 2 seconds)

So we have two distinct objects now.

We can easily understand that object is an entity containing properties and functions (methods).

Class

Even though we see Tesla and Ford cars above are two different entities, they seem to be have something in common. That’s because they both are Cars!

This is called a class. Class is a general implementation for objects, sharing common properties and methods.

Just like, a car is class. Every car has a brand name and color. Every car can start and stopped. So the above tesla and ford objects belong to car class.

Though they are different objects because of having different properties, one is red, one is blue, but as a car, they share the same fate!

Another example, say in a school. there are different types of students. They have different names, different height, different courses they are taking. But they all share one thing, that they are students. They have a name, height and they do undergo certain courses.

So here, the student becomes the class, while each distinct student is an object of the student class.

Visibility

When an object is made, it has certain information that can be known to everybody. While some information is hidden that should only be known to the object.

For example, take a student. Student’s name is visible to everyone. That is, his name is public. But there are some things that a student would like to keep secret, say his email address, he wants it to be private, not visible to anybody.

Then we can say that his name is a public property, while the email address is a property.

Then how will we know the email if its private? Simple, ask him! We can have a member function, or method, called askEmail, which when called for, will provide us the private information if it finds it right! (you cannot give your email address to anyone).

To access a property, usually, most of the languages use the Dot (.) Operator. So say if I am Mike, my full name can be accessed as Mike.fullName. But when we do something like Mike.emailAddress, its wrong, because only Mike knows his email address, so we have to ask himself.

Inheritance

This is another most important feature of Object Oriented Approach. That we can inherit the properties of a class to make a new one. Say, keeping the same example of Student, first there is a general Student as we have implemented. Having a name and email address.

But there can also be College Students who get semester, course, and they also have college email address. And School Students who have grades.

Do we have to make new classes for school and college students distinctly? No! We will use the main Student class, copy its properties or inherit it, and add new properties as required.

So the student already had:

  • full Name
  • email Address

A School Student will have:

  • both properties of Student (full name and email)
  • an Email asking function from Student
  • grade [NEW]

A College Student will have:

  • both properties of Student (full name and email)
  • an Email asking function from Student
  • College Email Address [NEW]
  • semester [NEW]
  • course [NEW]

That’s a simple thing is called Inheritance.

There are a lot of benefits! Whether a student is from college or School, we can still use the askEmail method to ask a student his email address!

Polymorphism

The word poly and morphism means multiple ways to provide. Let’s understand this with an example. So as we said above that a college student has two email addresses.

When we ask a college student to give his email address, he will give his personal one. So we have to ask him specifically to give his college email address.

In object oriented programming, we can do the same thing.

A college student already had a method (inherited from a general Student) that on calling askEmail(), he will give his email address.

We can have another method by the same name askEmail, with a different rule. Say askEmail(college). This will provide his college email address instead of personal.

So we have a method, askEmail, and this single method can do two different things based on the given conditions.

It might seem a bit tricky so let’s try to play with some pseudo code.

Let’s add up all

First of all, a class.

The below code is NO LANGUAGE! It’s just a pseudo code to help you understand the concept.

//Psuedo Code!!!

class Student {
    public fullName;
    private emailAddress;

    public askEmail(){
        say(emailAddress);
    }
}


class SchoolStudent: inherent from Student {
    public grade;
}

class CollegeStudent: inherent from Student {
    public course;
    public semester;
    
    private collegeEmailAddress;
    
    public askEmail(college){
        say(collegeEmailAddress);
    }
}

Now let’s make some objects.

//Psuedo Code!!!

SchoolStudent mike;
CollegeStudent john;

mike {
    fullName = Mike Milton;
    Address = mike@nail.com;
    grade = 6th;
}

john {
    fullName = John Jackson;
    Address = john@nail.com;
    semester = 4th;
    course = computer science;
    collegeEmailAddress = john4@clg.ac.xz
}

Now let’s access to some properties.

//Psuedo Code!!!

say (mike.fullName);
-> Mike Milton

Say (mike.emailAddress);
-> ERROR: You are not authorized to use private information.

mike.askEmail();
-> mike@nail.com 

say (john.semester);
-> 4th

john.askEmail();
-> john@nail.com 

john.askEmail(college);
-> john4@clg.ac.xz

The object oriented system is really made to think like humans. It’s abstract art! isn’t it?

This seems to be a very long article. Try to understand the concept. From the next article, we will setup Java into our system and then dive into it.

Until then, keep learning programming from zero at fuddlepixel.com


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

cpp 

java 

oop 

Top