L20: Classes and Objects

Q1. What is object-oriented model in programming ?

The object-oriented programming (OOP) model is a programming paradigm based on the concepts of objects which can contain data in the form of fields (often known as attributes or properties) and code in the form of methods. In object-oriented model objects are used to represent the real-world entities that have some physical or conceptual existance. OOP allows developers to model the complex problems in a natural way i.e. more closely to real-world entities. Furthermore, it promotes code reuse and makes easier to maintain, debug and upgrade large programs.

Q2. What is the relationship between classes and objects in OOP?

A class is a blueprint or template for creating objects. It defines the attributes and methods that all objects of that class will possess. Thus, a class descibes the structure and behaviour of an object but does not represent any specific instance of an object. For example, a Mobile class can have atributes like brand, model, operating_system, screen_size, color and methods like make_call, send_sms.
An object is a specific instance of a class. It is created using the template or blueprint provided by the class. An object has its own unique state (values of attributes) and behaviour (execution of methods). For example, if we create an object mymobile from the class Mobile then mymobile has specific values for brand, model, operating_system, screen_size, color and it can make_call, send_sms.
In summary, a class defines the structure and behaviour of objects, while objects are instances of classes that have their own specific state and behaviour.

Q3. What is the use of keyword new ?

new is used to instantiate an object from a class. In this process, it allocates memory for an instance of that class and returns a reference to that memory location. The new keyword is typically used as follows:
ClassName objectName=new ClassName();

Q4. What do you mean by instance variables and instance methods ?

In OOP, instance variables and instance instance methods are associated with an instance (object) of a class. Instance variables, also termed as member variables or attributes, are variables that are declared inside a class but outside any method. Each object has its own copy of these variables and represent the state or characteristic of that particular object.
Instance methods, also termed as member methods or object methods, are methods that define the behaviour or actions performed by an object of a class. Thus, instance methods are associated with objects of a class. Instance methods access and update the instance variables of the object to perform actions specific to each object.
Thus, instance variables and methods work together to define the state and behaviour of individual objects in a class.

Q5. Create a Rectangle class with attributes length and width. Implement method setData to assign values to length and width. Implement another method findArea, to compute and print the area of a rectangle. Then, create objects of this Rectangle class and demonstrate how to use them.

class Rectangle {
  double length;
  double width;
  void setData(double len, double wid) {
    length=len;
    width=wid;
  }
  void findArea() {
    double area=length*width;
    System.out.println("The area is:"+area);
  }
}
class Test {
  public static void main(String[] args) {
    Rectangle r1=new Rectangle();
    r1.setData(5.5,6.5);
    r1.findArea();
    Rectangle r2=new Rectangle();
    r2.setData(2.3,7.1);
    r2.findArea();
  }
}

Q6. Create a Book class with attributes title, author, and pages. Implement a method setBook to initialize these attributes and another method displayBook to display book details. Then, create objects of this class and demonstrate how to initialize and display book details.

class Book {
  String title;
  String author;
  int pages;
  void setBook(String t, String a, int p) {
    title=t;
    author=a;
    pages=p;
  }
  void displayBook() {
    System.out.println("The title is:"+title);
    System.out.println("The author is:"+author);
    System.out.println("Total pages of the book are:"+pages);
  }
}
class Test {
  public static void main(String[] args) {
    Book mybook=new Book();
    mybook.setBook("Arthasastra", "Chanakya", 600);
    Book yourbook=new Book();
    yourbook.setBook("Rich Dad Poor Dad", "Kiyosaki", 340);
    mybook.displayBook();
    yourbook.displayBook();
  }
}