write a program to object cloning?

package com.instanceofjava;

public class Employee implements Cloneable {


int a=0;
           String name="";
       Employee (int a,String name){
 this.a=a;
       this.name=name;
        }

public Employee clone() throws CloneNotSupportedException{
return (Employee ) super.clone();
}
public static void main(String[] args) {
          Employee e=new Employee (2,"Indhu");
            System.out.println(e.name);

                  try {
Employee b=e.clone();
System.out.println(b.name);
                             } catch (CloneNotSupportedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
            }
}

}


Output:
Indhu
Indhu

Write a program to Overriding in java?

Here I am taking class A:

package com.oops;

class A{
void msg(){
System.out.println("Hello");
}
}

Here I am taking class B:

class B extends A{
void msg(){
System.out.println("Welcome");
}
 
 public static void main(String args[]){
 A a=new A();
         B b=new B();
        A obj=new B();
     
        System.out.println(a.msg());   //   Hello
        System.out.println(obj.msg());  // Welcome
        System.out.println(b.msg());   //Welcome
}
}



Output:
Hello
Welcome
Welcome

Write a program to String reverse without using any string Api?

package com.instaceofjava;

public class ReverseString {

public static void main(String[] args) {
String str="Hello world";

String revstring="";

for(int i=str.length()-1;i>=0;--i){
revstring +=str.charAt(i);
}
System.out.println(revstring);

}

}


Output: dlrow olleH

Can you define an abstract class without any abstract methods? if yes what is the use of it?

  • Yes.
  • Declaring a class abstract without abstract methods means that you don't allow it to be instantiated on its own.
  • The abstract class used in java signifies that you can't create an object of the class directly.
  • This will work:
    public abstract class abs {

    protected int s=0;

    public void display() {
        System.out.print("hello");
    }

    }


what are the differences between Final , finally finalize()?

Final:

  •  Any variable declare along with final modifier then those variables treated as final variables
  • if we declare final variables along with static will became constants.
  • public final String name = "foo"; //never change this value
  • If you declare method as final that method also known as final methods.Final methods are not overridden.means we can't overridden that method in anyway.
  • public final void add(){
     }
     public class A{
     void add(){
     //Can't override
     }

     }
  • If you declare class is final that class is also known as final classes.Final classes are not extended.means we can't extens that calss in anyway.
  • public final class indhu{
     }
     public class classNotAllowed extends indhu {...} //not allowed

Finally:

  • Finally blocks are followed by try or catch.finally blocks are complasary executable blocks.But finally is useful for more than just exception handling.
  •  it allows the programmer to avoid having cleanup code accidentally bypassed by a return,
     continue, or break,Closing streams, network connection, database connection. Putting cleanup  code in a finally block is always a good practice even when no exceptions are anticipated
  •  where finally doesn't execute e.g. returning value from finally block, calling System.exit from try block etc
  • finally block always execute, except in case of JVM dies i.e. calling System.exit() .

     lock.lock();
    try {
      //do stuff
    } catch (SomeException se) {
      //handle se
    } finally {
      lock.unlock(); //always executed, even if Exception or Error or se
      //here close the database connection and any return statements like that we have to write
    }

Finalize():

  • finalize() is a method which is present in Java.lang.Object class.
  •  Before an object is garbage collected, the garbage collector calls this finalize() of object.Any unreferenced before destroying if that object having any connections with database or anything..It will remove  the connections and it will call finalize() of object.It will destroy the object.
  • If you want to Explicitly call this garbage collector you can use System.gc() or Runtime.gc() objects are there from a long time the garbage collector will destroy that objects.
  • public void finalize() {
      //free resources (e.g. un allocate memory)
      super.finalize();
    }


What is Exception? difference between Exception and Error? and types of Exceptions?

  •   Exceptions are the objects representing the logical errors that occur at run time and makes JVM enters into the state of  "ambiguity".
  • The objects which are automatically created by the JVM for representing these run time errors are known as Exceptions.
  • An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
  •  few of the subclasses of Error.
  • AnnotationFormatError - Thrown when the annotation parser attempts to read an annotation from a class file and determines that the annotation is malformed.
  • AssertionError - Thrown to indicate that an assertion has failed.
  • LinkageError - Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.
  • VirtualMachineError - Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.
  •  There are really three important subcategories of Throwable:
  • Error - Something severe enough has gone wrong the most applications should crash rather than try to handle the problem,
  • Unchecked Exception (aka RuntimeException) - Very often a programming error such as a NullPointerException or an illegal argument. Applications can sometimes handle or recover from this Throwable category -- or at least catch it at the Thread's run() method, log the complaint, and continue running.
  • Checked Exception (aka Everything else) - Applications are expected to be able to catch and meaningfully do something with the rest, such as FileNotFoundException and TimeoutException.
 instanceofjavaforus.blogspot.com

Select Menu