Tuesday, March 31, 2020

Polymorphism in Java-Part 1



The concept by which we can perform a single action in different ways is polymorphism. It means that polymorphism allows you to define one interface and have multiple implementations.  Polymorphism can be performed in java by method overloading and method overriding.
The two types of polymorphism in Java are:
  1. Compile-time polymorphism
  2. Run-time polymorphism
Compile time polymorphism
It is also called as static polymorphism. Function overloading or operator overloading is used to achieve this type of polymorphism.
Method Overloading: Multiple functions having same name, but different parameters then are said to be overloaded. These functions can be overloaded by change in number of arguments or change in type of arguments.
//Java program for Method overloading
  
class MultiplyFun {
  
    // Method with 2 parameter
    static int Multiply(int a, int b)
    {
        return a * b;
    }
  
    // Same name but 2 double parameter
    static double Multiply(double a, double b)
    {
        return a * b;
    }
}
  
class Main {
    public static void main(String[] args)
    {
  
        System.out.println(MultiplyFun.Multiply(2, 4));
  
        System.out.println(MultiplyFun.Multiply(5.5, 6.3));
    }
}


Output: 8,34.6
Runtime Polymorphism

It is also called as Dynamic Method Dispatch. In this process the function call to the overridden method is resolved at runtime. Method overriding is used to achieve this type of polymorphism.

// Java program for Method overriding
class Parent {
  
    void Print()
    {
        System.out.println("parent class");
    }
}
  
class subclass1 extends Parent {
  
    void Print()
    {
        System.out.println("subclass1");
    }
}
  
class subclass2 extends Parent {
  
    void Print()
    {
        System.out.println("subclass2");
    }
}
  
class TestPolymorphism3 {
    public static void main(String[] args)
    {
  
        Parent a;
  
        a = new subclass1();
        a.Print();
  
        a = new subclass2();
        a.Print();
    }
}
Output: subclass1
subclass2

Rishita Jaiswal
K-28


Sunday, March 22, 2020

Inheritance in Java - Part 2


The keyword used for inheritance is extends.

Syntax:

    class derived_class extends base_class
   {
               //members and methods
    }
 






super : reference variable that is used to refer parent class objects. 


In the example given below the Base Class is Employee and the Derived Class is Manager : 

public class Employee {

int empid;
String empname;
double salary;

public Employee() {
System.out.println("Default constructor of Emp class");
empid = 1;
empname = "Ram";
salary = 10000;
}

public Employee(int empid, String empname, double salary) {
System.out.println("Parameterized constructor of Emp class");
this.empid = empid;
this.empname = empname;
this.salary = salary;
}

@Override
public String toString() {
return empid + " " + empname + " " + salary;
}

public  double calSalary() {
return salary;
}

protected String showName(){
return empname;
}
}
­­­­­­­­

public class Manager extends Employee {

int ptallow, fdallow, incentive;

public Manager(int empid, String empname, double salary, int ptallow, int fdallow, int incentive) {

super(empid, empname, salary);
System.out.println("Parameterized constructor of Manager class");
this.fdallow = fdallow;
this.ptallow = ptallow;
this.incentive = incentive;
}

@Override
public String toString() {
return empid + " " + empname + " " + salary + " " + ptallow + " " + fdallow + " " + incentive;
}

@Override
public double calSalary() {
// TODO Auto-generated method stub
return super.calSalary() + ptallow + fdallow;
}
}
//Driver code
public class TestMain {

public static void main(String[] args) {
Employee  e1 = new Empoyee(100,"Smruti",10000);  //object of Employee
Manager m1 = new Manager(101,"Swapna",25000,100,100,100); //object of Manager

System.out.println(e1.showName());
System.out.println(e1.calSalary());

System.out.println(m1.showName());
System.out.println(m1.calSalary());
}
}

Output:
Smruti
10000
Swapna
25200


Smruti Khire
K38

Friday, March 20, 2020

Inheritance in Java


In OOPs we can create the different classes and functions according to our need and from the base class we can derived the classes so, using the Inheritance the derived class can access the behaviour and property of  base class .Using Inheritance the code reusability increases i.e if we want to create a new class and there is already a class which include some of the data we want then we can derive the new class from existing class and time requirement for process can be reduced and debugging of code becomes easy.
There are five types of Inheritance supported by Java:
Single Inheritance:
In a single inheritance, sub class inherit the features of one superclass. In below figure 'A' is super class and 'B' is a sub class which is inheriting the features from super class.
                                               
fig. single Inheritance
Multilevel Inheritance:
In multilevel inheritance derived class will be ineheriting a base class and it will act as a base class to another class .Here 'A' is base class and 'B' is inheriting features from 'A' and 'B' is base class as well for class'C'.                          
fig. Multilevel Inheritance

Hierarchical Inheritance:
In Hierarchical Inheritance there is only one super class and multiple sub classes inherit properties from single super class.
fig. Hierarchical Inheritance

Multiple Inheritance:
In Multiple Inheritance the one base class can inherit properties from more than one super class. In Java we can achieve Multiple Inheritance only with Interfaces
                                   

fig. Multiple Inheritance


Hybrid Inheritance:
It is the mix of all types of inheritance,Java does not support Hybrid Inheritance we can achieve it through interfaces like multiple inheritance
fig. Hybrid Inheritance

Amarja Chede(K 10)

Writing Classes in Java-Part 3


Keywords in Java

1] ‘this   keyword –

    There are  lot of usage of this keyword. In java, this is a reference variable that refers to the current object. Within an instance method or a constructor, this is a reference to the current object - the object whose method or constructor is being called. To refer any member of current object from instance method or constructor using this can be done.



Use of this keyword-
1.     this keyword can be used to refer current class instance variable.
2.     this() can be used to invoke current class constructor.
3.     this keyword can be used to invoke current class method (implicitly)
4.     this can be passed as an argument in the method call.
5.     this can be passed as argument in the constructor call.
6.     this keyword can also be used to return the current class instance.

The  reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

Example –
1]
   public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}
 
Same above example can be written in easy way using this keyword -
 
public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
2] From within a constructor, you can also use the this keyword to call another constructor in the same class

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    
}

2] ‘static’ keyword –

The static keyword in Java is used for memory management. Static keyword can be applied  with variables, methods, blocks and nested classes. The static keyword belongs to class than an instance of the class. When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level. Static variables are, essentially, global variables.
The static can be:
  1. Variable (also known as a class variable)
  2. Method (also known as a class method)
  3. Block
  4. Nested class
Advantage of static keyword is, it makes program memory efficient.
Example-
class Test
{
    // static variable
    static int a = m1();
      
    // static block
    static {
        System.out.println("Inside static block");
    }
      
    // static method
    static int m1() {
        System.out.println("from m1");
        return 20;
    }
      
    // static method(main !!)
    public static void main(String[] args)
    {
       System.out.println("Value of a ="+a);
       System.out.println("from main");
    }
  
  
}

OUTPUT-
From m1
Inside static block
value of a = 20
from main

Diksha Avhad(K 3)

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)