JAVA CLASSES AND OBJECTS

  1.   Java is completely based on object oriented programming language(OOPS) because when we compared to 'c' and 'c++' language, In 'c' we can't even create a class and In 'c++' we can create class, but for every program it's not mandatory to create class, but In 'Java' ,  class is mandatory to every program.
  2.  Class is the core of Java language.
  3.  Class is a blueprint/template that describes the behaviors of a particular entity.
  4.  Object is an instance(physical existence) of class. Example: Consider the classroom with 4 students(a,b,c,d) , so now the classroom is the class and those 4 Students are objects , these objects physically represent the classroom. 20200722_195804syntax for class :
class class_name
{      
methods or variables;
}

we create class by using class keyword.

Syntax for creating an object, accessing the methods and variables

creating object :

class_name object_name=new class_name();
Example : Operate op=new Operate(); 

Accessing the variable :

object_name.variable_name
Example : op.a=10; 

Accessing the method :

object_name.method_name(object_name);

Example : op.sum(op); 

Example program:

public class Operate
{
        int a,b,c;
        public int sum(Operate p)
       {
                p.a=10;
                p.b=20;
                p.c=p.a+p.b;
                return c;
        }
        public static void main(String[] args)
        {
               Operate op=new Operate();
               op.sum(op);
               System.out.println("sum is "+op.c);
        }
}
output:
sum is 30

Rules for Java class :

Rule 1: Class can have only public or no access specifier.

Rule 2: Class can be Abstract or final or normal.

Rule 3: Class variables and methods are declared within curly braces { }.

Rule 4: The file of java should be the same as class name which consists static main method with a suffix .java

Main features of OOPs concepts Java follows :

  1.  Abstraction
  2. Encapsulation
  3.  Inheritance
  4. Polymorphism

Abstraction :

  1. Abstraction is process of hiding internal details and showing essential features.
  2. Abstraction can be achieved by Abstract class and by Interface.

Example: Remote shows the buttons those are essential features but hide the internal things like diodes.

Encapsulation :

  1. Encapsulation is a process of wrapping code and data together in to single unit.
  2. We can Encapsulate class by making all the data members of the class private, then providing access to them by public methods that keep data and code safe with in class itself.
  3. We can reuse the objects as variables with out allowing open access to the data system.

Example: Java Bean class encapsulates the many objects in to single object.

Inheritance :

  1. Inheritance is a process of acquiring properties of one class from another class.
  2. The class which is acquiring the properties is child or sub class , the class which is giving the properties to another class is parent or super class.
  3. A class can only inherits all non private member from another class. we use extends keyword for inheritance.

Polymorphism :

  1. Polymorphism mean "many forms", It means ability of an object to take on many forms.
  2. Polymorphism allows us to perform single action in different ways.
  3. Compile time and Run time polymorphism are types of polymorphiosm, where compile time is static method dispatch, runtime is dynamic method dispatch.

Methods in java :

  1. Method is collection of statements grouped to one to perform operations, these are same as functions in c++ and c, just named as methods.
  2.  In java, the class consists of public static void main(String[] args) because this method accessed with out creating an object.
  3.  We can create many classes in one java file but the class name of java file should be the class name which consists of main method.

syntax:

modifier return_type method_name(datatype variable) // call by value
modifier return_type method_name(class_name object_name) // call by reference

Example:

public class Website
{
    String str="Programming9";
    public void m1(Website w) // In this method we need to create object to access it ,because its call by reference method
    {
        w.str="Welcome to Programming";
    }
    public int m2(int x) //call by value
    {
        x=9;
        return x;
    }
    public static void main(String[] args) // because its a static method ,JVM invoke method with out creating the object.
    {
        Website ws =new Website();//To access the variables or methods in class we need to create object for class.
        int x=0;    
        System.out.println(ws.str);
        ws.m1(ws);
        x=ws.m2(x);
        System.out.println(ws.str+x);
    }
}

Output:

Programming9
Welocome to Programming9