object and classes in java

object and classes in java
Since Java is an object-oriented language, complete Java language is built on classes and object. Java is also known as a strong Object-oriented programming language(oops).
OOPS is a programming approach which provides a solution to problems with the help of algorithms based on the real world. It uses a real-world approach to solve a problem. So object oriented technique offers a better and easy way to write a program then procedural programming model such as C, ALGOL, PASCAL.
object and classes in java free images


Main Features of OOPS

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction
As an object-oriented language, Java supports all the features given above. We will discuss all these features in detail later.

Class

In Java, everything is encapsulated underclasses. The class is the core of Java language. A class can be defined as a template/ blueprint that describes the behaviors /states of a particular entity. A class defines the new data type. Once defined this new type can be used to create an object of that type. The object is an instance of a class. You may also call it as physical existence of a logical template class.
A class is declared using class keyword. A class contains both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

Rules for Java Class

  • A simple class

    Suppose, Student is a class and a student's name, roll number, age will be its property. Let's see this in Java syntax
    class Student.
    {
     String name;
     int rollno;
     int age;
    }
    
    When a reference is made to a particular student with its property then it becomes an object, physical existence of Student class.
    Student std=new Student();
    After the above statement std is instance/object of Student class. Here the new keyword creates an actual physical copy of the object and assigns it to the std variable. It will have physical existence and get memory in heap area. The new operator dynamically allocates memory for an object

A simple class example












Q. How a class is initialized in java?

A Class is initialized in Java when an instance of the class is created using either a new operator or using reflection using.class.forName() A class is also said to be initialized when a static method of Class is invoked or a static field of Class is assigned.

Q. How would you make a copy of an entire Java object with its state?

Make that class implement Cloneable interface and call clone() method on its object. clone() method is defined in Object class which is a parent of all java class by default.

Methods in Java

The method describes the behavior of an object. A method is a collection of statements that are group together to perform an operation.
Syntax :
return-type methodName(parameter-list)
{
 //body of method
}

Example of a Method

public String getName(String st)
{
 String name="StudyTonight";
 name=name+st;
 return name;
}

Example of a Method










Modifier: Modifier is access type of method. We will discuss it in detail later.
Return Type: A method may return value. The data type of value return by a method is declared in method heading.
Method name: Actual name of the method.
Parameter: Value passed to a method.
Method body: a collection of statement that defines what method does.

Parameter Vs. Argument

While talking about the method, it is important to know the difference between the two terms parameter and argument.
The parameter is variably defined by a method that receives value when the method is called. Parameter are always local to the method they don't have scope outside the method. While the argument is a value that is passed to a method when it is called.
Parameter Vs. Argument















call-by-value and call-by-reference

There are two ways to pass an argument to a method
  1. call-by-value: In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments.
  2. call-by-reference: In this reference to an argument is passed through a method. Any changes made inside the method will affect the argument value.
NOTE: There is only call by value in java, not call by reference.

Example of call-by-value

public class Test
{
    public void callByValue(int x)
    {
        x=100;
    }
    public static void main(String[] args)
    {
       int x=50;
        Test t = new Test();
        t.callByValue(x); //function call
        System.out.println(x);
    }
    
}
Output :
50

Method overloading

If two or more method in a class has the same name but different parameters, it is known as method overloading.
Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing a number of arguments or by changing the data type of arguments. If two or more method have the same name and same parameter list but differs in return type are not said to be overloaded method

Different ways of Method overloading

There are two different ways of method overloading

Method overloading by changing the data type of Arguments

Example :
class Calculate
{
 void sum (int a, int b)
 {
  System.out.println("sum is"+(a+b)) ;
 }
 void sum (float a, float b)
 {
  System.out.println("sum is"+(a+b));
 }
 Public static void main (String[] args)
 {
  Calculate  cal = new Calculate();
  cal.sum (8,5);      //sum(int a, int b) is method is called. 
  cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
 }
}

Output :
Sum is 13
Sum is 8.4
You can see that sum() method is overloaded two times. The first takes two integer arguments, the second takes two float arguments.

Method overloading by changing no. of argument.

Example :
class Area
{
 void find(int l, int b)
 {
  System.out.println("Area is"+(l*b)) ;
 }
 void find(int l, int b,int h)
 {
  System.out.println("Area is"+(l*b*h));
 }
 public static void main (String[] args)
 {
  Area  ar = new Area();
  ar.find(8,5);     //find(int l, int b) is method is called. 
  ar.find(4,6,2);    //find(int l, int b,int h) is called.
 }
}
Output :
Area is 40
Area is 48
In this example, the find() method is overloaded twice. The first takes two arguments to calculate area, and the second takes three arguments to calculate the area.
When an overloaded method is called java look for a match between the arguments to call the method and the method's parameters. This match need not always be exact, sometimes when an exact match is not found, Java automatic type conversion plays a vital role.

Example of Method overloading with type promotion.

class Area
{
 void find(long l,long b)
 {
  System.out.println("Area is"+(l*b)) ;
 }
 void find(int l, int b,int h)
 {
  System.out.println("Area is"+(l*b*h));
 }
 public static void main (String[] args)
 {
  Area  ar = new Area();
  ar.find(8,5);     //automatic type conversion from find(int,int) to find(long,long) . 
  ar.find(2,4,6)    //find(int l, int b,int h) is called.
 }
}
Output :
Area is 40
Area is 48




















































Comments