0% found this document useful (0 votes)
14 views24 pages

11 - Super Keyword

The document discusses the uses of the 'super' keyword in Java programming. The 'super' keyword can be used to refer to the immediate parent class instance variable and invoke the immediate parent class method or constructor. It provides examples demonstrating how 'super' can be used to access variables and call methods from the parent class.

Uploaded by

Disha Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views24 pages

11 - Super Keyword

The document discusses the uses of the 'super' keyword in Java programming. The 'super' keyword can be used to refer to the immediate parent class instance variable and invoke the immediate parent class method or constructor. It provides examples demonstrating how 'super' can be used to access variables and call methods from the parent class.

Uploaded by

Disha Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

. .

www.smartprogram mmg.m
www.smartprogramming.in

r" keyword is a reference variable which is used to refer


diate parent class object
www.smartprogramming.in
l
ivi+ 9.-,~ 10 j
3
Jo1nS ~~ A
i

For more updates PreH the


I•fjffi § & "B~I lc:n·
Our Channel •
www.smartprogramming.in

'.{_

,vi+ ~=-10 i

~~2 ~~ A
l
,nf Q__,=-2..6 ;,
'JoJ ihO')(,,,,f ~)
\ ( Sop{ct); (/'So
LJ
For more updates Prell the
. . - . . ~. . "Bell Icon"
•· li®s, &
Our Channel
••~

www.smartprogramming.in

'.{

~o,½S ~~ A
,vif Q__,=-L6 ;,
oJ ihO')(,,,,f ~)
f { Sop{q); (/'50
l-J .Sof'-1(, s .q); //:lo For more updates Prell the.

'-- •·lit®> &


our Channel
"Bell Icon
••~

www.smartprogramming.in

wit ~=- 10 j

~~2 ~~ A
l

For more updates Pre11 the_


......~ . "Bell Icon
••fjffi51 &
our Channel
••~

0
.._,__,,,..,._ ,
• CB 1:.1 ?:> I s- Q tl • • • !ii1 la www.smartprogramming.in

int i =

class B extends A
e<
int i =
void show(int i )
$ (
System . out.println (i ) ;
I
public static vnid main (String [] args )
( I
B ob- -new B () ;
1 ob. s how( ) ;

For more updates Press the


I• J:mw,w, & ....,
"Bell Icon"
<a- t
Our Channel

. •· ~
•Microsoft Windo.,s [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights r@Serv@d.

C:\Users\Oeepak Panwar>d:

O:\>cd javaprograms

O:\javaprograms>javac B.java

O:\javaprograms>java B

~ P o e u e o o ~ • ~ m•
.._..,.,..,,..,._ , 0

t Ci l:i ::, I .I< - ■ (,.l ~ID www.smartprogramming.in

int i =

class B extends A
e<
int i =
void show (int i )

t
(
System . out . println (this .~) ;
)
public static vnid main (String [] args )
(
B ob=new B () ;
ob . show ( ) ;
•"licrosoft Windows [Vl!rsion 10.0.10240)
(c) 2015 Microsoft Corporation. All rights resl!rvl!d.

C:\Users\Oeepak Panwar>d:

O:\>cd javaprograms

O: \javaprograms>javac B.java

O:\javaprograms>java B
30

O: \javaprograms>javac B.java

O: \javaprograms>java B
20

O:\javaprograms>

~ P o e g e o o ~ • ~ m•
1>.....- - ~ - - ..,._ __ 0
[.. ,., S<M<• ~- IJI<--, ~ SC,.,.. , .... .S- ... t,..,.,. - l
o Iii Q • • ~ .. ti :, C a 'II • • CB Ii ::, 1 .I< - G.'l t.l ~ID www.smartprogramming.in

int i =

class B extends A
(
int i "'
void show(int i )
$ (
System. out.println (super .i ) ;
I
public static vnid main (String [] args )
(
B ob=new B () ;
ob . show ( ) ;
0:\javaprograms>javac B.java

O:\javaproarams>java B
28

O:\javaprograms>javac B.java

O:\javaprograms>java B
18

O:\javaprograms>. ~
Uses of "super" keyWord1programming.in
1. "super" keyword can be used to refer immediate
parent class instance variable.

2. "super" keyword can be used to invoke immediate


parent class method.

3. super() can be used to invoke immediate parent


class constructor.
...... R..,.__ , 0

• Ill Gl ", 1 J: ■ tl www.smartprogramming.in x

int i -=

class B extends A
{
int i =
void show (int i )
{

l System. out.println (i ) ; //30


System.out.println (this . i ); //20
System . out . println (super . i ) ;

public static void main (String [J args )


{
111n

B ol),,,new B () ;
ob . show ( );
;i ..- ..... -
li't , .. - ~ -
• liil Q . . .... ,i
lo<--
..

C :a
SC,,...,.... _
C . ..
g.., e,..,..- 1
t • QlGl .., , J:mu www.smartprogramming.in x

int i •

class B extends A
{
int i =
void show (int i )
{
System. out . println (i ) ; //30
System.out . println (this . i ) ; //20
System. out . println (super . i ) ; //10
}
public static void main (String [J args )
{
B ob- -new B () ;
ob . show ( ) ;

Use 1 : "super" keyword refers to the immediate parent class


instance variable
•~•'• IMgth 111 Mll'I 1, l.ft j l C. a Set · OIO W.,._, CCALf) UTF I 145
filt , .. i.--lo<--
;J"I>..... . . . -..... . ...._ ••

• a ~ ... a .. ti :,
tlo.-- 1
SC,...,,.... w,c,. g..
e • '\a • • [ii lil ", , J: :;i m11 !-'Ill www.smartprogramming.in
0

void ml ()
{
System . out . println (" min class ") ;

class B extends A
(
void show ()
(

! super . .-p. ();

public static void main (String [J args )


{
B ob>=new B () ;
ob . show () ;

For more updates Press the


I•J-hj@N & " B~I lc;n·
Our Channel •

kov1$ilka"C•f• ~ 11' lifta 11f lft j ll C.. 116 Sd 111 W......, (CALf) tJTf... M
Microsoft Windows [Version 10.0.10240]
(c) 2015 ~licrosoft Corporation. All rights reserved.

C:\Users\Deepak Panwar>d:

D:\>cd javaprograms

O:\javaprograms>javac B.java

D:\javaprograms>java B
30

D:\javaprograms>javac B.java

O:\javaprograms>java B
30
20
10

O:\javaprograms>javac B.java

D:\javaprograms>java B

O:\javaprograms>

UO PM
ll·M.•I'
;J-i,.,......,_...... .......... . 0
£ii< (dot ~ t- la<- L - SC,,... , .... - g.., tio.- - I
• 8 11 • .. 6 .r -.: ~ :a c • 'II t • Ill Gl ... 1 J til III t l www.smartprogramming.in x
.. ,,..,o

void ml ()
{
system . out.println {" m 1n class ") ;

class B extends A
{
void ml {)
{

! }
System. out . println {"

void show ()
n l B"I) ;

{
ml O ;
super . ml () ;
}
public static void main (String [] args)
{
B ob- -new B () ;
ob . show () ;
J.w1to1a"C•f• ""9th l:16 lrftin l4 lft j ll Col t.U Sd 010 Wlftdora.1 (CALf) Uff... M
12 errors

D:\j.vaprograms>javac B.j.v.

O:\javaprogr.ms>jav. B
i min class B
i m in cl itss A

O:\javaprograms>.
;J "I>..-....--..... ......... . 0
£4< (dot I<- - la<- i . - ~ l .... W,C,. S,.. e,....,. 11:.- l
B II • • .... 1:1 :, c • 'II t • lil "• 1 l: :il ■ t l www.smartprogramming.in x

A ()
{
system . out.println (" min class A") ;

class B extends A
{
BO
(

! >I
Sys tem . out . pri ntln ("

public static void main (String [J args )


1n r1 ") ;

(
B -new B () ;
ob-

For more updates Pren the


I► Jj@§ & "B~:1 lc:n·
Our Channel •

t.w1aou,c•f• Mft9'h 21' MCI 11 lMIJ Cehl U 010 W ~ (CA Lf} Ulf... M

. ,.. ~
L• I"' S.-C• r- lo<"""'9 1 - s.i,..,. ,..., 1,!,ao 8"" tt-,;,,. - l
• Iii 1:1 • .. Q .t IQ: C :a c: • 'lit t • Ill Gl ", 1 J lil ■ i l .,,, ..__w:ww._smartprogrammingjn_
l • C--
--~---=---------------------- -
A ()
{
system . out . println (" min class ") ;

class B extends A
{
B()
{
Sys tem . out . pri ntln (" 1 aa " ) ;

public static void main (String [J args )


{
B obenew B () ;
~-r-£n<°'""ll~s.i,..,.,...,l,lo<n, ... r......-z
• Iii II • .. & .t ~ ti :, c: • 'II t • Ill lil "• 1 J m■ i'J ~ smartpro.gr_ammingJa
--~---:---------------------- -
A ()
{
Sys tem . out.println (" min class ") ;

class B extends A
{
B ()
{
super () ;

1 )
Sys tem. out . println ("

public static vo:d main (String [J args )


;n rl ") ;

{
B ob-new 8 () ;
;J "I>..,_ _ ...,, , _ __ 0
f4o (dot I<- t - lo<_,, i . - SC,.,.. , .... - g.. ti,.,. 11:. - I
• 8 II • • G .. ~ ~ :a c: • "- ~ c lil "• , J ■ll www.smartprogramming.in •
w, ..., c

A ()
{
system . out . println (" min class ") ;

class B extends A
{
B()
{
super () ;

1 )
System. out . println ("

public static void main (String [ ) args )


;n rl ti ) ;

{
B ob-new B () ;

t.wuourc•f• ""9th ~S lrfta 11t lM ll Colt,t W 17 p W ~ COLf) Uff I HS

CJ ·) ~ •

You might also like