Class & Objects

  • Class
    • Classes and Objects are basic concepts of Object-Oriented Programming.
    • A class is a user-defined data type from which objects are created.
    • A class can be defined as a template/blueprint that describes the behavior/state that the objects.
    • Syntax:
modifier class class_name
{
member variable (properties);
member methods;
}
  • It represents the set of properties or methods.
    • Modifiers : A class can be public or private or default access.
      • The private keyword makes instance variables and methods private which can be accessed only from inside the same class.
      • The public keyword makes instance variables and methods public which can be accessed from outside of the class.
    • class: It is a keyword.
    • class_name: The name should begin with an initial letter.
    • Body (block of code): The class body surrounded by braces { }.
      • Class has mainly two types of members:
        • 1. member variable (states)
        • 2. member methods (behaviors)
  • Example:
public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

Note: When class is defined, no memory or storage is allocated.
  • Object
    • It is a basic unit of OOP.
    • A Java program creates many objects from class.
    • Objects have states and behaviors.
    • An object has:
      • States : 
        • It is represented by attributes of an object. 
        • It also reflects the properties of an object.
      • Behavior: 
        • It is represented by methods of an object.
        • It also reflects the response of an object with other objects.
      • Identity:  
        • It gives a unique name to an object.
        • One object to interact with other objects.
  • Example: 
    • A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating.

Creating & Declaring Objects:
  • A single class may have any number of instances
  • All the instances share the attributes and the behavior of the class.
  • Creating an Object
    • A class provides the blueprints for objects. 
    • So basically, an object is created from a class. 
    • In Java, the new keyword is used to create new objects.
    • There are three steps when creating an object from a class −
      • 1. Declaration − A variable declaration with a variable name with an object type.
      • 2. Instantiation − The 'new' keyword is used to create the object.
      • 3. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  • Example:
Dog myDog = new Dog();
Dog yourDog = new Dog();

Types of variables:
  • Local variables − Variables defined inside methods, constructors or blocks are called local variables. 
  • Instance variables − Instance variables are variables within a class but outside any method. 
  • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
How to access members?
  • You can access members (call methods and access instance variables) by using . operator. 
  • Syntax:
object_name.members_variable;
object_name.members_method;
  • For example:
myDog.breed;
yourDog.color;

myDog.sleeping();
yourDog.barking();

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form