Single Level Inheritance
This is simple java inheritance example . Where we take command line inputs
of students number , name , subjects then calculate the average . Class Results
extends the students and calculate average.
import java.io.*;
import java.lang.*;
class stud {
String sname;
int htno,m1,m2,m3;
void read()throws IOException {
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the hno");
htno=Integer.parseInt(dis.readLine());
System.out.println("Enter the sname");
sname=dis.readLine();
System.out.println("Enter the 3 sub marks");
m1=Integer.parseInt(dis.readLine());
m2=Integer.parseInt(dis.readLine());
m3=Integer.parseInt(dis.readLine());
}
void put() {
System.out.println(htno +" " +sname +" " +m1 +" " +m2 +" " +m3);
}
}
class Result extends stud {
int tot;
float avg;
void compute()throws IOException {
read();
put(); tot=m1+m2+m3;
avg=tot/3;
System.out.println("Total : " +tot);
System.out.println("Average : " +avg);
}
}
class ComputeResult {
public static void main(String args[])throws IOException {
Result obj=new Result();
obj.compute();
}
}
Programm output when you run this code . Like this enter your number then name then three
subjects
Enter the hno
112
Enter the sname
mukesh
Enter the 3 sub marks
45
56
67
112 mukesh 45 56 67
Total : 168
Average : 56.0
Single Level Inheritance
Reviewed by Mukesh Jha
on
10:12 AM
Rating:
No comments:
Add your comment