What is Java and Object-Oriented Programming | Learn Java Skills

Imran Shaikh
0

Java is a high-level object-oriented programming language, developed by the Sun Microsystems in the year 1995 and later java, acquired by the Oracle.


Java mainly revolves around two concepts i.e. classes and the objects, along with its methods and variables. In real life example, a Bike is an object, the speeds of the bike are a method and color is a variable.

What is java and Object-oriented programming thumbnail
toc

Features of the Java


The following are some features of Java.


Object-oriented programming language

Java is an object-orient programming language. Still, because of the primitive data types in java, Java is not a 100 percent object-oriented programming language. We will learn object-oriented programming in deep, later in this article.


Easy to learn

If you know the object-oriented program concepts, then java is simple and easy to learn.


Platform-Independent

Because of the JVM (Java Virtual Machine), Java is a platform-independent language. Java is also known as Write once and run anywhere. To run a java program, you only need JRE (Java Runtime Environment). So, it makes the java to platform-independent.


Multithreading

Java supports multi-threading, It means you can write a multiple thread program in java. The main advantage of multithreading is to handle all the core of the machine at the same time. In java, you can write programs to runs parallel.


Object-oriented programming has four main concepts.


  • Inheritance
  • Abstraction
  • Encapsulation
  • Polymorphism

Inheritance


Inheritance in java means reusing the behavior of the existing(parent) class. The inheritance in java is one of the most important concepts of the Object-oriented programming language (OOPs).


The inheritance in java is design with the intension to use the methods and fields of the parent class in the child class. With the help of the extends keyword, we can implement inheritance in java. In java, The extends is the keyword which allows other class to use parents class properties.


The following is the example of single level inheritance in java.



Java program for Inheritance example

    		  public class Car
  {
      public int nummberOfWheel = 4;
  }

  public class BMWZ4 extends Car
  {
      private boolean isAutodive = true;
      public static void main(String[] args)
      {
          BMWZ4 bMWZ4 = new BMWZ4();
          System.out.println("Number Of wheel = " + bMWZ4.nummberOfWheel);
          System.out.println("Is BMWZ4 auto dive = " + bMWZ4.isAutodive);
      }
  }

Output of the inheritance java program

Number Of wheel = 4
Is BMWZ4 auto dive = true(code-box)

In the earlier java program example, we have seen that the BMWZ4 class is using his field i.e isAutodive as well as the Car field i.e nummberOfWheel. This is also an example of reusing code.



Abstraction


Abstraction is nothing but it is a way of hiding the data from end-user, means showing only those component to end-user which they need and rest (the implementation) will be hidden from them.


In the real-world example let's have a car, Cars individual components such as break, engines, and the fuel tank are hidden inside of the car and it's hidden from the end-user how they work. To Achieve abstraction in java we can use two keywords Abstract and Interface.


The following is a simple example of abstraction using Interface in java.


Java program for Abstraction example

        	  public interface Car
  {
      void carBreak();
      void engine();
      void fuelTank();
  }

  public class BMWZ4 implements Car
  {
      public static void main(String[] args)
      {
          BMWZ4 bmwz4 = new BMWZ4();
          bmwz4.carBreak();
          bmwz4.engine();
          bmwz4.fuelTank();
      }

      public void carBreak()
      {
          System.out.println("car break");
      }

      public void engine()
      {
          System.out.println("engine");
      }

      public void fuelTank()
      {
          System.out.println("fuealTank");
      }
  }

Output of the abstraction java program

car break
engine
fuealTank(code-box)

Encapsulation


The encapsulation in java is nothing but wrapping the data (variables and methods) in a single unit.


The idea behind encapsulation is the variable of java class should not be accessible directly from other java classes.


To use encapsulation in java make all fields (variable) private access modifier (If you want to learn more about Access modifier in java then, let me know in the comment section) and create a setter method to set the value and getter method to fetch the value.


The following is a simple example of the encapsulation in java.


Java program for encapsulation example

    		  public class Car
  {
      // private fields
      private String engine;
      private String fuel;
      private String carBreak;
      public String getEngine()
      {
          return engine;
      }
      public void setEngine(String engine)
      {
          this.engine = engine;
      }
      public String getFuel()
      {
          return fuel;
      }
      public void setFuel(String fuel)
      {
          this.fuel = fuel;
      }
      public String getCarBreak()
      {
          return carBreak;
      }
      public void setCarBreak(String carBreak)
      {
          this.carBreak = carBreak;
      }
  }

  public class BMWZ4 
  {
      public static void main(String[] args)
      {
          Car car = new Car();
          car.setEngine("3.0L N55 Turbocharged DOHC I-6 ");
          car.setFuel("Diesel");
          car.setCarBreak("High performance break");

          System.out.println("car engine = " + car.getEngine());
          System.out.println("car fuel type = " + car.getFuel());
          System.out.println("car break type = " + car.getCarBreak());
      }
  }

Output of the encapsulation java program

car engine = 3.0L N55 Turbocharged DOHC I-6
car fuel type = Diesel
car break type = High performance break(code-box)

In the above java program example, we have created two classes such as Car and BMWZ4. Car class is the example of encapsulation, here the field of the car such as the engine, fuel and the car break are private so, that other classes can't access directly to these variables and fields.


To access these fields in other classes we have created a setter and the getter method for each private field(variable).


The BMWZ4 has created an object of the car by using, Car car = new Car() and then calling the method of Car using car object.


Polymorphism


The term Polymorphism means "many forms". In Java, there are two types of polymorphism.

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time Polymorphism


The compile-time polymorphism is achieved by using Method Overloading. The term method overloading means, two or more methods have the same name but have a different argument or have different numbers of arguments.


Method overloading java program

          public class Calculation
  {
      public int sum(int a, int b)
      {
          return a + b;
      }

      public int sum(int a, int b, int c)
      {
          return a + b + c;
      }

      public static void main(String[] args)
      {
          Calculation calculation = new Calculation();
          System.out.println("sum method have two arguments = " + calculation.sum(1,2));
          System.out.println("sum method have threee aruguments = " + calculation.sum(1, 2, 3));
      }
  }

Output of the method overloading java program
sum method have two arguments = 3
sum method have threee aruguments = 6(code-box)

There are two methods in Calculation class However both have the same name method i.e sum but have a different number of arguments.


In the first sum method, it has two arguments i.e int a and int b.In the second sum method, it has three arguments i.e int a, int b, and int c. This means it is a compile-time polymorphism.


Runtime Polymorphism


The runtime polymorphism is achieved using Method overriding. Unlike method overloading, method overriding must have the same method name and as well as the same method arguments.


Below is the simple example of the method overriding in java program.

          interface Laptop
  {
      void print();
  } 

  public class HP implements Laptop
  {
      @Override
      public void print()
      {
          System.out.println("HP Class");	
      }

      public static void main(String[] args)
      {
          HP hp = new HP();
          hp.print();
      }
  }

Output of the method overriding in java program
HP Class(code-box)

In the above java program example, HP Class is an overriding print method which is of Laptop interface.


Different between Compile-time polymorphism and Runtime polymorphism.



Compile-time Polymorphism Runtime Polymorphism
The compile-time Polymorphism is achieved by using method overloading. The Runtime Polymorphism is achieved by using method overriding
The Compile-time Polymorphism must have the same name but with different arguments or a different number of arguments The Runtime Polymorphism must have the same name as well as the same arguments
The Compile-time Polymorphism can use only within a single class To achieve the Runtime Polymorphism, you must have two different class

Conclusion


In this article, we have seen java object-oriented programming (OOPs) concepts. We have discussed all four main concepts of Object-oriented programing language (OOPs), which is the inheritance, the abstraction, the encapsulation, and the polymorphism. If you are a beginner then don't stop here, explore more about oops concepts.


If you have enjoyed this content, then please subscribe to this blog, so in the future, If I publish a new article you don't miss it. The subscription link is in the header section of this website and as well as on the sidebar of this website.


Thank you for reading and keep learning

Tags

Post a Comment

0 Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top