Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Classes And Objects

In Java, classes and objects are fundamental concepts in object-oriented programming (OOP). A class is a blueprint for creating objects, which are instances of a class. Classes define properties (attributes) and behaviors (methods) that objects can have.

In this tutorial, we will go through the basics of creating and using classes and objects in Java.

  • Creating a class:

To create a class, use the class keyword followed by the class name. Inside the class, you can define fields and methods. Here's an example:

public class Dog {
    String name;
    String breed;
    int age;

    void bark() {
        System.out.println("Woof!");
    }

    void displayInfo() {
        System.out.println("Name: " + name + ", Breed: " + breed + ", Age: " + age);
    }
}

In this example, we create a Dog class with three fields (name, breed, and age) and two methods (bark and displayInfo).

  • Creating objects:

To create an object of a class, use the new keyword followed by the class name and parentheses. Here's an example:

Dog myDog = new Dog();

In this example, we create an object of the Dog class named myDog.

  • Accessing object fields:

To access an object's fields, use the object name followed by the dot (.) operator and the field name. Here's an example:

myDog.name = "Buddy";
myDog.breed = "Golden Retriever";
myDog.age = 3;

In this example, we set the values of the name, breed, and age fields of the myDog object.

  • Calling object methods:

To call an object's methods, use the object name followed by the dot (.) operator and the method name. Here's an example:

myDog.bark();          // Output: Woof!
myDog.displayInfo();   // Output: Name: Buddy, Breed: Golden Retriever, Age: 3

In this example, we call the bark() and displayInfo() methods of the myDog object.

Here's the complete example:

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.name = "Buddy";
        myDog.breed = "Golden Retriever";
        myDog.age = 3;

        myDog.bark();          // Output: Woof!
        myDog.displayInfo();   // Output: Name: Buddy, Breed: Golden Retriever, Age: 3
    }
}

In summary, classes and objects are core concepts in Java and object-oriented programming. Classes define the structure and behavior of objects, and objects are instances of classes. By using classes and objects, you can create modular, reusable, and well-organized code.

  1. Creating Objects in Java Class Example: Objects are instances of a class. Here's an example of creating objects in Java:

    public class Car {
        String make;
        String model;
    
        public void start() {
            System.out.println("Car is starting.");
        }
    }
    
    public class CarExample {
        public static void main(String[] args) {
            // Creating objects
            Car myCar = new Car();
            Car friendCar = new Car();
    
            // Accessing object properties and methods
            myCar.make = "Toyota";
            myCar.model = "Camry";
            myCar.start();
        }
    }
    
  2. Instance Variables in Java Classes: Instance variables are fields that belong to each instance of a class. They represent the state of an object.

    public class Person {
        String name; // Instance variable
        int age; // Instance variable
    }
    
  3. Methods in Java Classes and Their Role in Objects: Methods define the behavior of objects. They perform actions and can manipulate the object's state.

    public class Circle {
        double radius; // Instance variable
    
        public double calculateArea() {
            return Math.PI * radius * radius;
        }
    }
    
  4. Java Class and Object Relationships: Classes define blueprints for objects. Objects are instances of classes, and they interact through methods and properties.

    public class Dog {
        String name;
        int age;
    
        public void bark() {
            System.out.println("Woof! Woof!");
        }
    }
    
    public class DogOwner {
        public static void main(String[] args) {
            Dog myDog = new Dog();
            myDog.name = "Buddy";
            myDog.age = 3;
            myDog.bark();
        }
    }
    
  5. Access Modifiers for Class Members in Java: Access modifiers control the visibility of class members. Common modifiers include public, private, protected, and package-private.

    public class MyClass {
        public int publicVar;
        private String privateVar;
        protected double protectedVar;
        int defaultVar; // Package-private by default
    }
    
  6. Inheritance and Polymorphism in Java Classes: Inheritance allows a class to inherit properties and methods from another class. Polymorphism enables objects to be treated as instances of their parent class.

    public class Animal {
        public void makeSound() {
            System.out.println("Animal makes a sound");
        }
    }
    
    public class Dog extends Animal {
        @Override
        public void makeSound() {
            System.out.println("Dog barks");
        }
    }
    
  7. Encapsulation in Java Classes and Objects: Encapsulation involves bundling data and methods within a class, and controlling access to the internal state.

    public class BankAccount {
        private double balance;
    
        public void deposit(double amount) {
            if (amount > 0) {
                balance += amount;
            }
        }
    
        public double getBalance() {
            return balance;
        }
    }