06 Introduction to Java

Java is an object-oriented programming language (OOP) developed by Sun Microsystems, which enables programmers to create flexible, modular and reusable codes. The important feature of OOP language which help programmers achieve this are encapsulation, polymorphism, and inheritance.



  • Encapsulation: It is a mechanism that binds together codes and data and manipulates to keep the code safe from outside interference and misuse. Java implements encapsulation by defining the accessibility condition of variables, methods, and classes (private, protected and public). Java's basic unit of encapsulation is class. A class specifies the data and the code that will operate on the data. A class specification is used to construct objects which are instances of a class.

  • Polymorphism: It is an OOP feature that enables an object to determine which method to implement or invoke upon receiving a method call. One of the ways Java implements this is through method overloading and method overriding.

  • Inheritance: It is an OOP feature which enables building new classes from the existing ones. With inheritance, subclasses get access to all the attributes of its superclass. This allows one object to acquire the properties of another. One way of implementing this in Java is by defining abstract classes.

  • Abstraction: It is an OOP feature that used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface).

6.1 | Classes

One of the fundamental ways in which Java handles complexity is an abstraction. A class models an abstraction by defining the properties and behaviors of objects representing the abstraction. Class acts as a blueprint for creating objects.

6.1.1 | Writing First Java Class Program

The following steps describe how to create a new java class and write a code to it.

1. Open Eclipse and create a new project, say 'SeleniumAutomation'.

2. Right-click on the 'src' and select New --> Package. 'New Java Package' window pops is opened. Enter the package name as 'com.testinganswers'.

3. Right-click on the 'com.testinganswers' package and select New --> Class. 'New Java Class' window pops is opened.

4. Specify the class name say 'MyFirstClass' and click on 'Finish' button. Specified class is created as shown in below figure.

5. Write code for the class. Below figure shows a simple class code with 'main' method to display 'Hello World!!!' on Eclipse console.


6. To execute this code from Eclipse, right click on the Eclipse editor window and select Run As --> Java Application.