NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Sunday, October 21, 2012

Java program to implement single inheritance


import java.io.*;
  class employee
  {
    private String empname;
    private  int empcode;

  void read(String s,int p)
  {
    empname= s;
    empcode=p;
  }
 void display()
  {
    System.out.println("The employee name is :" +empname);
    System.out.println("The employee code is :"+empcode);
  }
 }

 class labourer extends employee
 
  { private String labourtype;
    private int laboursalary;

  void read1()throws IOException
 
    {
       BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
       System.out.println("Enter the employee name:");
       String a=br.readLine();
       System.out.println("Enter the employee code :");
       int p= Integer.parseInt(br.readLine());
       read(a,p);
       System.out.println("Enter the labourtype :");
       labourtype = br.readLine();
       System.out.println("Enter the labour salary :");
       laboursalary=Integer.parseInt(br.readLine());
    }

   void display1()
 
    {
    
      display();
      System.out.println(" The labour type is : "+ labourtype);
      System.out.println(" The labour salary is :"+laboursalary);   }
 }
class single   
    { public static void main (String s[])throws IOException
      {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of employees");
        int n = Integer.parseInt(br.readLine());
        labourer l[]= new labourer[n];
        for(int i=0; i<n; i++)
        l[i]= new labourer();
        for(int i=0; i<n; i++)
        l[i] .read1();
        for(int i=0; i<n; i++)
      l[i].display1(); }
    }
Output

Sunday, August 12, 2012

Java program to read and print student information


import java.io.*;
class student
{
int slno;
String name;
student()
{
name=new String();
slno=0;
}
void get()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the student: ");
name=br.readLine();
System.out.print("\nEnter the Serial Number: ");
slno=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Serial Number : "+slno+"\nName : "+name);
}
}
class studmark extends student
{
int m1,m2;
studmark()
{
m1=m2=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the mark1: ");
m1=Integer.parseInt(br.readLine());
System.out.println("Enter the mark2: ");
m2=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Mark1 = "+m1+"\nMark2 = "+m2);
}
}
class personal extends studmark
{
int w,age;
personal()
{
w=age=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Weight : ");
w=Integer.parseInt(br.readLine());
System.out.println("Enter the age : ");
age=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Weight = "+w+"\nAge = "+age);
}
}
class studinheritance
{
public static void main(String[] args)throws IOException
{
student x;
student y= new student();
x=y;
x.get();
x.disp();
studmark i;
x=i;
x.get();
x.disp();
personal p;
x=p;
x.get();
x.disp();
}
}

Java program to Implement Runnable class


class childthread implements Runnable
{
Thread t;
childthread()
{
t=new Thread(this,"odd");
System.out.println("child created "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<10;i++)
 {
 System.out.println("child "+i);
 i++;
 Thread.sleep(500);
 }
}
catch(InterruptedException e)
{}
}
}
class oddeventhread
{
public static void main(String[] s)
{
 childthread c= new childthread();
try
{
 for(int i=2;i<=10;i++)
  {
   System.out.println("even "+i);
   i++;
   Thread.sleep(1000);
  }
}
catch(InterruptedException e)
{}
}
}

Java Program to implement multilevel inheritance


import java.io.*;
class xyz
{
int x,y;
void get() throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
System.out.print("Enter a num : ");
x=Integer.parseInt(br.readLine());
System.out.print("Enter a num : ");
y=Integer.parseInt(br.readLine());
}
}
class abc extends xyz
{
int a;
void calc()
{
a=x+y;
System.out.println("The sum of "+x+" & "+y+" is "+ a);
}
}
class mno extends abc
{
int s;
void disp()
{
s=x-y;
System.out.println("Difference : "+ s);
}
}
class inheritance
{
public static void main(String[] args) throws IOException
{
mno d= new mno();
d.get();
d.calc();
d.disp();
}
}