Introduction to classes with real-world Examples

10. Real world examples of classes, methods and Objects in Comparision with Object Oriented Programming

A class is like a blueprint for creating similar objects.

Let's take a real-world example

A car company has a blueprint of any car model and based on that they create similar kind of cars.

Here, every car is an object and the blueprint of car is like a class.

 every car object has it's own attributes such as weight, color, seating capacity, etc. and methods such as driving, applying breaks, turning on headlights, etc.

A class defines a new data type. Once defined, this new type can be used to create objects of that type. Means a class is a template for an object, and an object is an instance of an class. In other terms an Object is a specific case of a class.

The General form of a class

When we define a class we declare its exact form and nature. This is done by specifying the data that it contains and the code that operates on that data.

Syntax:

class name {
  type instance-variable1;
  type instance-variable2;
  type methodname1(parameter-list) {
	// body of method
  }
  type methodname2(parameter-list) {
	// body of method
  }
} 
  • A class is declared by using the keyword class.
  • The variables defined within a class are called instance variables.
  • The methods and variables defined inside a class are called member of that class.
  • Variables defined within a class are called instance variables because each object of the class contains its own copy of these variables. That means, the data for one object is unique from the data for another.

A simple Class

class Box {
  double width;
  double height;
  double depth;
}

In above example the new data type is called Box. Now, Box can be used to declare objects of type Box.

A class declaration only creates a template; it does not create an actual object.

To create an actual object of type box we have to use statement like the one below

// create a Box object called mybox
Box mybox = new Box();

Now, a new box object is created thus, it will contain its own copies of the instance variables width, height, and depth. To access this variables you will use the dot(.) operator.

mybox.width = 100;

Here is the complete code of the program that uses a box class

Real-world Examples of classes their data and methods

1. Class developer
Data members name, age, city, gender, Aadhar number
Methods eat(), code(), Sleep(), play(), talk() etc.

2. Class Laptop
Data members brand, Size, Storage, RAM, etc.
Methods power_on(), play_movies(), code(), shutdown(), etc

3. Class Hospital
Data members name, Specialization, 
Methods diagnose(), treat(), ..

4. Class Birds
Data members name, type, color, gender, 
Methods eat(), fly(), sleep(), tweet(),..

5. Class Car
Data members brand, color, weight, seating capacity, etc.
Methods driving(), applying-breaks(), turning-on-headlights(), speed(), etc..

6. Class Dog
Data members breed, color, age, Gender
Methods Eat(), sleep(), sit(), run(), bark()..

7. Class Watch
Data members brand, battery, 
Methods time(), date(), day(), ..

8. Class Mobile phone
Data members brand, model, storage, battery-life,..
Methods Call(), mobile_data(), wifi(), play_music(),...

9. Class Ball
Data members size, weight, diameter, color, ..
Methods roll(), bounce(), ..

10. Class Camera
Data members brand, size, lens, battery, quality, color, 
Methods click-images(), record(), store(), focus(), etc.

And much more real-life classes are available around us. What are other real-life classes ? Let others know your example. write it down in the comment.😀

Have a great Learning journey.

Also checkout some of the interesting notes you may like.

Comments

YouTube

Popular posts from this blog

GTU OOP Program - 16

Vim Text Editor

Introduction to Java, classes and objects