Advertisement

Tuesday 18 October 2016

Exception In java Id name and price


 Id is Not Nagative  And Name Is Only A to Z And Price is 0 to 9   Value Allow Exception



import java.io.*;
import java.util.*;
class NonNegId extends Exception
{
          String msg;
          NonNegId(String m)
          {
                   this.msg = m;
          
          public String toString()
          {
                   return this.msg;
          }
}
class Bname extends Exception
{
          String bnm;
          Bname(String nm)
          {
                   this.bnm = nm;
          
          public String toString()
          {
                   return this.bnm;
          }
         
         
         
}
class PrOnly extends Exception
{
          String str;
          PrOnly(String s)
          {
                   this.str = s;
          
          public String toString()
          {
                   return this.str;
          }
}


class Book
{
          int bid;
          String bname;
          double bprice;
          Book(int no,String nm,double pr) throws NonNegId,PrOnly,Bname
          {
                  

                   if(no<0)
                   {
                             throw new NonNegId("Book Id Can not be Nagative.");
                   }
                   for(int i=0;i< nm.length();i++)
                   {
                             if(nm.charAt(i)>='0' && nm.charAt(i)<='9')
                             {
                                                          throw new Bname("Not a perfect String");
                             }
                   }
                  
                  
                  
                   if(pr < 0){

            
            throw new PrOnly("Only supported for Price 0 to 10");

        }else{
                            
                             this.bid=no;
                             this.bname=nm;
                             this.bprice=pr;
                  
        }

          }
         
         
          public static void main(String[] args) throws NonNegId,PrOnly
          {
                  
                     try{

                   Book b= new Book(1,"ja0va",120.5);
            
        }catch(NonNegId eid){

            System.out.println(eid);         
            

        }catch(PrOnly eprice){

            System.out.println(eprice);         

        }catch(Bname enm){

            System.out.println(enm);         

        }finally{

            System.out.println("Releasing resources");         

        }

    }
                  
}



Thursday 13 October 2016

method overriding in java ?


What do you mean by method  overriding? Why it is needed? Explain the uses of this and super keyword with example.




The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a sub class can implement a parent class method based on its requirement.

In object-oriented terms, overriding means to override the functionality of an existing method

Method Overriding:

Method overriding means have same signature but with different implementation.

If subclass (child class) has same method as declared in the parent class, it is known as method overriding in java.

In other words, if sub class provides the specific implementation of a method that is already provided by its super class. it is known as

method overriding.

Super class reference can be used to refer to a subclass object. The dynamic method dispatch mechanism in java selects the appropriate version of an overridden method to execute based on the class of the executing object, not the type of a variable that references that object. 

Usage of java Method Overriding:

Method overriding is used to provide specific implementation of the method that is already provided by its super class. 

Method overriding is used for runtime polymorphism.

Why it is Needed?

Method overriding in object oriented programming is a language feature that all allows a subclass or child class to specific implementation of a method that is already provided by one of its super classes or parent classes.

The implementation in the subclass override(replaces)the implementation in the super class by providing a method that has same name, same parameters or signature and same return type as the method in the parent class.

Super keyword:

super keyword in java is a reference variable that is used to refer parent class object super class an implicit keyword create by JVM and supply each and every java program for performing impotent in three places:

---> At variable level
---> At method level
---> At constructor level

Need of super keyword:

Whenever the derived class is inherits the base class features there is a possibility that base class features are similar to derived class features and JVM gets an ambiguity.

In order to different between base class features and derived class feature must be preceded by super keyword.

Usage of java super Keyword:

1. super is used to refer immediate parent class instance variable.
2. super() is used to invoke immediate parent class constructor.
3. super is used to invoke immediate parent class method.


Real example of Java Method Overriding:

Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate of interest.




Example:

class A
{
int i;
A()
{
}
A(int i)
{
this.i=i;
}
void print()
{
System.out.println(this.i);
}
}
class B extends A
{
int k;
B(int k)
{
super(k);
this.k=k;
}
B()
{
super.i=5;
this.k=5;
}
void print()
{
super.print();
System.out.println(this.k);
}
public static void main(String s[])
{
B b1=new B(50);
b1.print();
}
}


OutPut:
50
50

Wednesday 12 October 2016

Explain the use of finalize method with Example


 Explain the use of 

 finalize method with Example 




Finalize() Method:

  • When you will need to do some actions when an object is destroyed by garbage collection.
  • For example when your program is using some non-java resources, you have to free the memory for these resources.
  • This process is known as finalization.
  • The finalization is done by the finalize() method.


Syntax :

protected void finalize()
{
//statements to be executed
}

  • Finalize() method does not return any value so the return type is void.
  • The finalize() mehod is called just before when java runtime system perform garbage collection.
  • Finalize method is object class method, so applied all the class.
Example :

class Demo
{
static int cnt=0;
protected void finalize()
{
cnt++;
System.out.println
                ("Finalize"+ cnt);
}
public static void main(String[] a)
{
int i;
Demo obj[] = 
                new Demo[10000000];
for (i=0;i<10000000;i++)
{
obj[i]s=new Demo();
}
for (i=0;i<10000000;i++)
{
obj = null;
}
System.gc();
}
}



Tuesday 11 October 2016

Java Pass By Value and Pass By Reference


 Java Pass By Value and Pass By  Reference 




Pass by Value


In java we have a pass by value, when we use primitive type as parameters then it is a pass by value. Thus, what occurs to the parameter that receives the argument has no affect outside the method.
This method is copy of the passed-in variable is copied into the argument. Any changes to the argument do not affect the original one.
Actual parameter expressions that are passed to a method are evaluated and a value is derived. Then this value is stored in a location and then it becomes the formal parameter to the invoked method. This mechanism is called pass by value and Java uses it.





Pass by Reference


In java we use the reference type as parameters in a method then it become a pass by value for the reference value. The reference type refer to an object, so when we use the reference data type as a parameter, a copy of the reference value is made available to the invoked method, which enables the invoked method to work on the same object as the one reference by the invoking method.

In pass by reference, the formal parameter is just an alias to the actual parameter. It refers to the actual argument. Any changes done to the formal argument will reflect in actual argument .


We have a method defined as follows:

 public void method(Account ac,int i)
 {
i+=100;
ac.deposit(10000);
i+=100;

ac = new CurrentAccount
  (1005, “tom”,4000);

i+=100;
ac.deposit(3000);
 }


This method is beging used from another code as follows:


1 Account ac = 
   new CurrentAccount(1005, “tom”, 2000);

2 int a = 100;

3 ac.deposit(3000);

4 method(ac,a);

5 System.out.println
   (ac.getBalance()+”, “+a);

It prints “15000, 100”


Since primitive types are pass by value, the value of a is unchanged. In line 1, balance in Account(ac) is 2000, in line 3 balance in ac 5000. Now the reference to ac is copied to the parameter ac in method, so ac in method refers to the same object ac which is declared in line 1. In line 7, the balance would be 15000, now line 9 makes ac in the method ac in method refer to a new object, so the object refered by ac in line 1 is unaffected. line 9 would make the method loose the reference to the object which was passed as a parameter in line 4.
Example:

class A
{
void print(int i) 
        //pass by value
{
System.out.println(i);
}
public String toString()
{
return "Class A";
}
void toPrint(A obj) 
        //pass by reference
{
System.out.println(obj);
}
}
class B extends A
{
public static void main(String[] str)
{
B obj = new B();
obj.print(5); 
                //pass by value
obj.toPrint(new B()); 
                //pass by reference
}
}






Searches related to java Pass By Value and Pass By Reference

Labels

ADVERTISEMENT