Public App


Category: Object Oriented Programming with Java

Objects and Classes

In Java, a class is a blueprint or a template for creating objects. It defines the properties and behaviors of the objects that belong to the class. A class contains variables, methods, and constructors that define the characteristics of the objects it creates.

An object, on the other hand, is an instance of a class. It represents a single entity that has a state and behavior defined by the class it belongs to. Objects can be created from a class by using the new keyword and invoking the constructor of the class.

Here’s an example of a simple class and an object created from that class:

public class Person {
    String name;
    int age;

    public Person(String name, int age) { = name;
        this.age = age;

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");

public class Main {
    public static void main(String[] args) {
        Person person1 = new Person("John", 30);

In this example, we have defined a class called Person that has two instance variables, name and age, and a constructor that takes two parameters and initializes these variables. The class also has a method called sayHello that prints a message to the console.

In the main method of the Main class, we create a new Person object named person1 by calling the constructor of the Person class with the arguments “John” and 30. We then call the sayHello method on the person1 object, which prints the message “Hello, my name is John and I am 30 years old.” to the console.

In summary, a class is a template for creating objects, and an object is an instance of a class that has its own state and behavior. By defining classes and creating objects, Java allows us to organize and model complex systems in a more structured and reusable way.

Simple Program in Java

Here is an example of a simple “Hello, World!” program in Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");

This program consists of a single class named HelloWorld. The main method is the entry point of the program and is executed when the program is run. The System.out.println statement prints the string “Hello, World!” to the console.

To run this program, you can save the code in a file named and compile it using the Java compiler. Open a command prompt or terminal window and navigate to the directory where the file is saved, then run the following command:


This will compile the code and generate a bytecode file named HelloWorld.class. To run the program, enter the following command:

java HelloWorld

This will execute the main method in the HelloWorld class and print “Hello, World!” to the console.

Java Environment (JVM)

Java Environment refers to the environment in which Java programs run, and it typically includes the Java Development Kit (JDK), the Java Virtual Machine (JVM), and the Java Runtime Environment (JRE).

The JDK is a set of software tools and libraries that are used to develop, compile, and debug Java applications. It includes the Java compiler, which is used to convert Java source code into bytecode, and other tools like the debugger, profiler, and Javadoc, which are used to generate API documentation.

The JVM is a software component that runs Java bytecode and provides an execution environment for Java applications. It is responsible for interpreting the bytecode, managing memory, and providing platform-independent features like automatic garbage collection, class loading, and security. The JVM also provides a set of standard Java APIs that can be used to develop and run Java applications.

The JRE is a subset of the JDK and is used to run Java applications on end-user machines. It includes the JVM and other essential libraries and tools needed to run Java applications, but it does not include the development tools like the Java compiler.

Together, the JDK, JVM, and JRE make up the Java environment, which provides a platform-independent way of developing and running Java applications. This allows developers to write Java code once and run it on any platform that has a compatible Java environment installed.

Byte Code

In Java, bytecode refers to the compiled code that is generated by the Java compiler from the source code written by the developer. Bytecode is a highly optimized, platform-independent format that can be executed by any Java Virtual Machine (JVM), regardless of the underlying hardware and operating system.

Bytecode is essentially a set of instructions that the JVM can understand and execute. When a Java program is compiled, the source code is translated into bytecode which is then saved in a .class file. The bytecode is not machine code specific, which means it can run on any platform that has a JVM installed.

The advantage of using bytecode is that it makes Java highly portable and enables “write once, run anywhere” (WORA) capabilities. Since bytecode is not machine code specific, developers can create Java programs on one platform and run them on any platform that has a compatible JVM installed, without having to recompile the code.

When a Java program is executed, the JVM reads the bytecode and translates it into machine code that can be executed by the host operating system. This process is called just-in-time (JIT) compilation, and it allows the JVM to optimize the bytecode for the specific hardware and operating system on which the code is running, resulting in improved performance.

Java Development Kit (JDK)

JDK stands for Java Development Kit, and it is a software development kit used to develop and deploy Java applications.

The JDK includes a set of programming tools, such as the Java compiler, Javadoc, and the Java Virtual Machine (JVM), as well as libraries and APIs for building Java applications. It also contains additional tools for debugging, testing, and profiling Java applications.

Developers use the JDK to write and compile Java code, package it into JAR files, and deploy it to servers or client machines. The JDK is available for multiple platforms, including Windows, macOS, and Linux, and can be downloaded for free from the Oracle website.

The JDK is an essential tool for Java developers, as it provides everything needed to create, test, and run Java applications. It is also updated regularly with new features and improvements to the Java language and platform.

Java Virtual Machine (JVM)

Java Virtual Machine (JVM) is an abstract computing machine that provides an environment in which Java programs can be executed. The JVM is a key component of the Java platform and is responsible for interpreting the compiled Java code and providing a runtime environment for the code to run.

When a Java program is compiled, it is translated into bytecode, which is a platform-independent code that can be executed on any machine that has a JVM installed. The JVM then reads the bytecode and translates it into machine code that can be executed on the host operating system.

The JVM is responsible for many important functions, including memory management, garbage collection, and security. It also provides a wide range of libraries and tools for developers to use in their Java programs.

One of the main advantages of using the JVM is that it provides platform independence, meaning that Java programs can be written once and run on any machine that has a compatible JVM installed. This makes Java a popular choice for developing applications that need to run on multiple platforms.

Features of Java

There are main four features of Java Programming Language:

  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

There are other features like:

  • Method Overloading
  • Method Overriding


Using the abstraction feature of Java, we can protect certain data and show only necessary information to the user. In Java, abstraction can be achieved using either abstract classes or interfaces.

Abstract Class

An abstract class in java is a restricted class that cannot be used to create an object. To access an abstract class you will need to inherit it from another class.

Abstract Method

An abstract method in java is only used inside an abstract class and it does not have a body. The body is provided by the subclass that inherits that abstract class.


Polymorphism means many forms and this feature is used when we have many classes and these are related to each other by using the inheritance feature.


The inheritance feature of Java is used to inherit attributes and methods from one class to another. The class that inherits attributes and methods from another class is called a child class or subclass. And which class’s attributes and methods are inherited is called parent class or superclass.


Encapsulation in Java is a feature that is used to wrap the data and code in a single unit (just like a capsule that contains a mix of particular medicines).