Skip to main content

Program to illustrate the concept of single inheritance.


CODE:

import java.io.*;
class Person 
{ BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int age;
String name;
public void getdata()throws IOException 
{ System.out.println("Enter name: ");
name=obj.readLine(); 
System.out.println("Enter age: ");
age=Integer.parseInt(obj.readLine());
} 
void putdata()
{ System.out.println("\nName: "+name);
System.out.println("Age: "+age);
} }
class employee extends Person 
{ BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));  

String desg;
double sal;
public void getdata()throws IOException 
{ super.getdata();
System.out.println("Enter designation: ");
desg=obj.readLine();
System.out.println("Enter salary: ");
sal=Double.parseDouble(obj.readLine()); }
void putdata()
{ super.putdata();
System.out.println("Designation: "+desg);
System.out.println("Salary: "+sal);
} }
public class EXP18Q5
{ public static void main(String[] args)throws IOException 
{ employee e1 = new employee();
e1.getdata(); 
e1.putdata(); 
}

Comments

Popular posts from this blog

Develop a program to accept a Password from the user and throw “Authentication Failure” exception if the password is incorrect

import java.util.*; class Test {   public static void main(String args[])   {     try     {       System.out.println("Enter Password: ");       Scanner obj=new Scanner(System.in);       String pass= obj.nextLine();       if(!pass.equals("1234"))          System.out.println("Authentication Failure");       else          System.out.println("Welcome....!!");      }      catch (Exception e)      {        System.out.println("");      }      finally      {        System.out.println("Thank You.........!!!!!");    ...

Program to calculate the room area and volume to illustrate the concept of single inheritance.

CODE: class Roomarea   { protected double l, b; Roomarea(double len, double br) { l=len; b=br; } void putdata() { System.out.println("Area of Room: "+(l*b)); } } class Roomvol extends Roomarea   { double h; Roomvol(double len, double br, double he) { super(len, br); h=he;   } void putdata() { super.putdata(); System.out.println("Volume of Room: "+(l*b*h)); } } public class EXP18Q4 { public static void main(String[] args) { Roomvol r1 = new Roomvol(10, 20, 10); r1.putdata();   } }

Define an exception called ‘NotMatchexception’ that is thrown when a string is not equal to ‘India’. Write a java program that uses this exception.

Code: import java.util.Scanner; class NotMatchException extends Exception {   public NotMatchException (String str)   {     System.out.println(str);   } } public class StringExc {    public static void main(String[] args)    {      Scanner scan = new Scanner(System.in);      System.out.print("Enter the string : ");      String input = scan.nextLine();      try      {        if(input.equalsIgnoreCase("India"))          System.out.println("String matched !!!");        else          throw new NotMatchException ("String not matched !!!");     }     catch (NotMatchException s)     {     ...