Saturday, 13 January 2018

4.11 Ploymorphism

Poly - many
Morph - Form (behaviour)

Polymorphism is the capability of a method to do different things based on the object.


class polydemo
{
public static void main(String args[])
{
A obj=new A();
obj.show(5);
B obj1=new B();
obj1.show();
}
}

class A
{
//Overloading - Early binding - Static binding - Compile Time
public void show()
{
System.out.println("HELLO");
}
public void show(int i)
{
System.out.println(i);
}

public void show(double i)
{
System.out.println(i);
}
}
class B extends A
{
//Overriding -- late binding -- dynamic binding -- run time 
public void show()
{
super.show(); // it will call show method of the superclass
System.out.println("WORLD");
}
}

Read More »

4.10 Inheritance

Inheritance - When one class(subclass) acquires the properties of other class (Super class) is called inheritance.

Syntax:

class SuperClass {
   .....
   .....
}
class SubClass extends SuperClass {
   .....
   .....
}


Multiple Inheritance in java is possible using Interface.

class A extends B, C not allowed
class A extends B implements C


super() is by default in every constructor first line but it is hidden.
When you create the object of subclass, because of super() method ,it will first call the default constructor of super class .




class A
{
public A()
{
System.out.println(" Default Constructor of A");
}

public A(int n)
{
System.out.println(" Parameterized Constructor of A");
}
}

class B extends A
{
public B()
{
System.out.println(" Default Constructor of B");
}

public B(int n)
{
//super(3);
//Parametrised Constructor of A
//Parametrised Constructor of B

System.out.println(" Parametrised Constructor of B");
}
}

class superdemo
{
public static void main(String args[])
{
A obj1=new A(); 
//Default Constructor of A

A obj2=new A(5); 
//Parametrised Constructor of A

B obj3=new B();
//Default Constructor of A
//Default Constructor of B

B obj4=new B(1);
//Default Constructor of A
//Parametrised Constructor of B

A obj5=new B(); // A type of reference for object of B class
//B obj6=new A(); superdemo.java:50: error: incompatible types: A cannot be converted to B
}

}

Read More »

Sunday, 7 January 2018

4.9 Encapsulation

Encapsulation : Binding of data members and member function into a single unit is called encapsulation.

Java suggests that, Your variables in your classes should be private so that no one can access it or modify it. It should only modify using methods.

getter and setter is a method to perform encapsulation.


class Emp
{
private int empId;
private String empName;

public void setEmpId(int empId)
{
this.empId=empId;
}
public int getEmpId()
{
return empId;
}

public void setEmpName(String empName)
{
this.empName=empName;
}
public String getEmpName()
{
return empName;
}
}
class encapsulation
{
public static void main(String args[])
{   Emp obj1=new Emp();
obj1.setEmpId(1);
obj1.setEmpName("Bikes");

Emp obj2=new Emp();
obj2.setEmpId(2);
obj2.setEmpName("Amitesh");

System.out.println("Emp Id = "+obj1.getEmpId()+" & Name = " + obj1.getEmpName());
System.out.println("Emp Id = "+obj2.getEmpId()+" & Name = " + obj2.getEmpName());
}
}
Read More »

4.8 Static keyword in java

Static - It is a keyword in java which can be use with variables, methods and block.

STATIC VARIABLE : A static variable will share the same memory location with all the object.

Note : A static method can't use the non static variable.


class test1
{
static int i=1;
int j=1;
public int display()
{
i++;
return i;
}
public int show()
{
j++;
return j;
}
}

class staticdemo
{
public static void main(String args[])
{
test1 obj1=new test1();
System.out.println("Value of i = "+obj1.display());

test1 obj2=new test1();
System.out.println("Value of i = "+obj2.display());

test1 obj3=new test1();
System.out.println("Value of j = "+obj3.show());

test1 obj4=new test1();
System.out.println("Value of j = "+obj4.show());

}
}

STATIC METHOD : if you want to call a method without instantiating the class or you can say without creating the object of a class then we make the method static.

syntax to call a static method : classname.method();

WHY MAIN METHOD IS STATIC - Execution of the program starts with main. JVM calls the main method, but we all know that to call any method we need to create the object of that class, which is not possible for JVM, therefore main method is static.

STATIC BLOCK :  when you want to initialize the value of a static variable then you have to use static block.

static block runs before main method because it loads first when your class is loaded.
compiler will print "Hello World1" then "Hello World2" in below code.

class Test {
    static {
        System.out.println("Hello World1");
    }

    public static void main(String[] args){

        System.out.println("Hello World2");
    }
}

Can we count how many objects created for a class ??
Yes we can . hint : by using default constructor with static incremental variable.
Read More »

4.7 javap command

javac - This command is use to compile your java code, which converts your .java into .class file.

But is this possible to do the reverse ?

Yes it is possible - I have seen a lots of decompiler software on the internet through which it is possible.

is javap is responsible ??

javap - This command will give you only the class structure not the code logic.

You can see the class structure of in built classes as well.


Read More »

Saturday, 6 January 2018

4.6 Access modifiers in java

Access modifiers in Java - scope of accessibility for class, methods, variables etc.

  1. Public
  2. Protected
  3. Default
  4. Private

Read More »

4.5 Package in Java

Package : It is a collection of similar type of classes.

syntax to create package : package packagename;

to compile it : javac -d . package_demo.java


package package_demo;

class package_demo
{
public void display()
{
System.out.println("Hello World");
}

public static void main(String args[])
{
package_demo obj=new package_demo();
obj.display();
}
}



to import a package :

import java.sql.*;

Read More »

4.4 Object passing in java

Passing an object in java is interesting concept - you can pass a whole object to a method.





class Bollywood
{
void bset(Hollywood h)
{
h.hset("Pyar kiya to darna kya");
}
}

class Hollywood
{
String movie;

public void hset(String m)
{
movie=m;
}

public String hshow()
{
return movie;
}
}
class object_passing_example
{
public static void main(String args[])
{
Hollywood obj1=new Hollywood();
obj1.hset("A walk to remember");
System.out.println(obj1.hshow());

Bollywood obj2=new Bollywood();
obj2.bset(obj1);
System.out.println(obj1.hshow());
}
}
Read More »

4.3 Constructor

Constructor : 
 A method with the same name of class is known as constructor.

Thumb rules for constructor -
1. Constructor is a method
2. Constructor have same name as class
3. Constructor will never return any value therefore no return type is required.
4. Constructor will automatically called once you create the object of the class
5. Constructor allocates the memory to the object.
6. We can have more than one constructor in a class by using constructor overloading
7. You can't call a constructor expicitly
8. Implicit conversions are possible in constructors.
9. You can initialize default values of variables.




Constructor example

 class constructor_example
{
public static void main (String args[])
{
A obj=new A(); //Default constructor
A obj1=new A(2); //Constructor with one parameter
}
}

class A
{
public A()
{
System.out.println("Default constructor");
}

public A(int i)
{
System.out.println("Constructor with one parameter");
}

public A(double d)
{
System.out.println("Constructor with double");
}
}
Read More »

4.2 Object creation

Everything in java treated as an object.

Syntax to create an object of a class :

A obj = new A()

A = Class name for which you want to create object
obj = Reference variable
new = keyword to create an object
A() = constructor

"new A()" is an object or you can call it instance.
obj is a reference variable which stores the address of this object.
obj is a reference variable and it is stored in a stack memory.
new A() is an object which is stored in heap memory.


Read More »