Day 1:Objects and Classes in Java

Day 1:Objects and Classes in Java

ยท

3 min read

Objects and classes are the two main pillars of object-oriented programming in Java. Classes allow us to define new types by bundling both data and behaviors together, while objects act as instances of those classes.

What is a class?

A class is a blueprint that defines the characteristics and behaviors that objects of that class will have. A class contains:

  • Fields (variables) that store the state of an object.

  • Methods that define the behaviors or actions that an object can perform.

For example, we can define a Student class like this:

class Student {
    String name;  //field
    int age;      //field

    void study() {  //method
       //program to make the student study!
    }
    void takeExam() {  //method
       // simulate the student taking an exam
    }
}

Our Student class has two fields - name and age to store the name and age of a student. It also has two methods - study() and takeExam() that define the behaviors of a student - studying and taking an exam.

What is an object?

An object is an instance of a class. We use the new keyword to create objects from classes. For example:

Student john = new Student();  
john.name = "John Doe";
john.age = 19;

Here we have created an object john of type Student by using the new keyword. We then set the name and age fields of this john object.

Member Variables and Methods

The fields and methods defined within a class are known as:

  • Fields are called member variables

  • Methods are called member methods

The member variables define the state of an object, while the member methods define the behavior.

Encapsulation

Encapsulation refers to binding the data and the functions that manipulate that data together within a single unit - the class.

The data can be hidden from the outside world by declaring the fields as private. This ensures that only member methods within the class can access and modify that data.

For example:

class Student {
    private String name;  
    private int age;    //fields are private

    public void setName(String name){
        this.name = name;  //member method setting name
    }
    public String getName(){
        return name;    //member method getting name 
    }
}

Here we have declared the fields as private and provided setter and getter methods to set and get the name and age. This encapsulates the data within the class.

Inheritance

Inheritance allows us to define a class (the child class) that inherits the properties and behaviors of another class (parent class).

For example:

class Person {
    String name;
    int age;
}

class Student extends Person {
    // inherits name and age from Person
}

Here Student inherits from Person so it automatically has the name and age fields. Inheritance allows us to reuse code and model "is-a" relationships.

Polymorphism

Polymorphism means the ability to take multiple forms. In Java, polymorphism occurs when we have multiple classes that are related by inheritance and they provide their own implementation of a method defined in their parent class.

For example:

class Animal {
    void makeSound() {
      System.out.println("Making some sound!") 
    }
}

class Dog extends Animal {
    void makeSound() {
      System.out.println("Bark! Bark!");
    }
}

class Cat extends Animal {
    void makeSound() {
       System.out.println("Meow! Meow!");
    }
}

Here the makeSound() method takes different forms in the Dog and Cat classes. This is polymorphism in action.

That covers the basics of objects and classes in Java! Happy Hacking!

Here's the complete implementation code for this log. https://gist.github.com/13x54n/00e0e43d11091041a0d1af7d1add0364

Did you find this article valuable?

Support Lexy Thinks by becoming a sponsor. Any amount is appreciated!

ย