Wednesday, March 8, 2017

Abstract Class

Abstract Class is used to make some methods compulsory for subclasses to redefine them according to the subclass.
Thus it provide basic about the functions which an object has to perform .

Syntax:    abstract type name(parameter list);

If a method is abstract then the class must be declared abstract. All the subclasses have to define that method.

Following will illustrate the concept:

Code::

abstract class Base
{
    abstract void show();
}
class Derived extends Base
{
    void show()
    {
        System.out.println("Derived Show");
    }
}
public class AbstractUse 
{
    public static void main(String [] nt)
    {
       // Base b=new Base();  Compile time error
        Derived d=new Derived();
        d.show();
        Base n;     //abstract class reference can be created
        n=d;
        n.show();
    }
}

Output:: 

Dynamic Method Dispatch

Dynamic Method Dispatch is an important feature of java in which the call to a overridden method is resolved at runtime i.e:runtime polymorphism.

In this case the Superclass reference object is assigned the subclass objects hence according to the object referenced the version of overridden method is called.

Following example will illustrate the concept:

Code::

import java.util.*;
class Figure
{
    double a,b;
    Figure(double c,double d)
    {
        a=c;
        b=d;
    }
    void area()
    {
        System.out.println("Area: 0");
    }
}
class Rectangle extends Figure
{
    Rectangle(double c,double d)
    {
        super(c,d);
    }
    void area()
    {
        System.out.println("Area of Rectangle:"+a*b+" square meters");
    }
}
class Triangle extends Figure
{
    Triangle(double c,double d)
    {
        super(c,d);
    }
    void area()
    {
        System.out.println("Area of Triangle:"+0.5*a*b+" square meters");
    }
}
class Circle extends Figure
{
    Circle(double c)
    {
        super(c,c);
    }
    void area()
    {
        System.out.println("Area of Circle:"+3.142*a*a+" square meters");
    }
}
class Square extends Figure
{
    Square(double c)
    {
        super(c,c);
    }
    void area()
    {
        System.out.println("Area of Square: "+a*b+" square meters");
    }
}
public class Area 
{
       public static void main(String[] nt)
       {
           Rectangle r=new Rectangle(5,4);
           Circle c=new Circle(10);
           Square s=new Square(5);
           Triangle t=new Triangle(4,5);
           Figure f;
           f=r;
           f.area();         //rectangle area
           f=c;
           f.area();          // circle area
           f=s;
           f.area();         //square area
           f=t;
           f.area();        //triangle area
       }
}

Output::

Tuesday, March 7, 2017

Electrical Resistance of Wire

The electrical resistance R of a cylindrical wire with length l (in meter) and diameter d (in meter) can be computed from the area A of its diameter (m2) and the  resistivity Ρ of the material (rho, meter times Ohm). The formula:

R=P*(l/A)

Code::

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mrdishant
 */
import java.util.*;
public class Resistance {
    public static void main(String[] nt)
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter Length of wire:");
        double length=in.nextDouble();
        System.out.print("Enter Diameter of Wire:");
        double d=in.nextDouble();
        System.out.print("Enter Resistivity of wire:");
        double p=in.nextDouble();
        double r=p*(4*length/3.142*d*d);
        System.out.println("Resistance of Wire:"+r);
 
    }
    
}

Output::

Metropolis City or Not

Question::  We define a Metropolis to be a city, that is either a capital city with more than 100 000 citizens or more than 200 000 citizens and an average income of 720 000 000 per year.

Give a boolean expression with all three variables that is true if and only if the city is a metropolis.

Code::

import java.util.*;
public class NT 
{
    public static void main(String [] nt)
    {
        boolean iscapital=false;
        long nofcitizens;
        double taxpercitizen;
        Scanner in=new Scanner(System.in);
        System.out.print("Press Y if City is a Capital Else anything:");
        char a=in.next().charAt(0);
        if(a=='y'||a=='Y')
        {
            iscapital=true;
        }
        System.out.print("Enter Number of Citizens:");
        nofcitizens=in.nextLong();
        System.out.print("Enter Tax Per Citizen:");
        taxpercitizen=in.nextDouble();
        if(iscapital  && nofcitizens>=100000||nofcitizens>=200000&&taxpercitizen*12*nofcitizens>720000000)
        {
            System.out.println("Yes This city is a Metropolis");
        }
        else
        {
            System.out.println(" This city is not a Metropolis");
        }
    }
    

}

Output:: 

Sunday, March 5, 2017

Method Overloading In Inheritance Java

If there is a method in subclass having same name as that of method in superclass but these have some difference in number and type of arguments then Method Overloading Takes place.

Code::
class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show(int x)
    {
        System.out.println("E's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        e ob=new e();
        ob.show();
        ob.show(67);
    }

}
Output:

Method Overriding In Java

When Superclass and subclass have method having same signature and name then a call to that method using subclass object will lead to invoking subclass version of that method. This is called method overriding.

Following Code will illustrate the concept:

Code::

class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show()
    {
        System.out.println("E's method");
    }
}
class f extends e
{
    void show()
    {
        System.out.println("F's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        f ob=new f();
        ob.show();
    }

}

Output::
As the above example shows that only subclass version is called.

Order of Execution of Constructors Java

Constructors follow the order of derivation in case of Inheritance
i.e- Superclass to Subclass
firstly superclass constructor is called then subclass constructor is called.
also in case of Multilevel inheritance.

Following code will illustrate the concept:

Code::

class a
{
    a()
    {
        System.out.println("A's Constructor");
    }
}
class b extends a
{
    b()
    {
        System.out.println("B's Constructor");
    }
}
class c extends b
{
    c()
    {
        System.out.println("C's Constructor");
    }
}
public class Constructors
{

    public static void main(String[] nt)
    {
        c ob=new c();
    }

}

Output::