Skip to content

Commit 79052e4

Browse files
authored
Merge pull request #511 from KeerthanaPravallika/master
Added Single Inheritance in java
2 parents 8512d8c + ea77905 commit 79052e4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)