Class and Object in Java

What is OOP?

Object oriented programming – As the name suggests uses objects in programming. Object oriented programming aims to implement real world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operates on them so that no other part of code can access this data except that function.

Oops Concept

Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts −

Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Methods

1. What is an Object:

In object oriented programming Whenever you do something you need an object.
Print, calculate, execute, process, save and to return
Object don't do things individually
Basically object has couple of information
What object knows  variables  -- to store values
What object does   methods (execute and process)
Object are crated using class. Objects and classes are interrelated to each other.

If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behaviour.

If we consider a dog, then its state is - name, breed, color, and the behaviour is - barking, wagging the tail, running.

2. What is a Class?

A class is a blueprint or design from which individual objects are created.

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

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

How to Create an Object?

In Java, an object is created from a class.

Syntax -  <ClassName> Reference = new <ClassName()>;

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behaviour of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

To get more details please watch below youtube video and Subscribe the channel.




No comments:

Post a Comment