DEV Community

darKLoin
darKLoin

Posted on

day-05

DAY-05
Index:-
01.Static Variable
02.Commandline argument
03.Garbage collection
04.Finalize keyword
05.Inheritance
06.Dynamic method dispatch
---------------------------------START----------------------------
----------------------------------01------------------------------
01.Static Variable
~It is comman to all among the object or shared among all the objecs
~constructor cannot used for te static variable
constructor--> shared variable/instance variable
static variable--> class variable shared among all the objects
~for the static variable we cannot use the constructor, constructor is for the static vatiable is 'statc' keyword
example: 01.1

class useStatic 
{
    static int c = 3;
    static int d ;
    static void show(int x)
    {
        System.out.println("X="+x);
        System.out.println("c="+c);
        System.out.println("d="+d);
    }
    static//it is the constructor for the static variable
    {
        System.out.println("static block is initialize.");
        d = c*2;
    }
    public static void main(String[] args)
        {
        show(200);//here it is just calling by it name
        }
}

Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
--------------------------------02--------------------------------
02.Commandline Argument
~in normal program we pass the value or input during run time to get input before running,
~we have the parameter in main as '(String args[])'
it rell that we can input before running the program
it is having a array with name args, its size depends on the input in the commandLine or before running value input
example: 02.1

class commandLine
{
    public static void main(String args[])
    {
        for(int i =0 ; i<args.length ; i++)
        {
            System.out.println("location args["+i+"] = "+args[i]);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
C:\Users\user\Desktop>javac commandLine.java
C:\Users\user\Desktop>java commandLine hello how are you 123 45 1 2 3 4 5
location args[0] = hello
location args[1] = how
location args[2] = are
location args[3] = you
location args[4] = 123
location args[5] = 45
location args[6] = 1
location args[7] = 2
location args[8] = 3
location args[9] = 4
location args[10] = 5
Enter fullscreen mode Exit fullscreen mode

~using this we can give input in any program from CommandLine only
---------------------------------END------------------------------
--------------------------------03--------------------------------
03.Garbage Collection
~in other languages we have to delocalize or delete the memory after the use during progamm
but in java it is done peridiocally by it means system will do this for you automatically,it will delete the free the memory of assign object or variable or when the variabel not come in use for a long time.
it will use the 'System.gc()' function to do this or we can call it when ever we want
~during automatic process of delocalizing memory we want to store some data, file or memory then we have to use the 'finalize()' function and the code in this finalize block will excute and do the process before delocalizing
exmaple: 03.1

class garbagecoll
{
    public static void main(String args[]) throws InterruptedException
    {
        garbagecoll t1 = new garbagecoll();
        garbagecoll t2 = new garbagecoll();
        t1 = null;
        System.gc();
        t2 = null;
        Runtime.getRuntime().gc();
    }
    protected void finalize() throws Throwable
    {
        System.out.println("Garbage collector called...");
    }
}

Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
--------------------------------04--------------------------------
04.Inheritace
~we all have seen that we get the geans or characterstics from our parent and java language is also follow the same type.
~a CHILD class can use or perform the task objects written in the Parent class or CHILD class can have it own special characterstics or property which can not be transform to their PARENT class.
parent class ->> SUPER CLASS[transform its characterstics to SUB-SUPER CLASS ]
child class ->> SUB-SUPER CLASS[it can also transform its characterstics to other SUB-SUB-SUPER CLASS, but it can not transform to it parent class]
~it also called as "reuse of code-- reusability" child class can use the code of parent class
~command/generalize objects are written in the PARENT CLASS
~special/specified objects are writtrn in the CHILD CLASS
NOTE:: anything defined as private by PARENT class is not accessible to CHILD class
~kind of inheritance

  1. Simple Inheritace (one parent class) ---> (one child class) A(PARENT) ---> B(CHILD)
  2. Multi-level Inheritace (two parent) ---> (one child) A(GRAND-PARENT) --> B(PARENT) ---> C(CHILD) 3.Multiple Inheritace(in java it is done by 'interface') (two different parent) ---> (one child) A(PARENT),B(PARENT) ---> C(CHILD) ~how it is written base class(parent) derived class(child)

class parent
{...
}
class child extends parent

example:
Enter fullscreen mode Exit fullscreen mode
class Motorvehicle
{
    int speed;
    String modelname;
    int modelyear;
    int noofpassenger;

    Motorvehicle()
    {
        speed = 250;
        modelname = "KTM";
        modelyear = 2021;
        noofpassenger = 2;
    }

    Motorvehicle(int ms , int my , int nop ,String mn)
    {
        speed = ms;
        modelyear = my;
        noofpassenger = nop;
        modelname = mn;
    }

    void show()
    {
        System.out.println("Maximum speed is: "+speed );
        System.out.println("Model Year is: "+modelyear );
        System.out.println("Model Name is: "+modelname );
        System.out.println("Number Of Passenger is: "+noofpassenger);
    }
}
class Bike extends Motorvehicle
{
    boolean kick;
    boolean self;

    Bike()
    {
        kick = true;
        self = false;
    }
    Bike(boolean k , boolean s )
    {
        kick = k ;
        self = s;
    }

    void show()
    {
        System.out.println("Bike is kick start:"+ kick);
        System.out.println("Bike is self start:"+ self);
    }
}
class Inheritance
{
    public static void main(String args[])
    {
        Motorvehicle obj1 = new Motorvehicle();
        Bike obj2 = new Bike();

        //Motorvehicle ee;
        //ee = obj1;
        //obj1.sohw();
        //ee = obj2;
        //obj2.show();


        obj1.show();
        obj2.show();
    }
}
Enter fullscreen mode Exit fullscreen mode

~here the same variable doing the different function or task this is known as 'dynamic method dispatch'

~Dynamic Method Dispatch: means the result of a function is depend on the runtime or the assign value.

~Method Overriding:: when two or more class (parent, child) have function with same name and same parameter then it us called method overriding
eg: the 'show()' function in the previous program
example:

class A 
{
    int i  =0 ;
    void overriding(int k)
    {
        i = k;
    }
}
class B extends A
{
    void overriding(int k)
    {
        i = 2*k;
        System.out.println("the value of i is:"+i);
        }
}
class test
{
    public static void main(String args[])
    {
        B ob = new B();
        ob.overriding(220);
    }
}
Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------

Top comments (0)