Monday, 28 April 2014

Constructor,Super(),this() in java



->The constructor is the first block of code. its called once the object created.

-> Constructor executes only once in the object lifecycle. its not execute after that.

-> it has the same name as the class name.

-> it can accept some arguments.

-> There is no return type for constructor. not even void.

-> if the class doesnot have the constructer, the default constructor will be generated by compiler with empty argument list. so only below code get execute.

car c new car();

-> if the class have any constructor, then its should match with constructor argument list and argument list while create object both should same, If it not match with any constructor argument then that statement will not get compiled.

car c new car(); //its not execute if class have any constructor.

car c new car(a,b); //Its execute if matched with constructor argument.




Super and this keyword

-> Super() is used to call parent class constructor.
this() is used to call same class constructor.

-> Super (with out paranthesis) is used to call parent class methods and variables.
this (with out paranthesis) is used to call same class methods and variables.

-> Both this and super not come together.

-> Super() and this() should be mentions in first line of constructor.



Java Arrays



Collection of similiar data types.

1) Declaring Arrays

int[] marks;

Also int marks[]; can possible. but its less readable. so we should not use this format.

2) Construction

marks = new int[5];

Array is the object. so we should use "new" keyword for construct the array. The size of the array is mandatory.

int[] marks=new int[5];

3) Initialization:

marks[0]=34;
marks[1]=37;
marks[2]=32;
marks[3]=30;
marks[4]=31;

Array index starts with 0 and ends with indexvalue-1.
We cannot use negative or maximum index value.


4) Initialization of objects

class pen {
pen[] pens=new pen[3];
}

we can create three object for above statement like pens[0],pens[1],pens[2].


5) Array intializer

if we know the values, we can give the value directly.

int[] marks={12,45,23,67}

Java Packages



-> Java pakages is a machanism for oraganizing java classes.

-> Pakaging name should start with reverse of our domain name.

Ex: The pakage for arunrajvdm.blogspot.com\drafts is

Pakage name = com.blogspot.arunrajvdm.drafts

-> Pakage statement should be on top of the java class file.

-> "import" statement is helpful to add pakage in class file.

-> There is no statement allowed in between the pakage statement and import statement while create the pakage.

Pakage com.mail;  \\
import com.blogspot.arunrajvdm.drafts; \\ here drafts is class. com,blogsport,arunrajvdm are pakages.

public class calc{
calc(){
System.out.println("hi");
}
}

Here not statement allowed in between import and pakage.


-> com.blogspot.arunrajvdm.*; - this statement says we can access all the available class inside the arunrajvdm pakage. But not sub pakage inside the arunrajvdm pakage.

Java Non Access modifiers



1) Final

-> Final can be applied in class, Methods, instance variable and local variables

-> If class marked as final, we cannot extended. That means we cannot create suib class for that.

-> IF method marked as final, we cannot override the method.

-> If variable marked as final, we cannot change the value.

-> If object reference as final, we cannot asign the new object or any value into the reference.


2) Static

-> Static can be applied for methods and variables.

-> If we are using static for methods or variables, all the objects are sharing the same memory location and refer those values. If we are not using static for methods or variablese, all objects create a new memory location and use those referances.

-> The methods or variables marked as static belongs to the class file memory location.

-> The static memeber should be accessed using the class name.

Ex:

public class ford{
public static void printCarInfo() {
System.out.println("Car Name: Ford);
}
}

Public class fordshowroom{
ford.printcarifo();
}

The above example method printcarinfo() is just print the message. If we are using object for this method, its crreates the seperate memory location for all instance for doing the same process for print those same message. So its waste of the memory location.

But if we are using the static for above method, all the object sharing the same memory location for print the same line of message. Static methods and static variables using .class file memory location. This is one advantage of save the memory allocations.

-> The static variables and methods are called class members. Because both are using the .class file memory location. But non-static variables and non-static methods are called as object members. Because all thos non-static variables referring the respective object memory location.

-> The method marked as static, they can access the other static methods and variables directly inside the same class.

-> Any object instance access the static variables, But the changes will reflect to all the objects.

-> Members marked as static can be final.



3) abstract

-> abstract can be applied for classes and methods only.

-> When a method is marked as abstract- Its should not have any implementation. So its end with ';'. So we have implemenation in sub class. So the abstract method should overriden in subclass or should marked as abstract.

-> when class is marked as abstract- we are not able to create object for those class. But we can create sub class and use those abstract class members.

-> if we have single abstract method in class, we should marked the whole class as abstract.

-> But the class can be marked as abstract even we dont have any abstract method in those class.

-> Abstract class can have concrete class(Non-abstract) methods on it.

-> Abstract methods cannot be marked as final.

-> Abstract class cannot be marked as final.

-> Abstract methods cannot be static.

-> Abstract methods cannot be private.

-> we cannot create object (instance) of abstract class. because abstract class have the incomplete method. its does not have the fully implemented method. so if we call the method by using object, its means incomplete task. so we cannot create instance for abstract class.

4)Strictfp

-> Strictfp is declared for methods and classes. Its have the implementation.

-> when declared inside a class or method, its confirm that those class or methods followed IEEE754 standared while writing code. and which makes methods or class behave in a platform independent way regarding the floating points.

-> strictfp cannot be used withh abstract.


5) native

-> Native modifier can only be applied to methods.

-> native method will always platform dependent code like C.

-> Its doesnot have any implementation. Its end with ;.

-> The native method implementation is omitted.


6) synchronized 

-> Synchronized can only be applied for methods.

-> Synchronized only allow one single thread to execute the code at a time.

-> Synchronized can be used with any access modifier.


7) transient

-> only instance variable marked as transient.

-> The variable marked as transient will not be serialized.


8) Volatile

-> only instance variable marked as volatile.


The following access and non access modifiers applicable for below item

Class - public,default,abstract,strictfp,final

Methods - public,protected,default,private,static,final,strictfp,native,synchronized,abstract

Instance variable - public,protected,default,private,static,final,transient

Local Variable - final

Access Modifiers in JAVA


1) Access modifiers helps to implement concept of encapsulation in oops.

2) Encapsulation helps to hide the data and behavior.

3) Access modifiers in java are
-> public
-> Private
-> Protected
-> default

4) Access modifiers is not applicable for local variables.

5) Access modifiers effect the accessbility in two levels
-> class
-> Members (Method and instance variable)



Access modifiers at class level

1) Public

-> when class is marked as public, it is visible to all other classes which are inside and outside

-> Its accessible to all class in other pakages, but should mention in import class.

-> There is no need import statement when a class used inside the pakage uses the class marked as public.


2) Default

-> Defult access has no keyword.

-> when there is no modifier declared, the access is defult level access.

-> The class marked with default access are accessible only to the class inside the same pakage. The outside pakage is not accessbile by the default modifier.


3) Private and protected

Private and protected is not applicable to class level declaration.



Access modifiers at member level

1) Public

-> The member with public class is access by any class inside and outside of the pakage.

-> if we want to use public member then we should import the pakage for the class.

-> If the class itself default modifier then we cannot use the public member in the class for outside the pakage. But we can use the default access modifier class have the public member inside the same pakage.



2) Default

-> The member with default access will be access to class that are in same pakage.

-> The member with default access will not be access to class that are in outside pakage.


3) Protected 

-> The member marked as protected is accessible all class in same pakage (same as default).

-> Protected members are also accessible by class outside of the pakage. But the accessing class should be a subclass of the member class.

-> Its used to acheive inheritance concept in java.


4) Private

-> When member marked as private, it is accessible only to the members inside the same class.

->  When member marked as private,other class inside and outside the pakage will not be able to access the private member of class.


Overloading in Java



1) overloading is applicable only in java.

2) Its applicable between the methods in same class or super class.

3) Overloading helps to implement the polymorphism concept in java.

4) overloading allows to define more methods in same name, so that its give many options to choose method.


overloading Rules:

1) overloading method argument types or argument number or return type should change in argument list.

2) Include anyone option in point (1), access modifiers also can change.

3) Overloading methods can have throws exceptions.

Ex:
class calc{
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b)
{ c=a+b; return c; }
int division(int a,int b, int c)
{ d=a+b+c; return d; }
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b,int c) throws ArithmaticException
{ c=a+b; return c; }
}



Overloading in Constructor:

1) constructor always can be overloaded.

2) overriding is not possble as oer rules of overloading.

Interfaces in JAVA




1) Interface does not have any method implementation.

2) All methods in an interface are public and abstract.

3) All the methods in interface should override in subclass. If sub class is not override interface means is called abstract class.

Ex: Public interface mother{
Public abstract void cooking();
}

    public class teacher implements mother
    {
Public void cooking(){
System.out.println("cook well");
}
    }

4)  we cannot use follwing access modifiers in interface methodPrivate,protected,final,static,synchronized,native,strictfp. That means interface method only allowed public and abstract.

5) By defaulr Interface variables are public,final and static

6) No multiple inheritance in java - Cannot extend more than one class at a time. But we can implements the more than one interface into the class.

Ex: The object teacher have many role(type) like teacher,Mother,housewife etc.

So its difficult to use all types in single class. Here we are using interface for implement those types.

Public class teacher extends mother implements housewife,citizen
{ }

So here we cannot extends more than one class at a time for reusability. But we can implements the more than one interface into the class.



7) Interface can extend any number of interfaces

Ex: Public interface car extends vehicle,fourwheeler{
}

Remember if we implements car interface we should override all interface like car,vehicle,fourwheeler


7) Any number of interface can be implemented by concrete class or abstract class.



8) Usage of Interface:

Ex: Selenium suports IE,Firefox,opera.. Here we can use common interface (WebDriver) for implement the function (Get()) for selenium interacts with muliple browser. In this way we can easily use the same variables and functions while developing the script.


Syntax:

Public interface calc{
int number=99;   // By default all fields are public and static
public abstract void add();
public abstract void add();
}



Interface


1) used to define the methods. Those methods are access by the class.
2) The main class able to access the all methods present in class and interface if we defined the object using class
3) But we not able to use methos which is not presented in interface if we defined object using interface.

public interface mobilphone

{
public void call();
public void recharge();
}

public class interfaceclass implements mobilephone

{

public void call()
{ system.out.println("call");

Public void recharge()
{ system.out.println("Rechaege");

public void ratecutter()
{ system.out.println("Rate cutter");

}


public class mainclass1
{
public static void main(String args[])
{
interfaceclass obj1=new interfaceclass();
obj1.call();
obj1.recharge();
obj1.ratecutter();


mobilphone obj2=new interfaceclass();
obj2.call();
obj2.recharge();
obj2.ratecutter();  // we not able to use methos which is not presented in interface if we defined object using interface.

}