Sunday, January 31, 2016

TestAccessModifiersExample3

package TestAccessModifiers;

import accessModifiers.TestAccessModifiers;

public class TestAccessModifiersExample3 extends TestAccessModifiers{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TestAccessModifiersExample3 obj = new TestAccessModifiersExample3();
obj.test1();
obj.test4();

System.out.println(obj.a);
System.out.println(obj.d);

/**
* Here test2() and test3() is not inherited by child class
* when we are trying to access members of class from different package class,
* we can access only public and protected members with inheritance.
*/

/**
* Through inheritance our child class can inherit only public and protected members
* when we are in different
* package
*/

/**
* only difference between protected and default access is, when we are in different
* package and when we are using inheritance , our child class will inherit
* protected members not default members.
*
*/

/**
* when we are in same
* package and when we are using inheritance , our child class will inherit
* protected and default members. it will inherit both access type
*
*/




}

}

TestAccessModifiersExample2

package accessModifiers;

public class TestAccessModifiersExample2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TestAccessModifiers obj = new TestAccessModifiers();

obj.test1();
obj.test2();
obj.test4();

System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.d);




}

}

TestAccessModifiersExample1

package accessModifiers;

public class TestAccessModifiersExample1 extends TestAccessModifiers{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TestAccessModifiersExample1 obj = new TestAccessModifiersExample1();

obj.test1();
obj.test2();
obj.test4();

System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.d);

}

}

TestAccessModifiers

package accessModifiers;

public class TestAccessModifiers {

public int a;
int b;
private int c;
protected int d;

public void test1() {
System.out.println(" I have public access");
}

void test2() {
System.out.println(" I have default access");
}

private void test3() {
System.out.println(" I have private accesss");
}

protected void test4() {
System.out.println(" I have protected access");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestAccessModifiers obj = new TestAccessModifiers();
obj.test1();
obj.test2();
obj.test3();
obj.test4();

}

}

ChildClass6

package inheritance;

public class ChildClass6 extends ParentClass6{


ChildClass6(){
super(6);
System.out.println("I am from child class constructor");
}



public static void main(String[] args) {
// TODO Auto-generated method stub

ChildClass6 obj = new ChildClass6();

}

}

ParentClass6

package inheritance;

public class ParentClass6 {


ParentClass6(){
System.out.println("I am from parent class default constructor");
}

ParentClass6(int a){
this();
System.out.println("I am from parent class Parametrised constructor");
}



}

ChildCalss5

package inheritance;

public class ChildCalss5 extends ParentClass5{



ChildCalss5() {
super(7);
System.out.println(" I am from child class constructor");
}

void test2(){
System.out.println("I am from child class");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

ChildCalss5 obj = new ChildCalss5();



}

}

ParentClass5

package inheritance;

public class ParentClass5 {

ParentClass5(int a){
System.out.println(" I am from parent class constructor");
}

void test1(){
System.out.println(" I am from parent calss");
}

}

ChildClass4

package inheritance;

public class ChildClass4 extends ParentClass4{

    int k = 90;

static int m = 70;

public void test4(){
System.out.println(" I am from child class test4");
}


public static void main(String[] args) {
// TODO Auto-generated method stub

ChildClass4 obj1 = new ChildClass4();

ParentClass4 obj = new ChildClass4();

obj.test1();
obj.test2();


obj1.test1();
obj1.test2();
obj1.test4();


}

}

ParentClass4

package inheritance;

public class ParentClass4 {
int a = 70;
int b = 50;

static int c = 60;
static double d = 60;

void test1(){
System.out.println(" I am from parent calss test1");
}

void test2(){
System.out.println(" I am from parent class test2");
}



}

ChildClass3

package inheritance;

public class ChildClass3 extends ParentClass3{

int k = 90;

static int m = 70;

public void test4(){
System.out.println(" I am from child class test4");
}

public static void test5(){
System.out.println(" I am from child class test5");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

ChildClass3 obj = new ChildClass3();

   
   
     obj.test1();
     obj.test2();
     obj.test4();
   
   
   
     System.out.println(obj.k);

}

}

ParentClass3

package inheritance;

public class ParentClass3 {

int a = 70;
int b = 50;

static int c = 60;
static double d = 60;

void test1(){
System.out.println(" I am from parent calss test1");
}

void test2(){
System.out.println(" I am from parent class test2");
}

static void test3(){
System.out.println(" I am from parent class test3");
}



}

ChildClass2

package inheritance;

public class ChildClass2 extends ParentClass2{

int c;

void test3(){
System.out.println(" I am from child class test3");
}


public static void main(String[] args) {
// TODO Auto-generated method stub

ChildClass2 obj = new ChildClass2();

obj.test1();
obj.test2();
obj.test3();

System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);

}

}

ParentClass2

package inheritance;

public class ParentClass2 {

int a = 10;

int b = 20;

void test1(){
System.out.println(" I am from parent calss test1");
}

void test2(){
System.out.println(" I am from parent class test2");
}

}

ChileClass1

package inheritance;

public class ChileClass1 extends ParaentClass1{



   
public static void main(String[] args) {
// TODO Auto-generated method stub

ChileClass1 obj = new ChileClass1();

obj.test1();
obj.test2();
System.out.println(obj.a);

System.out.println(obj.b);



}

}

ParaentClass1

package inheritance;

public class ParaentClass1 {

    int a;
    int b;
   
   public void test1(){
    System.out.println("I am form parent class");
    }
   
    public void test2(){
    System.out.println(" I am from paraent class");
    }
   
   
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

PrePostIncrementExample4

package LearnJava;

public class PrePostIncrementExample4 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int i = 0;

int j = i++ + i+ i;

System.out.println(j);

//output = 2

int k = 0;

int m = k++ + k + k++ + k;

System.out.println(m);

//output = 4
int l = 0;

int p = l-- + l-- + l-- +l;

System.out.println(p);

//output = -6

int o = 0;

int z = ++o + ++o + --o + o-- + o++ +o;

System.out.println(z);

//output = 6


}

}

PrePostIncrementExample3

package LearnJava;

public class PrePostIncrementExample3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int i = 2;

int k = --i;

int m = i--;

System.out.println(k);
System.out.println(m);

System.out.println(i);

}

}

PrePostIncrementExample2

package LearnJava;

public class PrePostIncrementExample2 {

/**
* @param args
*/
public static void main(String[] args) {
int i = 0;

int j = 0;

int k = j++;

int m = ++i;

System.out.println(k);
System.out.println(m);
System.out.println(j);

}

}

PrePostIncrementExample1

package LearnJava;

public class PrePostIncrementExample1 {


public static void main(String[] args) {

int i = 0;
int j = 0;

System.out.println("value of i is:-"+i);
System.out.println("value of J is:-"+j);

System.out.println("value of i is:-"+ ++i);
System.out.println("value of J is:-"+ ++j);

System.out.println("value of i is:-"+ i++);
System.out.println("value of J is:-"+ j++);

System.out.println(i);
System.out.println(j);


}

}

TestReturnTypeInJava

package LearnJava;

public class TestReturnTypeInJava {

void test1() {
System.out.println("I have void return type");

}

public int test2(int a) {
System.out.println("I am returning integre value");
return a;
}

boolean test3() {
System.out.println("I am returning boolen value");
return true;
}

public TestReturnTypeInJava test4() {
return this;
}

char test5() {
return 'c';

}

double test6(int a, int b) {
return a + b;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestReturnTypeInJava obj = new TestReturnTypeInJava();

obj.test1();
int c = obj.test2(7);
boolean d = obj.test3();

}

}

TestMethodOverLoading

package LearnJava;

public class TestMethodOverLoading {
int a;
float b;
double d;

TestMethodOverLoading obj;
TestMethodOverLoading obj1;
TestMethodOverLoading obj2;


void test1() {
System.out.println("I am method with no argument");
}

void test1(int a) {
System.out.println("I am method with one argument");
}

void test1(int a, int b) {
System.out.println("I am method with two argument");
}

void test1(TestMethodOverLoading a) {
System.out.println("I am method with class type argument");
}

void test1(TestMethodOverLoading a, TestMethodOverLoading b) {
System.out
.println("I am method with class type argument with two argument");
}

void test1(int a, double b) {
System.out.println("I am method with two argument");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestMethodOverLoading obj = new TestMethodOverLoading();

obj.test1();
obj.test1(5);
obj.test1(obj);
obj.test1(obj, obj);

}

}

TestMethodOverLoadingExample2

package LearnJava;

public class TestMethodOverLoadingExample2 {

public double myMethod(int num1, int num2) {
System.out.println("First myMethod of class Demo");
return num1 + num2;
}

public int myMethod(int var1, short var2) {
System.out.println("Second myMethod of class Demo");
return var1 - var2;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestMethodOverLoadingExample2 obj = new TestMethodOverLoadingExample2();

obj.myMethod(4, 7);

}

}

TestMethodOverLoadingExample1

package LearnJava;

public class TestMethodOverLoadingExample1 {


public void add(int p, int m) {
System.out.println("I am method with integre argument");
System.out.println(p + m);
}

public void add(double p, double m) {
System.out.println("I am method with double argument");
System.out.println(p + m);
}

public void add(float p, float m) {
System.out.println("I am method with float argument");
System.out.println(p + m);
}

public float add(int i, long p){
System.out.println("I am from method where return type is integer");
return p;


}

public boolean add(boolean i, boolean p){
System.out.println("I am from method where return type is boolean");
return true;

}

public static void main(String args[]) {
TestMethodOverLoadingExample1 obj = new TestMethodOverLoadingExample1();
obj.add(20.5, 21.5);
obj.add(20f, 20f);
obj.add(20, 20);
obj.add(8, 90L);
obj.add(false, true);

}

}

DataTypeExample1

package LearnJava;

public class DataTypeExample1 {




double d = 8976.98;
float f = 987.923F;
long l = 11111111111L;
int i = 1111111111;
short s;
byte by;
char c = 'A';
boolean b = false;



double doubledefaultValue;
float floatdefaultValue;
long longdefaultValue;
int intdefaultValue;
short shortdefaultValue;


//double>float>long>int>short>byte>char>boolean;


/**
* double:
* double data type is a double-precision 64-bit IEEE 754 floating
* point.
*
* This data type is generally used as the default data type for decimal
* values, generally the default choice.
*
* Double data type should never be used for precise values such as
* currency.
*
* Default value is 0.0d.
*
* Example: double d1 = 123.4
*
* @param args
*/

/**
* float:
* Float data type is a single-precision 32-bit IEEE 754 floating
* point.
*
* Float is mainly used to save memory in large arrays of floating point
* numbers.
*
* Default value is 0.0f.
*
* Float data type is never used for precise values such as currency.
*
* Example: float f1 = 234.5f

*/


/**
* For long
* Long data type is a 64-bit Minimum value is
* -9,223,372,036,854,775,808.(-2^63) Maximum value is
* 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
*/

/**
* For Int
* Int data type is a 32-bit Minimum value is -
* 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31 -1)
*/


/**
* boolean:
* boolean data type represents one bit of information.
*
* There are only two possible values: true and false.
*
* This data type is used for simple flags that track true/false conditions.
*
* Default value is false.
*/

/**
* char:
* char data type is a single 16-bit Unicode character.
*
* Minimum value is '\u0000' (or 0).
*
* Maximum value is '\uffff' (or 65,535 inclusive).
*
* Char data type is used to store any character.
*
* Example: char letterA ='A'
*
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub

DataTypeExample1 obj = new DataTypeExample1();
System.out.println("long value is:-"+obj.l);
System.out.println("float value is:-"+obj.f);
System.out.println("double value is:-"+obj.d);
System.out.println("char valus is:-"+obj.c);
System.out.println("********************************");

System.out.println("Double default value is:-"+obj.doubledefaultValue);
System.out.println("Float default value is:-"+obj.floatdefaultValue);
System.out.println("Long default value is:-"+obj.longdefaultValue);
System.out.println("Integer default value is:-"+obj.intdefaultValue);
System.out.println("Short default value is:-"+obj.shortdefaultValue);
System.out.println("boolean default value is:-"+obj.b);
System.out.println("********************************");



System.out.println("Double size is:-"+Double.SIZE);
System.out.println("Float size is:-"+Float.SIZE);
System.out.println("Long size is:-"+Long.SIZE);
System.out.println("Integer size is:-"+Integer.SIZE);
System.out.println("Short size is:-"+Short.SIZE);
System.out.println("Byte size is:-"+Byte.SIZE);
System.out.println("Character size is:-"+Character.SIZE);

System.out.println("**-------*********************************");
System.out.println("Double MIN_VALUE is "+Double.MIN_VALUE);
System.out.println("Double MAX_VALUE is "+Double.MAX_VALUE);

System.out.println("Float MIN_VALUE is "+Float.MIN_VALUE);
System.out.println("Float MAX_VALUE is "+Float.MAX_VALUE);

System.out.println("Long MIN_VALUE is "+Long.MIN_VALUE);
System.out.println("Long MAX_VALUE is "+Long.MAX_VALUE);

System.out.println("Integer MIN_VALUE is "+Integer.MIN_VALUE);
System.out.println("Integer MAX_VALUE is "+Integer.MAX_VALUE);



}

}

DataType

package LearnJava;

public class DataType {

double d = 0.0d;
float f = 0.0f;
long l = 0;
int i = 0;
short s = 0;
byte b = 0;
char c = '\u0000';
boolean bo = false;

DataType obj;

// double>float>long>int>short>byte>char>boolean;

/**
* class name should start with uppercase letter and be a noun
* interface
* name should start with uppercase letter and be an adjective
* method name
* should start with lowercase letter and be a verb
* variable name should
* start with lowercase letter
* package name should be in lowercase letter
* constants name should be in uppercase letter
*/

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

ThisUses

package Constructor;

public class ThisUses {

int a;
int b;

void test1(int a, int b){
this.a = a;
this.b = b;
}

void test2(int p, int H){
a = p;
b = H;
}

void display(){
System.out.println("Value of a and b:-"+a+","+b);
}
public static void main(String[] args) {

ThisUses obj = new ThisUses();

//obj.test1(4, 5);

obj.test2(6, 7);

obj.display();

}

}

TestThisKeyword

package Constructor;

public class TestThisKeyword {

int a;
int b;
int c;
static int d;

TestThisKeyword() {
System.out.println("I am default constructor");
}
TestThisKeyword(int a, int b) {
this();
System.out.println("I am from parameterised constructor with 2 argument");
this.a = a;
this.b = b;

}

TestThisKeyword(int a, int b, int c){
 this(a,b);
 System.out.println("I am from parameterised constructor with 3 argument");
 this.c = c;
}

void display(){
System.out.println("value of a, b, c is:-"+a+","+b+","+c);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThisKeyword obj = new TestThisKeyword(2,3,4);
obj.display();
}

}

TestThisinJava

package Constructor;

public class TestThisinJava {

int a;
int b;
int c;
static int d;

void test1() {
this.test2();
System.out.println("I am test one");
}

void test2() {

System.out.println("I am test two");
}

static void test3(int a) {

System.out.println(" I am test three");
}

void test4(TestThisinJava obj) {
System.out.println("method is invoked");
System.out.println(obj.getClass().getName());

}

void test5() {
test4(this);
test3(4);
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestThisinJava obj = new TestThisinJava();
obj.test5();

obj.test1();


}

}

TestConstructor1

package Constructor;

public class TestConstructor1 extends TestConstructor{

int a;
int b;
int c;

TestConstructor1() {
super();
System.out.println("I am default constructor");
}

TestConstructor1(int a) {
this.a = a;
System.out.println("I am integer constructor");
}

TestConstructor1(String a) {

System.out.println("I am String constructor");
}

TestConstructor1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}

void display() {

System.out.println("The value of a, b, c is:-" + a + "," + b + "," + c);
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestConstructor1 obj1 = new TestConstructor1(5);
TestConstructor1 obj2 = new TestConstructor1("Bhanu");
TestConstructor1 obj = new TestConstructor1(2, 3, 4);

obj.display();
}

}

TestConstructor

package Constructor;

public class TestConstructor {

int a;
int b;


TestConstructor() {
System.out.println("I am from parent default constructor");
}

TestConstructor(int a){
this.a = a;
System.out.println("I am from parent parameterized constructor");
}


TestConstructor(int a,int b){
this.a = a;
this.b = b;
}
void display(){
System.out.println("value of a is:-"+a);
}

void sum(){
int c = a+b;

System.out.println("sum of two number is :-"+c);
}
public static void main(String[] args) {
// TODO Auto-generated method stub

TestConstructor obj = new TestConstructor();

obj.display();
TestConstructor obj1 = new TestConstructor(5);

obj1.display();

TestConstructor obj2 = new TestConstructor(5,10);

obj2.sum();
}

}

Test2

package Constructor;

public class Test2 extends Test1{

Test2() {
//super();
super("Bhanu");
System.out.println("I am from child default constructor");
}

Test2(int a){
this();
System.out.println("I am from child paremeterised constructor");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

//Test2 obj = new Test2();

Test2 obj = new Test2(5);

}

}

Test1

package Constructor;

public class Test1 {

Test1() {
System.out.println("I am from parent default constructor");
}

Test1(int a) {
System.out.println("I am from parent parameterized constructor");
}

Test1(String c){
System.out.println("I have string argument");
}

}

callMethodFromOtherClass

package ClassAndObject;

public class callMethodFromOtherClass {

public static void main(String[] args) {
TestMethod obj = new TestMethod();
obj.method1();
obj.method2();
TestMethod.method3();
}

}

TestClassAndObject

package ClassAndObject;

public class TestClassAndObject {


int a = 10;
int b = 20;
String c = "Bhanu Pratap";


public static void main(String[] args) {

TestClassAndObject obj = new TestClassAndObject();

System.out.println(obj.a);

System.out.println(obj.c);


}


}

TestMethod

package ClassAndObject;

public class TestMethod {



void method1(){
System.out.println("I am from Method1");
}

void method2(){
System.out.println("I am from Method2");
}

static void method3(){
System.out.println("I am from Method3");
}

public static void main(String[] args) {
// TODO Auto-generated method stub

TestMethod obj1 = new TestMethod();
TestMethod obj2 = new TestMethod();


obj1.method1();
obj1.method2();

System.out.println("------------");

obj2.method1();
obj2.method2();

System.out.println("-------");

TestMethod.method3();

}

}

TestSIBandIIB

package ClassAndObject;

public class TestSIBandIIB {
static int i;
int b;

static {
i = 10;
//b = 20;
System.out.println("I am from SIB1");
}
static {
System.out.println("I am from SIB2");
}

{
b = 20;
i = 70;
System.out.println("I am from IIB1");
}

{
System.out.println("I am from IIB2");
}

public static void main(String[] args) {
// TODO Auto-generated method stub
TestSIBandIIB obj1 = new TestSIBandIIB();
System.out.println("--------");
TestSIBandIIB obj2 = new TestSIBandIIB();
System.out.println("----------");
TestSIBandIIB obj3 = new TestSIBandIIB();

System.out.println(obj3.b);
}

}

TestObject

package ClassAndObject;

public class TestObject {

/**
* @param args
*/

static int a = 30;
int b = 40;

public static void main(String[] args) {
// TODO Auto-generated method stub
TestObject obj = new TestObject();

System.out.println(obj.b);

System.out.println(TestObject.a);
}

}