6
SUPER KEYWORD

Super Keyword

Embed Size (px)

DESCRIPTION

thi ppt contains a theory about super keyword and also programs ie syntax

Citation preview

SUPER KEYWORD

SUPER KEYWORD

What is Super Keyword???Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keywordsuper.You can also usesuper to refer to a hidden field (although hiding fields is discouraged).It has advantage so that you dont to have to perform operations in the parentclass again.Only the immediate parentclasss data and methods can be accessed

Super Keyword UsesSuper keyword can be used for 3 purposeTo call the super class constructorTo call the super class methodTo call the super class variable

Super class Constructor callingSuper keyword can be used to call the super class constructor and use it in the sub class by writingsuper()in constructor.

Syntaxclass a{constructor()}class b{constructor(){super();}}

Super class Method callingSuper keyword can be used to call the super class method that has been overridden by the sub class method.it can be done by super.methodname() .Syntaxclass a{void method();}class b{constructor(){void method();{super.method(); //will use the super class method}}}

Super class Variable callingSuper keyword can be used to call or use the super class variable that has been overwritten in sub class.It can be done by super.variablenameSyntaxclass a{variablename;}class b{variablename;super.variablename;//will use the superclass variable}