Instanceof Operator
Instanceof Operator
*******************************************************************
class Parent1 {}
class InstanceOp extends Parent1 {
}
}
*******************************************************************
Upcasting
*******************************************************************
class Employee {
String name="Amit";
void display(){System.out.println(name);};
}
*******************************************************************
Downcasting
*******************************************************************
class Animal
{
void eat(){System.out.println("eating...");}
}
*******************************************************************
Upcasting, Downcasting and Instanceof
*******************************************************************
class Animal1
{
void eat(){System.out.println("eating...");}
}
class AnimalSound
{
void identifySound(Animal1 a)//upcasting
{
if(a instanceof Dog1)
{
Dog1 d=(Dog1)a;//Downcasting
d.bark();
}
else if(a instanceof Cat1)
{
Cat1 c=(Cat1)a;//Downcasting
c.meow();
}
else
{
a.eat();
}
}
}
public class upDown {
public static void main(String args[])
{
Animal1 a1=new Dog1();
a1=new Animal1();
s.identifySound(a1);
*******************************************************************
Conversion
*******************************************************************
public class conversion {
public static void main(String args[]){
int i=10;
long m=10000L;
double d=3.141;
String s="200";
i=Integer.parseInt(s);
System.out.println(i+100);
s=String.valueOf(i);
//s= Integer.toString(i);
System.out.println(s+100);
}
}
*******************************************************************
Static polymorphism- Overloading
*******************************************************************
*******************************************************************
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}
*******************************************************************
a1=new Dog();
a1.eat();
a1=new BabyDog();
a1.eat();
}
}
*******************************************************************
Overriding- Hierarchical inheritance
*******************************************************************
class Bank{
float getRateOfInterest(){return 0;}
}
class SBI extends Bank{
float getRateOfInterest(){return 8.4f;}
}
class ICICI extends Bank{
float getRateOfInterest(){return 7.3f;}
}
class AXIS extends Bank{
float getRateOfInterest(){return 9.7f;}
}
class TestPolymorphism{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
b=new AXIS();
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
}
}
*******************************************************************
Overriding – Super keyword
*******************************************************************
class Animal2 {
public void move() {
System.out.println("Animals can move");
}
}
class TestDog {
*******************************************************************
*******************************************************************
Multiple Inheritance in Interface
*******************************************************************
interface Printable{
void print();
}
interface Showable{
void print();
}
*******************************************************************
*******************************************************************
interface Printable1{
void print();
}
*******************************************************************
Default method in Interface
*******************************************************************
interface Drawable2{
void draw();
default void calArea(){System.out.println("calculating area");}
}
class Rectangle2 implements Drawable2{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfacedefault{
public static void main(String args[]){
Drawable2 d=new Rectangle2();
d.draw();
d.calArea();
}}
*******************************************************************
interface Drawable4{
void draw();
default void msg(){System.out.println("default method4");}
}
}
class TestInterfacedefault1{
public static void main(String args[]){
Drawable3 d=new Rectangle4();
d.draw();
d.msg();
}}
*******************************************************************
Static method in Interface
*******************************************************************
interface Drawable5{
void draw();
static int cube(int x){return x*x*x;}
}
class InterfaceStatic{
public static void main(String args[]){
Drawable5 d=new Rectangle5();
d.draw();
System.out.println(Drawable5.cube(3));
}}
*******************************************************************
Checked Exception- throws keyword
*******************************************************************
import java.io.*;
*******************************************************************
*******************************************************************
Creating Risky Method
*******************************************************************
import java.io.IOException;
import java.sql.SQLException;
*******************************************************************
Unchecked Exception
*******************************************************************
class Testtrycatch1{
public static void main(String args[]){
int i=50;
int j=0;
int data=i/j;
*******************************************************************
Handling Unchecked Exception
*******************************************************************
public class Testtrycatch2{
public static void main(String args[]){
try{
int i=80;
int j=0;
int data=i/j;
}catch(ArithmeticException e){System.out.println(e);}
*******************************************************************
*******************************************************************
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
*******************************************************************
Finally Block- with exception
*******************************************************************
public class TestFinallyBlock1{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
*******************************************************************
Custom Exception
*******************************************************************
class InvalidAgeException extends Exception{
private String msg;
InvalidAgeException(String s){
this.msg = s;
}
public String getMsg() {
return msg;
}
}
class TestCustomException1{
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("Age is not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
try{
validate(13);
}catch(InvalidAgeException m){System.out.println("Exception occured: "+m.getMsg());}
System.out.println("rest of the code...");
}
}
*******************************************************************
JDBC Driver- extracting data from the database
*******************************************************************
import java.sql.*;
class jdbcEx{
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt=con.createStatement();
while(rs.next())
System.out.println("ID="+ rs.getInt(1) + " --- name=" + rs.getString("name"));
con.close();
}
}
*******************************************************************
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO Emp VALUES(3, 'Bob')");
con.close();
}
}
*******************************************************************
JDBC- updating record
*******************************************************************
import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt=con.createStatement();
con.close();
}
}
*******************************************************************
*******************************************************************
import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt=con.createStatement();
stmt.executeUpdate("DELETE from Emp WHERE id=2");
con.close();
}
}
*******************************************************************
PreparedStatement
*******************************************************************
import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver");
int i=stmt.executeUpdate();
System.out.println(i+" records updated");
}
}
*******************************************************************