04Oct 2019

What is Object-Oriented Programming Systems (OOPs)?

Object-oriented programming is one of the most popular concepts in the programming world. There are many programming languages that are preferred just because they use this concept. Most notably, Java and C++. Other languages such as python also have the concept of object-oriented programming. But what is object-oriented programming?

It is actually a programming paradigm that is based on the concept of objects. Objects contain data in the form of fields/properties/attributes and code, in the form of methods. Objects are very useful when it comes to data privacy. There are lots of features of object-oriented programming.

  • Inheritance
  • Polymorphism
  • Data Hiding
  • Encapsulation
  • Overloading
  • Reusability

OOPs in Java

OOPs in Java

Java is an object-oriented programming language that is class-based. Let’s understand the concept of objects and classes in Java with the help of an example.

// Dog class
class Dog{
        
        private String breed;
        
		// constructor of the class
        public Dog(String breed){
            this.breed = breed;
        }
        
        public void printBreed(){
            System.out.println(this.breed);
        }
        
    }
    

class main
{
    
  
	public static void main (String[] args) throws java.lang.Exception
	{
		
		// creating object of the dog class and passing a value to it's constructor
		Dog dog1 = new Dog("German Shepherd");
		
		// using the object to call a method of the dog class
		dog1.printBreed();
		
		Dog dog2 = new Dog("Labrador");
		
		dog2.printBreed()
		
	}
}

This is a simple example. We have a class named Dog. Its constructor takes a value and that value is stored in a private method. To get the value, we need to call the “printBreed” method.

Now observe the main class. There are two objects of the dog class – dog1 and dog2. Before going ahead, let me tell you, the output is –

“German Shepherd” is passed when the first object is created and “Labrador” is passed when the second object is created. The printBreed() method prints “German Shepherd” when the “dog1” object call it and it prints “Labrador” when the “dog2” object calls it. This is how objects work. Create as many objects of a single class and use each of them for different values.

This was just a simple example. There are a few more concepts that are immensely used in Java. They are the features we discussed above. Let’s understand them one by one.

Inheritance

Inheritance

Inheritance is one of the most important concepts of object-oriented programming. It is very useful when it comes to creating relationships among classes. As the name suggests, it is a mechanism in which one class inherits the properties and methods of any other class. Before going deep, let’s discuss some terminologies related to inheritance.

Super Class

The class whose properties and methods are inherited by another class (or classes) is known as a super class. It is also known as the parent class.

Sub Class

The class that inherits the properties and methods of another class is known as a sub class. It is also known as a derived class, extended class, or child class. The sub class can also have its own properties and methods.

Reusability

reusability

The concept of reusing the code is supported by inheritance.

Now, let’s understand what actually inheritance does. Suppose there is a class A and a class B. Here, class A is the super class and class B inherits from class A. Class B can use all the properties and methods of class B which are not private. But class A can’t. Let’s understand this with the help of an example.

//super class
class A{
    
    public int a = 10;
    
}

// sub class
class B extends A{
    
    public int getValue(){
        
        return a;
    }
}
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		B obj =  new B();
		
		System.out.println(obj.getValue());
	}
}

In the above java code, class A is the super class and class B is its sub class. We use the extends keyword for inheritance.

class B extends A{

Class A has a variable – ‘a’ that is initialed with value 10. In class B, the getValue() method returns the same variable. But no such declaration of this variable is done in class B. In the main class, we have an object of class B and it is calling the getValue() method. Let’s see if it results in an error or not.

Everything works fine! But how is it possible? There is no declaration of the variable in class B. It happens because of inheritance. Class B inherits everything from class A, thus the variable a is accessible in it.

Now observe the way we declared variable ‘a’ in class A.

public int a = 10;

It is a public variable. But if we change it to private, even the inheriting class cannot access it.

Polymorphism

The meaning of word Polymorphism is “Having many forms”. In real life, a man has many forms. He is a son, he is a husband, he is a father, all at the same time.

Polymorphism is also an important concept of object-oriented programming. It allows a programmer to perform a single action in multiple ways. There are two types of polymorphism in Java – Compile-time and runtime polymorphism.

Compile-time

The compile-time polymorphism is achieved by function or operator overloading.

Function overloading: Functions can be overloaded by giving a different number of parameters. The name will be the same but the number of parameters will differ. Observe the following example.

class A{
    
	// This method will be invoked when there are two parameters
    public int add(int a, int b){
        
        return a + b;
    }
    
	// This method will be invoked when there are three parameters
    public int add(int a, int b, int c){
        
        return a + b + c;
    }
    
}


class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		
	    
	    A obj = new A();
	    
	    System.out.println(obj.add(2,4)); // same method but two parameters
	    System.out.println(obj.add(2,4,6)); // same method but three parameters
	}
}

In class A, there are two functions with the same name, i.e. add. But the count of there parameters differ. In the main class, first, we passed two parameters to the add function and then three. It will choose the correct function automatically during compile time.

Operator overloading: Operator overloading is nothing but overloading variables with the plus (+) sign. Like we do concatenation of strings or adding two numbers.

Runtime

In runtime polymorphism, functions (methods) are overridden. If the super class, as well as the sub class, have a function with the same name, at runtime, the super class function will be overridden. Let’s understand this with the help of an example.

class A{
    
	// this method will be overridden
  public void print(){
      System.out.println("Super class");
  }
    
}


class B extends A{
    
     public void print(){
      System.out.println("sub class");
  }
}

class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    
	   B obj = new B();
	   
	   obj.print();
	}
}

class A is the super class and class B is its sub class. Both classes have a print function, each displaying different text. When the object of class B is used to invoke the print() function, the function of class A gets overridden, thus calling the function of class B itself.

Encapsulation

encapsulation

Wrapping up the data under a single unit is known as encapsulation. Encapsulation is very useful. It helps in protecting the data from being accessed from outside. In encapsulation, variables are hidden and cannot be accessed from outside class. The only way to access them is by using the method of the class itself. That is why it is also known as data hiding.

To use the encapsulation, declare all the variables private and use get and set methods (that are declared public) to access them. Let’s understand encapsulation with the help of an example.

class A{

    public int a = 10;    
    
    
}

class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
	
		A obj = new A();
		
		System.out.println(obj.a);
		
	}
}

In the above Java code, there is a class A. This class has a single variable – a, that is declared public. In the main class, we used the object of class A to access this variable. And this is not a good practice because anyone can access it. For encapsulation, let’s change this variable to private.

class A{

    private int a = 10;    
    
    
}

class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		
		A obj = new A();
		
		System.out.println(obj.a); // It will throw an error because a is private
		
	}
}

Let’s see what is the output now.

It throws an error because variable a is private and we cannot access it outside the class A. Access to this variable is limited and that is good, but what could we do when we need to access it from outside this class? We will use a public method.

class A{

	// private variable
    private int a = 10;    
    
	// method to return a
	public int getValue(){
	
		return a;
	
	}
    
}

class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		A obj = new A();
		
		System.out.println(obj.getValue());
		
	}
}

The getValue() method inside the class A returns the variable we need. It is a public method. Let’s see if this works.

Yes! everything works fine.

Abstraction

Abstraction is another important concept of object-oriented programming. It says, show only essential details to the user, only that is required, and hide everything else. To make an abstract class, use the abstract keyword. The abstract class can have an abstract method, constructor, and final methods. (Final methods are those methods which cannot be overridden)

abstract class A{

	public abstract void printClassName();
}

This is an abstract class with an abstract function. Note, there is no body of the abstract function. Now, if we will try to create an object of this abstract class, it will throw an error. The abstract class must be inherited from another class.

//abstract class
abstract class A{

	//abstract method
	public abstract void printClassName();


}


class B extends A{
    
    public void printClassName(){
        
        System.out.println("Class name is: B" );
    }
}
class main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		
		// Object of class B
	    B obj = new B();
		
	    // call the printClassName method
	    obj.printClassName();
	    
	}
}

Class B inherits from class A and the body of the abstract function is also present in it. This is how abstraction is achieved.

Conclusion

Object-oriented programming is very important as well as a useful part of Java. If you want to masterJava, Object-oriented programming is a must. A perfect Java developer needs to learn how to work with objects and classes. Inheritance may be complex (when gone deeper), but it is very useful. The same goes for polymorphism, encapsulation, and abstraction. But once you get familiar will these concepts, Java becomes more interesting.

Acodez is a leading website design and web development company in India. We offer all kinds of web design and web development services to our clients using the latest technologies. We are also a leading digital marketing company providing SEO, SMM, SEM, Inbound marketing services, etc at affordable prices. For further information, please contact us.

Looking for a good team
for your next project?

Contact us and we'll give you a preliminary free consultation
on the web & mobile strategy that'd suit your needs best.

Contact Us Now!
Jamsheer K

Jamsheer K

Jamsheer K, is the Tech Lead at Acodez. With his rich and hands-on experience in various technologies, his writing normally comes from his research and experience in mobile & web application development niche.

Get a free quote!

Brief us your requirements & let's connect

Leave a Comment

Your email address will not be published. Required fields are marked *