Classes and Objects in Java

Introduction to Classes and Objects in Java

Classes and objects are the fundamental building blocks of object-oriented programming (OOP). OOP is a programming paradigm that allows developers to create reusable and extensible code.

A class is a blueprint for creating objects. It defines the properties and behaviors that objects of that class will have. An object is an instance of a class. It is a concrete representation of a class, with specific values for its properties.

For example, you could create a class called `Person` to represent people. The `Person` class could have properties such as `name`, `age`, and `occupation`. It could also have behaviors such as `eat()`, `sleep()`, and `work()`.

Once you have created a class, you can use it to create objects. For example, the following code creates a `Person` object with the name "Alice" and the age 25:

    Person alice = new Person("Alice", 25);
  

You can then access the properties and behaviors of the `alice` object using the dot notation. For example, the following code prints the name of the `alice` object to the console:

    System.out.println(alice.name);
  

Output:

    Alice
  

You can also call the methods of the `alice` object to make it perform actions. For example, the following code calls the `eat()` method of the `alice` object:

    alice.eat();
  

This will cause the `alice` object to perform the action of eating.

Classes and objects are a powerful way to organize your code and make it more reusable and extensible. By understanding the basics of classes and objects, you can write more efficient and maintainable code.

Here are some of the benefits of using classes and objects in Java:

  • Code reuse: Classes and objects allow you to reuse your code by creating blueprints for objects that you can use in different parts of your program.
  • Extensibility: Classes and objects make it easy to extend your code by adding new properties and behaviors to existing classes.
  • Encapsulation: Classes and objects encapsulate data and code, which makes your code more secure and easier to maintain.
  • Modularity: Classes and objects allow you to break your code down into smaller, more manageable modules.

Classes and objects are essential to Java programming. By understanding the basics of classes and objects, you can write more efficient and maintainable code.

Exercises

Code for declaring a Student Class. Creating an Object for student class and invoking it's methods


  import java.util.Scanner;
  public class javalabclass 
  {
    public static void main(String args[])
      {
      student satish = new student();
      satish.set_profile();
      satish.view_profile();
    }
  }
  
  class student
  {
    
    String regNumber;
    String Name;
    String city;
    
    public void set_profile()
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter your regNumber");
      this.regNumber = input.next();
      System.out.println("Enter your Name");
      this.Name = input.next();
      System.out.println("Enter your City");
      this.city=input.next();
    }
    
    public void view_profile()
    {
      System.out.println(this.Name+this.regNumber+this.city);
    }
    
  }
  
  Output:
  
  Enter your regNumber
  112
  Enter your Name
  Satish
  Enter your City
  Vellore
  Satish112Vellore 

Code for creating multiple objects of a student class and invoking its methods


  import java.util.Scanner;
  public class javalabclass 
  {
    public static void main(String args[])
      {
      student satish = new student();
      satish.set_profile();
      satish.view_profile();
      student ram = new student();
      ram.set_profile();
      ram.view_profile();
    }
  }
  
  class student
  {
    
    String regNumber;
    String Name;
    String city;
    
    public void set_profile()
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter your regNumber");
      this.regNumber = input.next();
      System.out.println("Enter your Name");
      this.Name = input.next();
      System.out.println("Enter your City");
      this.city=input.next();
    }
    
    public void view_profile()
    {
      System.out.println(this.Name+this.regNumber+this.city);
    }
    
  }
  
  Output:
  Enter your regNumber
  112
  Enter your Name
  Satish
  Enter your City
  Vellore
  Satish112Vellore
  Enter your regNumber
  113
  Enter your Name
  Mathew
  Enter your City
  Pune
  Mathew113Pune 

Create student, course and faculty objects. You should be able to add a new object and view the details of the objects by getting user input. Define your own data members and methods for the classes


  import java.util.*;
  public class javalabclass{
      public static void main(String args[])
      {
          students s1=new students();
          s1.set_profile();
          s1.get_profile();
          course s2= new course();
          s2.set_profile();
          s2.get_profile();
          faculty s3= new faculty();
          s3.set_profile();
          s3.get_profile();  
      }
  }
  
  class students{
      public String name;
      public String regno;
      public void set_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter name of the student:");
          this.name=sc.next();
          System.out.println("Enter regno of the student:");
          this.regno=sc.next();
      }
        public void get_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("name of the student:"+this.name);
          System.out.println("regno of the student:"+this.regno);
      }
      
  }
  
  class course{
      public String name;
      public String code;
      public void set_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter name of the course:");
          this.name=sc.next();
          System.out.println("Enter code of the course:");
          this.code=sc.next();
      }
        public void get_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("name of the course:"+this.name);
          System.out.println("code of the course:"+this.code);
      }
      
  }
  class faculty{
      public String name;
      public String id;
      public void set_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter name of the faculty:");
          this.name=sc.next();
          System.out.println("Enter id of the faculty:");
          this.id=sc.next();
      }
        public void get_profile()
      {
          Scanner sc=new Scanner(System.in);
          System.out.println("name of the faculty:"+this.name);
          System.out.println("id of the faculty:"+this.id);
      }
      
  }
  
  Output
  Enter name of the student:
  Satish
  Enter regno of the student:
  111
  name of the student:Satish
  regno of the student:111
  Enter name of the course:
  Java
  Enter code of the course:
  CSE1007
  name of the course:Java
  code of the course:CSE1007
  Enter name of the faculty:
  Mathew
  Enter id of the faculty:
  1243
  name of the faculty:Mathew
  id of the faculty:1243