Wednesday, 8 June 2016

Compiling Java Programs online


      http://www.javatpoint.com/java-oops-concepts
1)  http://www.tutorialspoint.com/compile_java_online.php

   http://goo.gl/EGmh0Z
  http://goo.gl/xD6PuL

2)  ls -> for listing
     dir

 3) save file with name  sree.java

4)  javac  sree.java


5) ls
   >>>   sree.java     sree.class


6) java sree


-------------------------------------------------------------


Example of an instance variable:

class Taxes
{
  int count;
  /*...*/
}


Class variables – also known as static member variables

Example of a class variable:

class Taxes
{
  static int count;
  /*...*/
}




----------------------------------------------------------------------------

lass variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.

Advantage of naming conventions in java

By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
NameConvention
class nameshould start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface nameshould start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method nameshould start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable nameshould start with lowercase letter e.g. firstName, orderNumber etc.
package nameshould be in lowercase letter e.g. java, lang, sql, util etc.
constants nameshould be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

Class in Java

A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.
A class in java can contain:
  • data member
  • method
  • constructor
  • block
  • class and interface

Syntax to declare a class:

  1. class <class_name>{  
  2.     data member;  
  3.     method;  
  4. }  



==================================================================
class Student
{
   String name;


public static void main(String[] args)
{
    Student myname=new Student();
    Student hername=new Student();
    myname.name="sree";
    hername.name="sahi";
    System.out.println(myname.name);
    System.out.println(hername.name);
    
    
}
    
}

==================================================================


class Info{
 
    int roll;
    String name;
 
     void insert(int r, String n)
     {
       
      roll=r;
      name=n;
     }
     void display()
     {
         System.out.println("my name is "+name +"and my roll no is "+roll);
       
     }
   
     public static void main(String[] args)
     {
       
         Info student1=new Info();
         Info student2=new Info();
       
         student1.insert(99,"Sree");
         student2.insert(2,"Sahi");
       
         student1.display();
         student2.display();
     
       
     }
 
 
}


==================================================================

What are the different ways to create an object in Java?

There are many ways to create an object in java. They are:
  • By new keyword
  • By newInstance() method
  • By clone() method
  • By factory method etc.
========================================================

Annonymous object

Annonymous simply means nameless.An object that have no reference is known as annonymous object.
If you have to use an object only once, annonymous object is a good approach.
  1. class Calculation{  
  2.   
  3.  void fact(int  n){  
  4.   int fact=1;  
  5.   for(int i=1;i<=n;i++){  
  6.    fact=fact*i;  
  7.   }  
  8.  System.out.println("factorial is "+fact);  
  9. }  
  10.   
  11. public static void main(String args[]){  
  12.  new Calculation().fact(5);//calling method with annonymous object  
  13. }  
  14. }  

=========================================

Creating multiple objects by one type only

We can create multiple objects by one type only as we do in case of primitives.
  1. Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects  

==========================================================


Method Overloading in Java

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.


Different ways to overload the method

There are two ways to overload the method in java
  1. By changing number of arguments
  2. By changing the data type


1)Example of Method Overloading by changing the no. of arguments

In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.
  1. class Calculation{  
  2.   void sum(int a,int b){System.out.println(a+b);}  
  3.   void sum(int a,int b,int c){System.out.println(a+b+c);}  
  4.   
  5.   public static void main(String args[]){  
  6.   Calculation obj=new Calculation();  
  7.   obj.sum(10,10,10);  
  8.   obj.sum(20,20);  
  9.   
  10.   }  
  11. }  

2)Example of Method Overloading by changing data type of argument

In this example, we have created two overloaded methods that differs in data type. The first sum method receives two integer arguments and second sum method receives two double arguments.
  1. class Calculation2{  
  2.   void sum(int a,int b){System.out.println(a+b);}  
  3.   void sum(double a,double b){System.out.println(a+b);}  
  4.   
  5.   public static void main(String args[]){  
  6.   Calculation2 obj=new Calculation2();  
  7.   obj.sum(10.5,10.5);  
  8.   obj.sum(20,20);  
  9.   
  10.   }  
  11. }  

----------------------------------------------------------------------------------------------------------
=========================================================

Constructor in Java



Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation


Types of java constructors

There are two types of constructors:
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
java constructor


Example of default constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.

  1. class Bike1{  
  2. Bike1(){System.out.println("Bike is created");}  
  3. public static void main(String args[]){  
  4. Bike1 b=new Bike1();  
  5. }  
  6. }  


Q) What is the purpose of default constructor?

Default constructor provides the default values to the object like 0, null etc. depending on the type.

Example of default constructor that displays the default values


  1. class Student3{  
  2. int id;  
  3. String name;  
  4.   
  5. void display(){System.out.println(id+" "+name);}  
  6.   
  7. public static void main(String args[]){  
  8. Student3 s1=new Student3();  
  9. Student3 s2=new Student3();  
  10. s1.display();  
  11. s2.display();  
  12. }  
  13. }  

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
  1. class Student4{  
  2.     int id;  
  3.     String name;  
  4.       
  5.     Student4(int i,String n){  
  6.     id = i;  
  7.     name = n;  
  8.     }  
  9.     void display(){System.out.println(id+" "+name);}  
  10.    
  11.     public static void main(String args[]){  
  12.     Student4 s1 = new Student4(111,"Karan");  
  13.     Student4 s2 = new Student4(222,"Aryan");  
  14.     s1.display();  
  15.     s2.display();  
  16.    }  
  17. }  

Constructor Overloading in Java

Example of Constructor Overloading

  1. class Student5{  
  2.     int id;  
  3.     String name;  
  4.     int age;  
  5.     Student5(int i,String n){  
  6.     id = i;  
  7.     name = n;  
  8.     }  
  9.     Student5(int i,String n,int a){  
  10.     id = i;  
  11.     name = n;  
  12.     age=a;  
  13.     }  
  14.     void display(){System.out.println(id+" "+name+" "+age);}  
  15.    
  16.     public static void main(String args[]){  
  17.     Student5 s1 = new Student5(111,"Karan");  
  18.     Student5 s2 = new Student5(222,"Aryan",25);  
  19.     s1.display();  
  20.     s2.display();  
  21.    }  
  22. }

==================================================================
class StudentDetails{
 
    int roll;
    String name;
    int marks;
 
    StudentDetails(int r,String n)
    {
      roll=r;
      name=n;
    }
    StudentDetails(int r,String n,int m)
    {
      roll=r;
      name=n;
      marks=m;
   
    }
 
    void display()
 
    {
        System.out.println("my name is : " +name +"and my roll no is :"+ roll + "my makrs"+ marks );
     
    }
 
 
    public static void main(String [] args)
    {
        StudentDetails s1=new StudentDetails(9,"sree");
     
        StudentDetails s2=new StudentDetails(2,"sahi",499);    
     
        s1.display();
        s2.display();
     
    }
 
}


=================================================================

Java static keyword

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static can be:
  1. variable (also known as class variable)
  2. method (also known as class method)
  3. block
  4. nested class

Example of static variable

  1. //Program of static variable  
  2.   
  3. class Student8{  
  4.    int rollno;  
  5.    String name;  
  6.    static String college ="ITS";  
  7.      
  8.    Student8(int r,String n){  
  9.    rollno = r;  
  10.    name = n;  
  11.    }  
  12.  void display (){System.out.println(rollno+" "+name+" "+college);}  
  13.   
  14.  public static void main(String args[]){  
  15.  Student8 s1 = new Student8(111,"Karan");  
  16.  Student8 s2 = new Student8(222,"Aryan");  
  17.    
  18.  s1.display();  
  19.  s2.display();  
  20.  }  
  21. }  

Program of counter by static variable

As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
  1. class Counter2{  
  2. static int count=0;//will get memory only once and retain its value  
  3.   
  4. Counter2(){  
  5. count++;  
  6. System.out.println(count);  
  7. }  
  8.   
  9. public static void main(String args[]){  
  10.   
  11. Counter2 c1=new Counter2();  
  12. Counter2 c2=new Counter2();  
  13. Counter2 c3=new Counter2();  
  14.   
  15.  }  
  16. }  

Java static method

If you apply static keyword with any method, it is known as static method.
  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.
class Student
{
    int roll;
    String name;
    static String clg="abc";
 

  Student(int r,String n)
  {
      roll=r;
      name=n;
   
  }


static void change()
{
    clg="xyz";
}
 
    void display()
    {
        System.out.println("my name is "+name + " my roll is : "+ roll + " my clg "+clg);
     
     
    }
 

public static void main(String args[])
{
    Student A=new Student(1,"sri");
    A.display();
    Student.change();
    Student B=new Student(2,"sahi");
    B.display();

}


}



-----------------------------------------------------------------------------------------------------------
================================================================

this keyword in java


 The this keyword can be passed as argument in the constructor call.

We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example:
  1. class B{  
  2.   A4 obj;  
  3.   B(A4 obj){  
  4.     this.obj=obj;  
  5.   }  
  6.   void display(){  
  7.     System.out.println(obj.data);//using data member of A4 class  
  8.   }  
  9. }  
  10.   
  11. class A4{  
  12.   int data=10;  
  13.   A4(){  
  14.    B b=new B(this);  
  15.    b.display();  
  16.   }  
  17.   public static void main(String args[]){  
  18.    A4 a=new A4();  
  19.   }  
  20. }  
=====================================================================

class B{
   A obj;
   B(A obj)
   {
       this.obj=obj;
     
   }
 
    void display()
    {
        System.out.println(obj.data);
        {
         
        }
     
    }
 
}


class A {

   int data=9;
 
   A()
   {
    B b=new B(this);
    b.display();
   }
 
    public static void main(String args[])
 
    {
        A a=new A();
 
    }
 
}

====================================================================

class Address {

  String city,state,country;
 
   Address(String city, String state,String country)
   {
  this.city=city;
  this.state=state;
  this.country=country;
 
 
   }
}

class Emp{

int id;
String name;
Address addr;

Emp(int id,String name,Address addr)
{
this.id=id;
this.name=name;
this.addr=addr;

}

void display()
{
System.out.println(id + " "+name);
System.out.println(addr.city +" "+addr.state + " "+ addr.country );

}

public static void main(String args[])
{
Address a1=new Address("hyd","ts","IN");
Address a2=new Address("ohi","akr","US");

Emp e1=new Emp(99,"sree",a1);
Emp e2=new Emp(02,"sahi",a2);

e1.display();
e2.display();


}



}
=====================================================================

Inheritance in Java


The idea behind inheritance in java is that you can create new classes that are built upon existing classes.

When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.


Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.
=====================================================================



1 comment: