//Interface Programs 1
public interface Printable {
void print ();
}
class Testing implements Printable
{
public static void main(String[] args)
{
Testing
t=new Testing();
t.print();
}
@Override
public void print() {
System.out.println("hi i am
printing");
}
}
// Multiple Ineritance in interfaces
public interface Printable {
void print ();
}
interface
Showble {
void show ();
}
class Testing implements Printable ,Showble
{
public static void main(String[] args)
{
Testing
t=new Testing();
t.print();
}
@Override
public void print() {
System.out.println("hi i am
printing");
}
@Override
public void show() {
System.out.println("showing");
}
}
// Interface Proograms
public interface T1 {
void a();//bydefault,
public and abstract
void b();
void c();
void d();
}
abstract class H implements T1
{
public void b()
{
System.out.println(" b
implementaion");
}
}
class
S extends H
{
public void a()
{
System.out.println(" a implementaion");
}
public void c()
{
System.out.println("c implementaion");
}
public
void d()
{
System.out.println("d implementaion");
}
public static void main(String[] args)
{
T1
t=new S();
t.a();
t.b();
t.c();
t.d();
}
}
// Encapsulation
public class Stock {
int sid;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public static void main(String[] args)
{
Stock
s=new Stock();
s.setSid(122);
System.out.println(s.getSid());
}
}
// cloning in java
public class Template12 implements Cloneable {
public Template12(int id, String name) {
super();
this.id = id;
this.name = name;
}
int id;
String
name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
protected Object clone() throws
CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args)
{
Template12
t=new Template12(12, "sravan");
try {
Template12
t1= (Template12)t.clone();
}
catch
(CloneNotSupportedException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
System.out.println(t.id);
System.out.println(t.name);
}
}
// Array Demos for eachLoop
public class TestArray {
public static void main(String[] args)
{
int x[]={12,78,89};
for(int x2: x)
{
System.out.println(x2);
}
}
}
// Strictfp Key word
public class Demo1 {
Demo1
get()
{
return this;
}
strictfp void m1()
{
System.out.println("in m1 method
");
}
public static void main(String[] args)
{
new Demo1().get().m1();
}
}
// Exception Demo
public class Edemo1 {
public static void main(String[] args)
{
try
{
int x=12;
int y=0;
int z =x/y;
}
catch(ArithmeticException
e)
{
System.out.println("Aritmetic
exception ");
}
}
}
// Multi
Try Blocks
public class Edemo1 {
public static void main(String[] args)
{
try
{
int x[]=new int [9];
System.out.println(x[10]);
}
catch(ArithmeticException
e)
{
System.out.println("Aritmetic
exception ");
}
catch
(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Array index
out of bound exception");
}
}
}
// Finally Block
public class Edemo1 {
public static void main(String[] args)
{
try
{
int x[]=new int [9];
System.out.println(x[10]);
}
catch(ArithmeticException
e)
{
System.out.println("Aritmetic
exception ");
}
catch
(StringIndexOutOfBoundsException ae)
{
System.out.println(" String index
out of bound exception");
}
finally
{
System.out.println(" I am finally
block ");
}
}
}
// throw Key word
public class Voting {
int age;
public Voting(int age) {
super();
this.age = age;
}
void eligible ()
{
if(age <18)
{
throw new ArithmeticException("not eligible
for vote ");
}
else
{
System.out.println("eligible
");
}
}
public static void main(String[] args)
{
Voting
v=new Voting(17);
v.eligible();
}
}
o/p:--
Exception in thread "main" java.lang.ArithmeticException: not eligible for vote
at
Voting.eligible(Voting.java:14)
at
Voting.main(Voting.java:25)
// unchecked Exception Probagation
public class Demo16 {
void m()
{
int x=12/0;
}
void n()
{
m();
}
void k()
{
n();
}
public static void main(String[] args)
{
try
{
Demo16
d=new Demo16();
d.k();
}
catch(Exception e)
{
System.out.println( "exceptin is
handled");
}
}
}
// custom exceptions
public class Voting {
int age;
public Voting(int age) {
super();
this.age = age;
}
void eligible ()
{
if(age <18)
{
try {
throw new InvalidAgeException("not eligible
for vote");
}
catch (InvalidAgeException
e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
else
{
System.out.println("eligible
");
}
}
public static void main(String[] args) {
Voting
v=new Voting(17);
v.eligible();
}
}
No comments:
Post a Comment