File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Inheritance : Inheritance in Java is a mechanism in which one object
4
+ acquires all the properties and behaviors of a parent object.
5
+
6
+ Single Inheritance : When a class inherits another class, it is known as a single inheritance.
7
+
8
+ class A
9
+ ^
10
+ |
11
+ |
12
+ class B
13
+
14
+
15
+ */
16
+
17
+ class base_class
18
+ {
19
+ int base_value ;
20
+ base_class (int num ) // constructor
21
+ {
22
+ base_value = num ;
23
+ }
24
+ }
25
+
26
+ class derived_class extends base_class // single inheritance
27
+ {
28
+ int derived_value ;
29
+ derived_class (int der_val ,int base_val ) // constructor
30
+ {
31
+ super (base_val ); //calling base class constructor
32
+ derived_value = der_val ;
33
+ }
34
+ void display ()
35
+ {
36
+ System .out .println ("Base class variable value : " + base_value ); // accessing base class variable
37
+ System .out .println ("Derived class variable value : " +derived_value );
38
+ }
39
+ }
40
+ public class Main
41
+ {
42
+ public static void main (String [] args ) {
43
+
44
+ derived_class obj = new derived_class (10 ,20 ); // creating derived class object
45
+ obj .display ();
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments