EXPLANATION :Simple Explanation of Above Code ! Dissecting into its Skeleton System
|
public class file1
{
}
1) The above line tells that we are creating a new java class named file1
2) keyword public is an access specifier which tells that this class is available
anywhere within the program .
3) method is nothing but a what we call function in php or any other programming language like C
public class file1
{
public static void main (String[] args)
{
}
}
4) Our class named file1 has a method named main and please note main is the first method
executed by the java virtual machine or in fact java virtual machine looks for main method first
5) the main method is public , and its also static means its a class method and not object method (This will be explained in depth later)
6) void means this function or method main returns nothing
main (String[] args)
7) now this main method has an argument array which is of String Type that is passed from your command line,
to make it understand better let us change the program little..
public class file2
{
public static void main(String[] args)
{
System.out.println("Argument 1 "+args[0]);
System.out.println("Argument 2 "+args[1]);
}
}
//Execute Argument e:\workarea\>java file2 message1 message2
|
E:\workarea>javac file2.java
E:\workarea>java file2 message1 message2
Argument 1 message1
Argument 2 message2
|
CLASSES : A class is a template for which u can create objects Definition: A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.
|
class classname
{
datatype instance-variablename1;
datatype instance-variablename2;
return_datatype method_name1 (parameter_list1)
{
//body of method_name1;
}
return_datatype method_name2 (parameter_list2)
{
//body of method_name2;
}
}
class myfamily
{
public static void main(String[] args)
{
String dads_name;
String moms_name;
int dads_age;
dads_name = "Rohith kelleher";
moms_name = "Christina Smith";
dads_age = 50;
// call whos_mysis method from main method
String mysis_name_ret=whos_mysis();
System.out.println("My Sisters Name : "+mysis_name_ret);
// call mysavings method from main method
int savings_amt_ret=mysavings(5000,3000);
System.out.println("My Savings Amount : $ "+savings_amt_ret);
}
public static int mysavings(int income, int expense)
{
int saving_amt = income-expense;
return saving_amt;
}
public static String whos_mysis()
{
String mysis_name = "Rubecca Kelleher";
return mysis_name;
}
}
|
E:\workarea>javac myfamily.java
E:\workarea>java myfamily
My Sister Name : Rubecca Kelleher
My Savings Amount $ 2000
|
OBJECTS : An Object is an Instance of Class , Definition: An object is a software bundle of variables and related methods.
|
1) We will move our above example farward to see Why we Infact need an Object anyway
2) We create a subclass named subclass_name outside the scope of our main class named myfamily2
3) We wish to pass some arguments into this subclass method named our_tot_savings from mainclass
4) So we create an Object named subclass_object using new Operator
5) The color coding that I have used will make it Visibly more clear to understand how the linking is done with ref to names
6) The new operator dynamically allocates memory for an object a run time
7) Infact delete - finalize operatord de-allocates memory when its no longer needed
class subclass_name
{
int our_tot_savings(int my_savings_amt_ret,int dads_savings_amt_ret)
{
int our_tot_sav_amt = my_savings_amt_ret+dads_savings_amt_ret;
return our_tot_sav_amt;
}
}
class myfamily2
{
public static void main(String[] args)
{
int our_tot_sav_amt;
// call whos_mysis method from main method
String mysis_name_ret=whos_mysis();
System.out.println("My Sisters Name : "+mysis_name_ret);
// call mysavings method from main method
int my_savings_amt_ret=mysavings(5000,3000);
System.out.println("My Savings Amount : $ "+my_savings_amt_ret);
// call dadssavings method from main method
int dads_savings_amt_ret=dads_mysavings(8000,4000);
System.out.println("Dads Savings Amount : $ "+dads_savings_amt_ret);
// calling Another Class outside this class using Object named subclass_object
subclass_name subclass_object = new subclass_name();
our_tot_sav_amt=subclass_object.our_tot_savings(my_savings_amt_ret,dads_savings_amt_ret);
System.out.println("our_tot_sav_amt : "+our_tot_sav_amt);
}
public static int mysavings(int income, int expense)
{
int saving_amt = income-expense;
return saving_amt;
}
public static String whos_mysis()
{
String mysis_name = "Rubecca Kelleher";
return mysis_name;
}
public static int dads_mysavings(int income, int expense)
{
int dads_saving_amt = income-expense;
return dads_saving_amt;
}
}
|
E:\workarea>javac myfamily2.java
E:\workarea>java myfamily2
My Sister Name : Rubecca Kelleher
My Savings Amount $ 2000
Dads Savings Amount $ 4000
our_tot_sav_amt $ 6000
|
CONSTRUCTORS : A Constructor initializes an Object immediately upon creation, it has the same name as that of the class it resides in
|
1) Now we make our family program grow more, using a Constructor
2) A constructor gets Executed even without being called by the main program unlike any other method in the subclass
class subclass_name
{
subclass_name()
{
System.out.println("constuctor of subclass_name class");
}
int our_tot_savings(int my_savings_amt_ret,int dads_savings_amt_ret)
{
int our_tot_sav_amt = my_savings_amt_ret+dads_savings_amt_ret;
return our_tot_sav_amt;
}
}
class myfamily2
{
public static void main(String[] args)
{
int our_tot_sav_amt;
// call whos_mysis method from main method
String mysis_name_ret=whos_mysis();
System.out.println("My Sisters Name : "+mysis_name_ret);
// call mysavings method from main method
int my_savings_amt_ret=mysavings(5000,3000);
System.out.println("My Savings Amount : $ "+my_savings_amt_ret);
// call dadssavings method from main method
int dads_savings_amt_ret=dads_mysavings(8000,4000);
System.out.println("Dads Savings Amount : $ "+dads_savings_amt_ret);
// calling Another Class outside this class using Object named subclass_object
subclass_name subclass_object = new subclass_name();
our_tot_sav_amt=subclass_object.our_tot_savings(my_savings_amt_ret,dads_savings_amt_ret);
System.out.println("our_tot_sav_amt : "+our_tot_sav_amt);
}
public static int mysavings(int income, int expense)
{
int saving_amt = income-expense;
return saving_amt;
}
public static String whos_mysis()
{
String mysis_name = "Rubecca Kelleher";
return mysis_name;
}
public static int dads_mysavings(int income, int expense)
{
int dads_saving_amt = income-expense;
return dads_saving_amt;
}
}
|
E:\workarea>javac myfamily2.java
E:\workarea>java myfamily2
My Sister Name : Rubecca Kelleher
My Savings Amount $ 2000
Dads Savings Amount $ 4000
constuctor of subclass_name class
our_tot_sav_amt $ 6000
|
OVERLOADING METHODS : 2 or more methods inside a same class with same name but different number of arguments is called method overloading
|
public class file3
{
public static void main(String[] args)
{
System.out.println("Calling method_name1 with no arguments");
method_name1();
System.out.println("Calling method_name1 with 1 arguments");
String firstname="James";
method_name1(firstname);
System.out.println("Calling method_name1 with 2 arguments");
String lastname="Smith";
method_name1(firstname,lastname);
}
static void method_name1()
{
System.out.println("Inside method_name1 with no arguments");
}
static void method_name1(String firstname)
{
System.out.println("Inside method_name1 with 1 argument : " +firstname);
}
static void method_name1(String firstname,String lastname)
{
System.out.println("Inside method_name1 with 2 argument : " +firstname+ " " +lastname);
}
}
|
E:\workarea>javac file3.java
E:\workarea>java file3
Calling method_name1 with no arguments
Inside method_name1 with no arguments
Calling method_name1 with 1 arguments
Inside method_name1 with 1 argument : James
Calling method_name1 with 2 arguments
Inside method_name1 with 2 argument : James Smith
|
OVERLOADING CONSTRUCTOR : Here constructor of same name is passed with more that one set of argument list
|
public class file4
{
file4()
{
System.out.println("file4 Constructor with no arguments ");
}
file4(String first_name)
{
System.out.println("file4 Constructor with 1 arguments :"+first_name);
}
file4(String first_name,String last_name)
{
System.out.println("file4 Constructor with 2 arguments :"+first_name+" "+last_name);
}
public static void main(String[] args)
{
file4 file4_object1 = new file4();
String first_name="James";
file4 file4_object2 = new file4(first_name);
String last_name="Smith";
file4 file4_object3 = new file4(first_name,last_name);
}
}
|
E:\workarea>javac file4.java
E:\workarea>java file4
file4 constructor with no arguments
file4 constructor 1 arguments : James
file4 constructor 2 arguments : James Smith
|
final : when a variable is declared final it cannot be re-initialised
|
public class file10
{
public static void main(String[] args)
{
int i =100;
i = i + 10;
System.out.println("i is : "+i); // Possible
final int j = 100;
j = j + 10;
System.out.println("j is : "+ j); // Not Possible gives Error
}
}
|
E:\workarea>javac file10.java
E:\workarea>cannot assign a value to final variable j
|
Static : Static methods can access only static variables, static methods gets executed once during class initialisation
|
public class file11
{
static int a =100;
int b = 200;
static
{
System.out.println("This is a Static method where a is "+a); // Works Fine
//System.out.println("This is a Static method where b is "+b); // Gives Error
}
public static void main(String[] args)
{
int c = 300;
System.out.println("This is main method where a is "+a); // Works Fine
//System.out.println("This is main method where b is "+b); // Gives Error
System.out.println("This is main method where c is "+c); // Works Fine
}
}
|
E:\workarea>javac file11.java
E:\workarea>java file11
E:\workarea>This is a Static method where a is 100
E:\workarea>This is main method where a is 100
E:\workarea>This is main method where c is 300
|
Super : a same variable may have 2 values 1 is super value other is just its value
|
class A
{
int man; // Super man
}
class B extends A
{
int man; // just man
B(int a, int b)
{
super.man = a; // Super man
man = b; // just man
}
void show()
{
System.out.println("This is show method where super.man has "+super.man+" legs "); // Super man
System.out.println("This is show method where man is "+man+" legs "); // Common man
}
}
public class file12
{
public static void main(String[] args)
{
B b_obj = new B(4, 2);
b_obj.show();
}
}
|
E:\workarea>javac file12.java
E:\workarea>java file12
E:\workarea>This is show method where super.man has 4 legs
E:\workarea>This is show method where man is 2 legs
|
INHERITANCE : one classes's properties are extend ed to another classe's properties or Physically Inter-linking classes
|
class A // Super Class of B
{
int i,j;
void show_ij()
{
System.out.println("i and j are : "+ i +" "+j);
}
int m=70; // ***
}
class B extends A // Sub Class of A
{
int k;
void show_k()
{
System.out.println("k value is : "+k );
}
void sum()
{
System.out.println("i+j+k are : "+(i+j+k));
}
void show_m()
{
System.out.println("m value is taken from A: "+m );
}
}
public class file5
{
public static void main(String[] args)
{
A A_object = new A();
B B_object = new B();
System.out.println("Contents of class A");
A_object.i=10;
A_object.j=20;
A_object.show_ij();
System.out.println("Contents of class B");
B_object.i=30;
B_object.j=40;
B_object.k=50;
B_object.show_ij();
B_object.show_k();
B_object.sum();
B_object.show_m();
}
}
*** Note in the line int m=70; had we made it private int m=70;, we wouldnt have been able to access this class B
|
E:\workarea>javac file5.java
E:\workarea>java file5
Contents of class A
i and j are : 10 20
Contents of class B
i and j are : 30 40
k value is : 50
i+j+k are : 120
m value is taken from A: 70
|
ABSTRACT : one classes's properties are extend ed to another classe's properties or Physically Inter-linking classes
|
1) Any Class that holds one or more abstract methods is called abstract class
2) Abstract methods are just place holders in super class level
3) Abstract methods dont have any body of their own
4) Abstract classes/methods cannot be called from main program unlike other classes
5) Consider this example below
class A
{
void method1()
{
System.out.println("method1 in A");
}
void method2()
{
System.out.println("method2 in A");
}
}
class B extends A
{
void method1()
{
System.out.println("method1 in B");
}
void method2()
{
System.out.println("method2 in B");
}
}
public class file6
{
public static void main(String[] args)
{
A A_object = new A();
A_object.method1();
B B_object = new B();
B_object.method1();
B_object.method2();
}
}
|
E:\workarea>javac file6.java
E:\workarea>java file6
method1 in A
method1 in B
method2 in B
|
6) Now will make method2 abstract and so class A also becomes abstract
7) note that method2 in class A has no body and even method1 of class A cant be called from main body
abstract class A
{
void method1()
{
System.out.println("method1 in A");
}
abstract void method2();
}
class B extends A
{
void method1()
{
System.out.println("method1 in B");
}
void method2()
{
System.out.println("method2 in B");
}
}
public class file6
{
public static void main(String[] args)
{
//A A_object = new A(); --------> illegal
//A_object.method1(); --------> illegal
B B_object = new B();
B_object.method1();
B_object.method2();
}
}
|
Method Overriding : Subclasses having same method names as superclass methods Override the former
|
1) If u observe method1 is common both for class B and Class A , Even though they mean and work differently
2) If same method names are repeated then its called method Overriding
class A
{
void method1()
{
System.out.println("method1 in A");
}
}
class B extends A
{
void method1()
{
System.out.println("method1 in B");
}
}
public class file7
{
public static void main(String[] args)
{
A A_object = new A();
A_object.method1();
B B_object = new B();
B_object.method1();
}
}
|
E:\workarea>javac file7.java
E:\workarea>java file7
method1 in A
method1 in B
|
Ovoiding Method Overriding : by using final
|
class A
{
final void method1()
{
System.out.println("method1 in A");
}
}
class B extends A
{
//method1 is not allowed in Class B as Class A has made it final
/*
void method1()
{
System.out.println("method1 in B");
}
*/
}
public class file7
{
public static void main(String[] args)
{
A A_object = new A();
A_object.method1();
}
}
|
using super to call super class
|
1) class A is called superclass of B and class B is called subclass of A
2) We are having a situation where class B and Class A have same method named method1 with different values
3) and we like to call class A's method1 from class B's method so we use super
class A
{
void method1()
{
int mysal = 5000;
System.out.println("Inside method 1 of Class A mysal : $ "+mysal);
}
}
class B extends A
{
void method1()
{
int mysal = 8000;
System.out.println("Inside method 1 of Class B mysal : $ "+mysal);
//Call Superclass A's method1
super.method1();
}
}
public class file8
{
public static void main(String[] args)
{
A A_object = new A();
A_object.method1();
B B_object = new B();
B_object.method1();
}
}
|
E:\workarea>javac file8.java
E:\workarea>java file8
Inside method 1 of Class A mysal : $ 5000
Inside method 1 of Class B mysal : $ 8000
Inside method 1 of Class A mysal : $ 5000
|
using Access Controls public, private, protected
|
class A
{
int int_a = 5;
private int int_pri = 10;
public int int_pub = 20;
protected int int_prot = 30;
void method1()
{
System.out.println("Inside method1 of Class A : private int_a : "+int_a);
System.out.println("Inside method1 of Class A : private int_pri : "+int_pri);
System.out.println("Inside method1 of Class A : private int_pub : "+int_pub);
System.out.println("Inside method1 of Class A : private int_prot : "+int_prot);
}
}
class B extends A
{
void method2()
{
System.out.println("Inside method1 of Class B : private int_a : "+int_a);
System.out.println("Inside method1 of Class B : private int_pri : illegal ");//+int_pri
System.out.println("Inside method1 of Class B : private int_pub : "+int_pub);
System.out.println("Inside method1 of Class B : private int_prot : "+int_prot);
}
}
public class file9
{
public static void main(String[] args)
{
A A_object = new A();
A_object.method1();
B B_object = new B();
B_object.method2();
}
}
|
E:\workarea>javac file9.java
E:\workarea>java file9
Inside method1 of Class A : private int_a : 5
Inside method1 of Class A : private int_pri : 10
Inside method1 of Class A : private int_pub : 20
Inside method1 of Class A : private int_prot : 30
Inside method1 of Class B : private int_a : 5
Inside method1 of Class B : private int_pri : illegal
Inside method1 of Class B : private int_pub : 20
Inside method1 of Class B : private int_prot : 30
|
Packages are containers for classes
|
1) Now i assume that u are working under e:\workarea
2) Create a folder dir1 under e:\workarea
3) Create a package named pack1 inside the directory e:\workarea\dir1
4) Compile this pack1.java file in e:\workarea\dir1 directory
package dir1;
class interest
{
void find_interest(int p, int t, int r)
{
int interest_val = (p*t*r)/100;
System.out.println("interest_val inside dir1 package: "+interest_val);
}
}
class pack1
{
public static void main(String args[])
{
interest interest_obj = new interest();
interest_obj.find_interest(10000, 2, 5);
}
}
5) Following steps in ur DOS prompt is important
6) Remember to Run a package u should go one level up directory
7) java < pacakagename >.< classname > is General Syntax
|
E:\workarea>cd dir1
E:\workarea\dir1>javac pack1.java
E:\workarea\dir1>cd..
E:\workarea>
E:\workarea>java dir1.pack1
interest_val inside dir1 package: 1000
|
IMPORT : This is probably the most important topic i feel worth learning
|
1) If u got this wrong now u will never get it ,
2) Right now my working directory is E:\workarea
3) under this i create a new directory named packagedir so we have E:\workarea\packagedir
4) calc_interest.java is inside packagedir directory, which does some simple calculation work using a formula I = (p*t*r)/100
5) and another file findinterest is in E:\workarea which imports this packagedir related classes
E:\workarea>cd packagedir
E:\workarea\packagedir>javac calc_interest.java
E:\workarea\packagedir>cd..
E:\workarea>
|
E:\workarea\>javac findinterest.java
E:\workarea\>java findinterest
interest : 700.0
|
package packagedir;
public class calc_interest
{
double i;
//p for principle
//i for interest
//t for time
//r for rate
public calc_interest(double p, double t, double r)
{
i=(p*t*r)/100;
}
public void show_interest()
{
if(i>0)
{
System.out.println("interest : "+i);
}
}
}
|
import packagedir.*;
class findinterest
{
public static void main(String args[])
{
calc_interest ci_obj= new calc_interest(3500,2,10);
ci_obj.show_interest();
}
}
|
6) what actually happns inside is import packagedir.* virtually loads all the classes inside packagedir into the file which is importing it like this
7) Its somewhat like single file being split into 2 files or like a copy and paste of 2 files into single file
8) pls note : the classes which can be imported must be public
package packagedir;
public class calc_interest //= = = = = = = >>
{
double i;
//p for principle
//i for interest
//t for time
//r for rate
public calc_interest(double p, double t, double r)
{
i=(p*t*r)/100;
}
public void show_interest()
{
if(i>0)
{
System.out.println("interest : "+i);
}
}
}
|
public class calc_interest
{
double i;
//p for principle
//i for interest
//t for time
//r for rate
public calc_interest(double p, double t, double r)
{
i=(p*t*r)/100;
}
public void show_interest()
{
if(i>0)
{
System.out.println("interest : "+i);
}
}
}
class findinterest
{
public static void main(String args[])
{
calc_interest ci_obj= new calc_interest(3500,2,10);
ci_obj.show_interest();
}
}
|
|
INTERFACE : An interface is a named collection of method definitions (without implementations). An interface can also declare constants
|
1) Defining an interface in its General Form
access interface interface_name
{
return-type method_name1(parameter-list);
return-type method_name2(parameter-list);
data-type variable_name1 = some-value;
data-type variable_name2 = some-value;
}
interface A
{
void myname(); //method myname() not implemented in interface A
void myage(); //method myage() not implemented in interface A
int mysal_val = 5000;
/* ILLEGAL BLOCK
public void mycountry() //method mycountry() should not be implemented in interface A
{
System.out.println("My Country is Britain ");
}
*/
}
interface B extends A
{
void mysal(); //method mysal() not implemented in interface B
}
class C implements B
{
public void myname() //method myname() is implemented in class C
{
System.out.println("My Name is James Smith ");
}
public void myage() //method myage() is implemented in class C
{
System.out.println("My Age is 26");
}
public void mysal() //method mysal() is implemented in class C
{
System.out.println("My Sal is pounds "+mysal_val);
}
}
class IFtest
{
/*
ILLEGAL BLOCK
public void myhobby()
{
System.out.println(" myhobby is collecting web sites ");
}
*/
static void myhobby()
{
System.out.println("Myhobby is collecting web sites ");
}
public static void main(String arg[])
{
C ob = new C();
ob.myname();
ob.myage();
ob.mysal();
myhobby();
}
}
2) Access must be public
3) Consider this small above code , interface B extends A means B gets all the features of interface A
4) that means B will get void myname(); , void myage(); , int mysal_val = 5000;
5) In addition to that interface B also addes its own method void mysal();
6) class C implements B means that this class C gets all the above methods and variables
7) Our main classname is IFtest this creates an object named ob linked to class C
|
E:\workarea>javac Iftest.java
E:\workarea>java Iftest
My Name is James Smith
My Age is 26
My Sal is pounds 5000
Myhobby is collecting web sites
|
My Dream to be your Friend and Create a Group of Intelligent and Understanding Programmers
|
If you like this article and/or code mailme or Join our small Java User Group which is by the Programmers for the Programmers ,
Till we meet next time BYE Kind Regards - James Smith
|
|
Java, J2EE, J2SE and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc.
in the United States and other countries.
|