-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperMethodAndSuperKeyword.java
More file actions
45 lines (36 loc) · 1.07 KB
/
Copy pathSuperMethodAndSuperKeyword.java
File metadata and controls
45 lines (36 loc) · 1.07 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
/*
* i) super() method:- Super() is present inside the constructor (explicitly or implicitly by JVM)
* to call and execute the parent class constructor then the child class constructor will execute.
* Note- If we didn't write in the constructor then JVM include it implicitly(see in the below example)
*
* ii) super keyword:- super keyword is used to call the parent class instance variable.
*/
//Example-
class Parent1
{
int age=18;
Parent1()
{
System.out.println("Parent class constructor");
}
}
class Childd1 extends Parent1
{
int age=23;
Childd1()
{
//super(); //Here I didn't write the super() but JVM will include it automatically
System.out.println("Child class constructor");
}
public void disp()
{
System.out.println(age);// output- 23
System.out.println(super.age);// output- 18 (super keyword will call the parent class age(instance variable))
}
}
public class SuperMethodAndSuperKeyword {
public static void main(String[] args) {
Childd1 ch=new Childd1();
ch.disp();
}
}