Java Online - Class and Object in Java Programming language

Programming languages or concepts

 Class and Object in Java Programming Language:


    In This lesson we are learning Class and Object in Java. in Object-Oriented Programming technique. We design a program using objects and classes.

    These are fundamental concepts in the field of Object Oriented Programming and having a good grasp of these concepts is essential for further concepts of OOP.


What is means by object ?

  Object in java is a physical and as well as logical entity.

What is a class in java?


A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity.
    A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type.
   class defines the attributes of objects

 In general, class declarations can include these components they are as follows:-

  • Modifiers:
  • class keyword
  • Class name:
  • Superclass(if any):
  • Interfaces(if any)
  • Body

  • Modifiers:-
                A class can be public or has default access.
               This determines the accessibility of the class whether its public(accessible to all) or private(accessible to limited members of the program. )

  • class keyword:-
                class keyword is used to create a class.
               

  • Class name:
            The name should begin with an initial letter.
            It should not be any keyword. Convention suggests that all class names should begin with a capital letter.

  • Superclass(if any):
             Superclasses will be explained when we learn about inheritance in the later chapters. For now it is enough to know that superclasses are mentioned in the class declaration when it inherits data from a super class. The keyword used is “extends”.
            The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.


  • Interfaces(if any):-
               A class can implement more than one interface.
             Interfaces are mentioned, separated by commas, individually after the class definition.
                
  • Body:-
            The class body surrounded by braces, { }.


HOW TO DECLARE CLASS And Types of Classes:


  • The keyword class is used to declare a class.
  • A class declaration may have zero or more modifiers.
  • The <<class name>> is a user-defined name of the class, which should be a valid identifier.
  • Each class has a body, which is specified inside a of braces ({ … }).
  • The body of a class contains its different components, for example, fields, methods, etc.

Syntax to declare a class:-
public class Main {

    public static void main(String[] args) {

    }
}


Types of Classes:-


  1. Abstract class:-
  2. Non-Abstract class:-

 1.Abstract class:-

          These classes are abstract. These are incomplete classes. It means you     cannot create an instance of this class.
         You can only extend these classes to complete their specification.

2.Non-Abstract class:-

        These classes define their full state and behavior. They are complete classes.
 You can create objects of this class.


What is an object in Java:-


        The objects are real life entities which are derived from classes.They share the same variables and methods but different values of them .
        It is a basic unit of Object-Oriented Programming and represents the real life entities.  A typical Java program creates many objects, which as you know, interact by invoking methods.
        An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical entity.


An object has Following  characteristics:-

  1. State:
  2. Behavior:
  3. Identity:
        

1. State:
        It is represented by attributes of an object. It also reflects the properties of an object. that means its represents the data (value) of an object.

2. Behavior:
        It is represented by methods of an object.
It is determined by the methods of the object. 
 It also reflects the response of an object with other objects.

For example an object having a function to print (“Hello”) will be the behaviour of that particular object.

3. Identity:
        It allows objects to interact with one another. Object names must be unique. 
        An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user.


Declaring Objects in Java:-

We are now familiar with the concept of objects.

<type><object-name>.

Ways of Creating Object in Java:-


There are following ways of creating objects.

Using New Keyword:-

 This creates objects with the name mentioned. This is the most simple way to create objects in Java.

Example:-

// using new keyword
public class Demo
{
String str = "Hello Friends:";
public static void main(String[] args)
{
// Here we are creating Object of
Demo obj = new Demo();
System.out.println(obj.str);
}
}

Hello Friends:"


Using the Class package:-

        This is a slightly different method of declaring objects. There is a predefined class called Class in the java.lang package. This has a method called “forName” which takes one string argument “package.className”. The method returns an instance of the class of the same name as the string argument mentioned.

Using Clone method:-

        Whenever clone() is called on any object, the JVM actually creates a new object and copies all content of the previous object into it.

        As the name suggests, the clone method clones/copies all the variables and methods that are in an object into another object. It is a member of the Object class.

        Creating an object using the clone method does not invoke any constructor.



Tags:
close