Friday, October 31, 2014

Inheritance in java



How to achieve multiple inheritance in Java using interfaces


interface X
{
 public void myMethod();
}
interface Y
{
public void myMethod();
}
class Demo implements X, Y
{
 public void myMethod()
{
       System.out.println(" Multiple inheritance example using interfaces");
   }
}

Try Catch in Java Exception handling

class Example1 
{
 public static void main(String args[]) 
{
     int num1, num2;
     try 
     { 
        num1 = 0;
        num2 = 62 / num1;
        System.out.println("Try block message");
     } 
      catch (ArithmeticException e) 
      {
            System.out.println("Error: Don't divide a number by zero");
       }
     System.out.println("I'm out of try-catch block in Java.");
   }
}

Java Serialization

  public class Student implements java.io.Serializable
  {
  private int stuRollNum;
  private int stuAge;
  private String stuName;
  private transient String stuAddress;
  private transient int stuHeight;
  public Student(int roll, int age, String name,
  String address, int height) 
  {
    this.stuRollNum = roll;
    this.stuAge = age;
    this.stuName = name;
    this.stuAddress = address;
    this.stuHeight = height;
  }
 public int getStuRollNum() 
 {
    return stuRollNum;
  }
  public void setStuRollNum(int stuRollNum) 
 {
    this.stuRollNum = stuRollNum;
  }
  public int getStuAge() 
  {
    return stuAge;
  }
  public void setStuAge(int stuAge) 
 {
    this.stuAge = stuAge;
  }
  public String getStuName() 
 {
    return stuName;
  }
  public void setStuName(String stuName) 
 {
    this.stuName = stuName;
  }
  public String getStuAddress() 
 {
    return stuAddress;
  }
  public void setStuAddress(String stuAddress) 
 {
    this.stuAddress = stuAddress;
  }
  public int getStuHeight() 
 {
    return stuHeight;
  }
  public void setStuHeight(int stuHeight) 
 {
    this.stuHeight = stuHeight;
  }
}
 

No comments:

Post a Comment