File tree 3 files changed +65
-3
lines changed
3 files changed +65
-3
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,6 @@ public static void main(String[] args){
7
7
System .out .println ("b is " + b );
8
8
System .out .println ("b is " + !b );
9
9
10
- // TODO :Complete the Program
11
-
12
10
if (b ) System .out .println ("This is executed." );
13
11
b =false ;
14
12
if (b ) System .out .println ("This is not executed." );
Original file line number Diff line number Diff line change
1
+ /*5. Create a class student with instance variables roll_no and two methods to read and display the
2
+ roll_no. Then create another class Test that inherits class student, consisting of its own instance
3
+ variables to hold the marks of two subjects and also methods to read and display the marks.
4
+ Finally, create another class Result which inherits class Test. It also has its own instance variable
5
+ total to hold the total of two marks scored by the student. Similarly, it has methods to calculate
6
+ and display the total. Create some instance of above classes and demonstrate inheritance. */
7
+
8
+ /*
9
+ * author: @pray3m
10
+ */
11
+
12
+ class Student {
13
+ int rollNo ;
14
+ Student (int rollNo ){
15
+ this .rollNo =rollNo ;
16
+ }
17
+
18
+ void display (){
19
+ System .out .println ("Roll No: " +rollNo );
20
+ }
21
+ }
22
+
23
+ class Test extends Student {
24
+ int marks1 ,marks2 ;
25
+
26
+ Test (int roll ,int marks1 ,int marks2 ){
27
+ super (roll );
28
+ this .marks1 =marks1 ;
29
+ this .marks2 =marks2 ;
30
+ }
31
+
32
+ void displayMarks (){
33
+ System .out .printf ("The marks \n \t Sub 1 :%d \n \t Sub 2 : %d" ,marks1 ,marks2 );
34
+ }
35
+ }
36
+
37
+ class Result extends Test {
38
+ int total =0 ;
39
+
40
+ Result (int roll ,int marks1 , int marks2 ){
41
+ super (roll , marks1 , marks2 );
42
+ total =marks1 +marks2 ;
43
+ }
44
+
45
+ int calcTotal (){
46
+ return total ;
47
+ }
48
+
49
+ void displayResult (){
50
+ super .display ();
51
+ super .displayMarks ();
52
+ System .out .println ("\n The total marks is: " +total );
53
+ }
54
+ }
55
+
56
+
57
+ public class InheritanceDemo {
58
+ public static void main (String [] args ) {
59
+ Result s1 =new Result (1 ,56 ,76 );
60
+ s1 .displayResult ();
61
+ }
62
+ }
63
+
64
+
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ super, abstract class and method.
12
12
- [ Q.N 2] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/MultilevelInheritance.java )
13
13
- [ Q.N 3] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/HierarchicalInheritance.java )
14
14
- [ Q.N 4* ] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo.java )
15
- - [ Q.N 5* ] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo .java )
15
+ - [ Q.N 5] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/InheritanceDemo .java )
16
16
- [ Q.N 6] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/SuperKey.java )
17
17
- [ Q.N 7] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/superMethod.java )
18
18
- [ Q.N 8* ] ( https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo.java )
You can’t perform that action at this time.
0 commit comments