Friday, March 20, 2020

Writing Classes in Java-Part 2


Access Specifiers in Java
The scope of a constructor, class, variable, method or data member can be restricted using access modifiers in Java. There access modifiers available in java are of four types:
1.     Default – No keyword required
The element is assigned default accessibility level when one does not set access specifier for the element. The class, data members or methods which have default access modifier are accessible only within the same package.

2.     Private:
Private specifiers achieve the lowest level of accessibility. Private methods and fields can be accessed within the same class and is not visible to subclasses and can neither be inherited by it. It is used to achieve encapsulation.

3.     Protected:
The data members or methods which are declared as protected are accessible within same package or sub classes in different package. This access specifier cannot be applied to class.

4.     Public:
They have highest level of accessibility. The methods, fields and classes declared as public can be accessed from any class.


Types of variables in Java
The three types of variables in Java are:
  1. Local Variables
It is a variable which is defined within a block or method or constructor. A local variable’s initialisation is mandatory. Its scope only exists within the block in which it is declared.

  1. Instance Variables
These variables are non-static variables and are declared in a class outside any method, block or constructor. These variables are created when an object of the class is created and destroyed when the object is destroyed, since these variables are declared in a class. Access specifiers can be used for instance variables. The default access specifier will be used if we do not specify any access specifier. The Instance Variable’s initialisation is not mandatory, its default value is 0. They can only be accessed by creating objects.

  1. Static Variables (also known as class variables)
These variables are declared using the static keyword within a class outside any method constructor or block. Unlike instance variables, only one copy of a static variable per class can be created irrespective of how many objects we create. These variables are created at the start of program execution and destroyed automatically when execution ends. The static variable’s initialisation is not mandatory, its default value is 0. When the static variable is accessed like the instance variable (through an object), the compiler will replace the object name to class name automatically. The compiler automatically appends the class name when the static variable is accessed without the class name.


Method Overloading in Java
Overloading allows different methods to have the same name, but they can differ by the type of input parameters or the number of input parameters or both. Overloading is related to compile-time (or static) polymorphism.
// Java program to demonstrate working of method
// overloading in Java.
public class Sum {
  
    // Overloaded sum(). This sum takes two int parameters
    public int sum(int x, int y)
    {
        return (x + y);
    }
  
    // Overloaded sum(). This sum takes three int parameters
    public int sum(int x, int y, int z)
    {
        return (x + y + z);
    }
  
    // Overloaded sum(). This sum takes two double parameters
    public double sum(double x, double y)
    {
        return (x + y);
    }
  
    // Driver code
    public static void main(String args[])
    {
        Sum s = new Sum();
        System.out.println(s.sum(10, 20));
        System.out.println(s.sum(10, 20, 30));
        System.out.println(s.sum(10.5, 20.5));
    }
}

Output: 30,60,31.0

Rishita Jaiswal(K 28)

19 comments: