-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice10.java
More file actions
66 lines (55 loc) · 1.45 KB
/
Practice10.java
File metadata and controls
66 lines (55 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
super(): It is always used in the child class and is used to call the parent class(Parent constructor) member function.
And it is always written in the constructor only.
- super keyword: It is used to access the parent class variable.
- this keyword: It is used to access the current class variable.
*/
class One
{
int x, y;
void getParent()
{
System.out.println("Value of x in Parent class is " + x);
System.out.println("Value of y in Parent class is " + y);
}
// Parent Constructor is called by the super() method.
One(int a, int b)
{
System.out.println("Super() method is used for the addition " + (a+b));
}
}
class Two extends One
{
int x, y;
void setParent(int x, int y)
{
super.x = x; // it point to the parent variable
super.y = y;
}
void setChild(int x, int y)
{
this.x = x; // it point to the child or currect variable
this.y = y;
}
void getChild()
{
System.out.println("Value of x in child class is " + x);
System.out.println("Value of y in child class is " + y);
}
// It is child constructor which is used to calling the parent constructor using super() method.
Two()
{
super(4,4);
}
}
public class Practice10
{
public static void main (String[] args)
{
Two t = new Two();
t.setParent(3,2);
t.setChild(4,5);
t.getParent();
t.getChild();
}
}