What is Java Class and Object?


In this Java Class and Object tutorial, we will learn about the fundamentals of Java Programming. Java Class and Object are the base of Java. And we have tried to explain it in a simpler way. For your better understanding, we have also provided example Java programs for concepts of Java Classes and Object and Java Constructors.

Classes and Objects are the fundamental building blocks of OOPS (Object Oriented Programming). A Java object is a physical and logical entity whereas a Java class is a logical entity.

Table of Content

1. What is a Java Class?
  • Syntax of java Class
  • Explanation of example code
  • Types Of Java Classes

2. What is a Java Object?
  • How to create Java Object?
  • Example Code and it's explanation
  • What is the use of Java Objects?

3. Write your first Java Program - "Hello World"

4. What is Java Constructor?
  • Non-Parameterized Constructor (Default)
  • Parameterized Constructor

1. What is a Java Class?

Before creating an object in Java, you need to define a class. A class is a blueprint from which an object is created. We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object. Since many houses can be made from the same description, we can create many objects from a class. A class can have the following entities inside it:

  • Fields (Variables)
  • Methods
  • Constructor
  • Nested Class
  • Interface
  • Blocks
Syntax of a Java Class:
public class Dog {

   String breed;
   int age;
   String color;

   public void barking() {
   }

   public void hungry() {
   }

}
Explanation of the example code:
  • Variables like breed, age, and color are attributes or exhibits state.
  • Whereas barking() and hungry() are methods that exhibit behavior.
Let's understand these concepts from an example:

Class: 

Animal

Objects:

Dog

Cat

Cow

Now, you can relate the definition that a Class is a template and an object is an instance of the class.


Java Class and Object Video Tutorial



1.1. Types of Classes in Java

In Java, classes can be classified into various types based on their functionality, accessibility, and scope. Here are the main types of classes in Java:

1. Concrete Class

A concrete class is a class that can be instantiated or can be used to create objects. It provides the implementation for all its methods and is fully defined. It can be used as a base class or a parent class for other classes. An example of a concrete class is the String class.

java
public class Car { private String make; private String model; private int year; public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } }

In the above example, Car is a concrete class that can be instantiated and used to create objects.

    2. Abstract Class

    An abstract class is a class that cannot be instantiated but can be used as a base class or parent class for other classes. It contains at least one abstract method that has no implementation and must be implemented in the subclass. Abstract classes are useful for creating a class hierarchy and for code reuse. An example of an abstract class is the AbstractList class.

    java
    public abstract class Animal { private String name; private int age; public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public abstract void makeSound(); }

    In the above example, Animal is an abstract class that cannot be instantiated, but can be used as a base class or parent class for other classes. It contains an abstract method makeSound() that must be implemented in the subclass.

    3. Interface

    An interface is a collection of abstract methods and constants that define the behavior of a class. It can be implemented by a class, and a class can implement multiple interfaces. The methods declared in an interface are abstract by default and must be implemented by the implementing class. An example of an interface is the Serializable interface.

    java
    public interface Drawable { void draw(); }

    In the above example, Drawable is an interface that defines a single abstract method draw(). A class that implements this interface must provide an implementation for this method.

    4. Final Class

    A final class is a class that cannot be subclassed. Once a class is declared final, it cannot be extended or modified. Final classes are useful for creating immutable classes or utility classes. An example of a final class is the Math class.

    java
    public final class Constants { public static final double PI = 3.14159265359; public static final int MAX_VALUE = 100; private Constants() {} }

    In the above example, Constants is a final class that cannot be subclassed. It contains only static final fields and a private constructor. It is used to define constants that are used throughout the program.

    5. Static Class

    A static class is a nested class that has only static methods and fields. It does not have any instance variables and methods. Static classes are used for grouping related methods and fields together. An example of a static class is the Arrays class.

    java
    public class MathUtils { public static int add(int a, int b) { return a + b; } public static int subtract(int a, int b) { return a - b; } private MathUtils() {} }

    The above example, MathUtils is a static class that contains only static methods. It is used to group related methods together.

    6. Inner Class

    An inner class is a class that is defined inside another class. It can be static or non-static. Inner classes can access the private members of the outer class and are useful for encapsulation and creating helper classes. An example of an inner class is the Entry class in the Map interface.

    java
    public class Map { private Entry[] entries; public Map(int size) { entries = new Entry[size]; } public void put(String key, Object value) { Entry entry = new Entry(key, value); // add entry to the entries array } public Object get(String key) { // find entry with matching key and return its value } private class Entry { private String key; private Object value; public Entry(String key, Object value) { this.key = key; this.value = value; } public String getKey() { return key; } public Object getValue() { return value; } } }

    In the above example, Map is a class that contains an inner class Entry. Entry can access the private members of Map and is useful for encapsulation and creating helper classes.

    7. Local Class

    A local class is a class that is defined inside a block of code, such as a method or a loop. It is only accessible within that block of code and is useful for creating short-lived objects. An example of a local class is a class defined inside a loop that iterates over a collection.

    java
    public class MyClass { public void printNames(List<String> names) { class NamePrinter { public void print() { for (String name : names) { System.out.println(name); } } } NamePrinter printer = new NamePrinter(); printer.print(); } }

    2. What is a Java Object?

    In Java, an object is an instance of a class. It is a fundamental unit of object-oriented programming and can be thought of as a combination of data and behavior that is defined by a class. Objects are created from classes and are used to interact with the program and other objects.

    Here's an example to illustrate how objects work in Java:

    java
    public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }

    In the above code, we have a class called Person with two private instance variables name and age, along with their respective getter and setter methods.

    To create an object of this class, we use the new keyword followed by the class name and the constructor parameters:

    java
    Person person1 = new Person("John", 30);

    This line of code creates a new Person object with the name "John" and age 30, and assigns it to the variable person1.

    We can access the object's properties using the getter methods:

    java
    String name = person1.getName(); int age = person1.getAge(); System.out.println(name + " is " + age + " years old.");

    This will output: John is 30 years old.

    We can also modify the object's properties using the setter methods:

    java
    person1.setName("Jane"); person1.setAge(25); System.out.println(person1.getName() + " is now " + person1.getAge() + " years old.");

    This will output: Jane is now 25 years old.

    In summary, an object in Java is an instance of a class that contains data and behavior defined by the class. They are created using the new keyword and can be used to interact with the program and other objects.

    2.1. Uses of Java Objects


    Objects in Java are used to represent real-world entities or concepts in a program. They allow developers to organize code into manageable units of data and behavior, making it easier to create and maintain complex applications. Here are some common uses of objects in Java:

    1. Modeling Real-World Entities: Objects can be used to model real-world entities in a program. For example, a Person object can be used to represent a real person, with properties like name, age, and address, and behaviors like walking, talking, and eating.

    2. Data Storage: Objects can store data and state within a program. This can be useful for storing complex data structures like trees or graphs, or for storing data that needs to be accessed from multiple parts of a program.

    3. Encapsulation: Objects can be used to encapsulate data and behavior, which means that the data is hidden from other parts of the program and can only be accessed through well-defined methods. This helps to prevent unintended changes to the data and improves program reliability and maintainability.

    4. Inheritance: Objects can be used to create inheritance hierarchies, where subclasses inherit properties and behavior from a superclass. This allows developers to reuse code and create more specialized objects.

    5. Polymorphism: Objects can be used to achieve polymorphism, which means that objects of different classes can be treated as if they are of the same type. This allows for a more flexible and dynamic program design, where objects can be created and manipulated at runtime.

    6. Modularity: Objects can be used to create modular code, where different parts of a program can be developed and tested independently. This allows for more efficient and collaborative development, as multiple developers can work on different objects at the same time.


    3. Write your First Java Program - Hello World

    It's always exciting to write your first program and execute it without error. In this post, you will learn to write your first Java program.
    Creating Hello World Program
    • Declare a class 'MyFirstProgram'
    • Declare the main method public static void main()
    • And print a string "Hello World" using System.out.println()
    Hello World Program

    // Class
    class MyFirstProgram {
     
     // Main method
     public static void main(String[] args) {
      
      // Print Hello World
      System.out.println("Hello World");
     } 
    
    }

    2.1. Explanation of Java keywords used in the 'Hello World' Program:

    • the class keyword is used to declare a class.
    • Public is an access modifier that defines the main method's accessibility. Public means the main method is global or accessible to all other classes in the project.
    • static is a keyword in Java, when static is used with any method then it is called a static method. A static method can be called without creating the object of the class.
    • main is the method name and also the execution point of any program. The main method is required to execute the program.
    • String[] args is an array of strings that is basically for command line arguments.
    • System.out.println() is used to print any statement. 
      • System is a class
      • out is the object of PrintStream class
      • println() is method of PrintStream class

    Run your First Java Program:

    To run this program, we need to compile the source code into bytecode and then execute it using the Java Virtual Machine (JVM). Here are the steps to do that:

    1. Open a text editor and copy the above code into a new file called HelloWorld.java.
    2. Save the file to your local disk.
    3. Open a command prompt or terminal window and navigate to the directory where the HelloWorld.java the file is saved.
    4. Type the following command to compile the program: javac HelloWorld.java. This will generate a bytecode file called HelloWorld.class.
    5. Type the following command to execute the program: java HelloWorld. This will run the program and output the message "Hello, World!" to the console.
    6. If you are using Eclipse then you can run this program by just clicking the green play button displayed on the top bar. 
    7. Or right-click anywhere in the class and click 'Run as Java Program'.

    And that's it! You've written and executed your first Java program. Congratulations!


    Output:
    • Hello World

    4.  What is Java Constructor?

    In Java, a constructor is called a special method. It is used to create objects in Java. A constructor is called whenever we initialize an object. 

    Some special features of Java Constructor:

    • It has the same name as that of a class.
    • It does not have any return type.
    • Its syntax is similar to the method.
    • A Java constructor cannot be static, final, or abstract.
    • A class always has a default constructor whether you have declared it or not. But if you define a custom constructor then the default constructor is no longer in use.

    Java Constructor Video Tutorial


    4.1. Types of Constructors in Java:

    • Non-Parameterized Constructor (Default Constructor)
    • Parameterized Constructor

    4.1.1. Non-Parameterized Constructor

    As the name suggests, this constructor doesn't have any parameter in its signature.

    Syntax for Default Constructor:

    public class MyClass{  
    	
    	// Creating a default constructor  
    	MyClass(){
    		System.out.println("Object created");
    	}  
    
    	// Main method  
    	public static void main(String args[]){  
    		//Calling a default constructor  or Creating an object of MyClass
    		MyClass obj = new MyClass();  
    	}  
    }  

    Explanation of example code:

    • We have created a default constructor "My Class", which will print "Object created" whenever we create an object.
    • In the main method, we are calling the constructor to initialize our object named "obj".
    Output:
    Object Created

    4.1.2. Parameterized Constructor

    It's a constructor which accepts parameters. It can have one or more parameters. It is used to provide different values to different objects.

    Syntax for Parameterized Constructor:

    public class MyClass {
    
      // Declare variable
      int num;
    
      // Parameterized Constructor
      public MyClass(int y) {
        num = y;
      }
    
      public static void main(String[] args) {
        // Call the parameterized constructor and pass parameter 5
        MyClass myObj = new MyClass(5);
        // Print num
        System.out.println(myObj.num);
      } 
    }

    Explanation of the example code:
    • We declared a variable num.
    • Declared a parameterized constructor with parameter int y.
    • Inside the constructor, we are initializing the num's value to y.
    • In the main method, we are calling the parameterized constructor and passing the value 5 to y. It will initialize num to 5 inside the parameterized constructor.
    • Now the value of the num is set to 5 and it'll print 5.
    Output:
    5

    Java Modifiers and Operators << Previous  |  Next >>  Java Methods

    Author
    Vaneesh Behl
    Passionately writing and working in Tech Space for more than a decade.

    Comments

    1. oh good! you bring good & unique content for us. I'm glad to read. Please keep sharing. - Mahindra Jivo 365

      ReplyDelete
    2. You are giving such interesting information. It is great and beneficial info for us, I really enjoyed reading it. Thankful to you for sharing an article like this.dft training in bangalore

      ReplyDelete
    3. Thanks for sharing beneficial tutorial. Web development, design and All SEO service provider Houston Digital Marketing Agency

      ReplyDelete
    4. The blog you have shared is awesome about C Programming Assignment Help This is very useful for us. Thanks for sharing such a good blog.

      ReplyDelete
    5. Good to read this article when starting a blog. Happy to read that is only a limitation in our minds Thanks!

      ReplyDelete
    6. This article was such an amazing masterpiece. I really want to know how did you get this information? I’m really thankful to you to provide this knowledge. Tablet hire is providing the best services for tablets, iPad, laptops, and many other technical devices at rent. we serve our services at a very responsible price.
      tab rentals

      ReplyDelete

    Post a Comment

    Popular posts from this blog

    Top 7 Web Development Trends in the Market

    17 Best Demo Websites for Automation Testing Practice

    Top Mobile Test Automation Tools for Efficient App Testing: Features and Cons

    Mastering Selenium WebDriver: 25+ Essential Commands for Effective Web Testing

    Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface

    What Role Graphic Design Services Play in Marketing

    Top 51 Most Important Selenium WebDriver Interview Questions

    What's New in Selenium-Automated Testing

    How Tesla is Revolutionizing AI and Energy in 2025