SlideShare a Scribd company logo
Super Keyword in Java
• The super keyword in Java is a reference
variable which is used to refer immediate
parent class object.
• Whenever you create the instance of subclass,
an instance of parent class is created implicitly
which is referred by super reference variable.
Usage of Java super Keyword
• super can be used to refer immediate parent
class instance variable.
• super can be used to invoke immediate parent
class method.
• super() can be used to invoke immediate
parent class constructor.
Use of super with variables
• This scenario occurs when a derived class and
base class has the same data members. In that
case, there is a possibility of ambiguity for
the JVM.
• class Vehicle {
• int maxSpeed = 120;
• }
• // sub class Car extending vehicle
• class Car extends Vehicle {
• int maxSpeed = 180;
• void display()
• {
• // print maxSpeed of base class (vehicle)
• System.out.println("Maximum Speed: "
• + super.maxSpeed);
}
• }
• // Driver Program
• class Test {
• public static void main(String[] args)
• {
• Car small = new Car();
• small.display();
• }
• }
• OutputMaximum Speed: 120
• class Animal{
• String color="white";
• }
• class Dog extends Animal{
• String color="black";
• void printColor(){
• System.out.println(color);//prints color of Dog class
• System.out.println(super.color);//prints color of Animal class
• }
• }
• class TestSuper1{
• public static void main(String args[]){
• Dog d=new Dog();
• d.printColor();
• }}
super can be used to invoke parent
class method
• The super keyword can also be used to invoke
parent class method. It should be used if
subclass contains the same method as parent
class. In other words, it is used if method is
overridden.
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void eat(){System.out.println("eating bread...");}
• void bark(){System.out.println("barking...");}
• void work(){
• super.eat();
• bark();
• }
• }
• class TestSuper2{
• public static void main(String args[]){
• Dog d=new Dog();
• d.work();
• }}
Use of super with constructors
• The super keyword can also be used to access the
parent class constructor
• class Person {
• Person()
• {
• System.out.println("Person class
Constructor");
• }
• }
• class Student extends Person {
• Student()
• {
• // invoke or call parent class constructor
• super();
•
• System.out.println("Student class Constructor");
• }
• }
• class Test {
• public static void main(String[] args)
• {
• Student s = new Student();
• }
• }
final Keyword in Java
Final Variables
• When a variable is declared with the final
keyword, its value can’t be modified,
essentially, a constant.
• Initializing a final Variable
• We must initialize a final variable, otherwise,
the compiler will throw a compile-time error.
A final variable can only be initialized once.
• class Bike9{
• final int speedlimit=90;//final variable
• void run(){
• speedlimit=400;
• }
• }
• Class Demo
• { public static void main(String args[]){
• Bike9 obj=new Bike9();
• obj.run();
• }
• }//end of class
• Output:Compile Time Error
final method
• If you make any method as final, you cannot override it.
• class Bike{
• final void run(){System.out.println("running");}
• }
•
• class Honda extends Bike{
• void run(){System.out.println("running safely with 100kmph");}
• class Demo{
• public static void main(String args[]){
• Honda honda= new Honda();
• honda.run();
• }
• } Output:Compile Time Error
Final classes
• When a class is declared with final keyword, it is called a
final class. A final class cannot be extended(inherited).
• final class A
• {
• // methods and fields
• }
• // The following class is illegal
• class B extends A
• {
• // COMPILE-ERROR! Can't subclass A
• }
Interfaces in Java
• The interface in Java is a mechanism to
achieve abstraction.
• There can be only abstract methods in the Java
interface, not the method body. It is used to
achieve abstraction and multiple inheritance in
Java.
• In other words, you can say that interfaces can
have abstract methods and variables. It cannot
have a method body.
• Java Interface also represents the IS-A
relationship.
• Like abstract classes, interfaces cannot be used to
create objects.
• Interface methods do not have a body - the body is
provided by the "implement" class
• On implementation of an interface, you must override
all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and
final
• An interface cannot contain a constructor (as it cannot
• be used to create objects)
How to declare an interface?
• An interface is declared by using the interface
keyword.
• Syntax:
• interface <interface_name>{
•
• // declare constant fields
• // declare methods that abstract
• // by default.
• }
• interface printable{
• void print();
• }
• class A6 implements printable{
• public void print(){System.out.println("Hello");}
•
• public static void main(String args[]){
• A6 obj = new A6();
• obj.print();
• }
• }
•
Why And When To Use Interfaces?
• To achieve security - hide certain details and only
• show the important details of an object (interface).
• Java does not support "multiple inheritance" (a
• class can only inherit from one superclass).
• However, it can be achieved with interfaces,
• because the class can implement multiple
• interfaces.
• Note: To implement multiple interfaces , separate
• them with a comma
Multiple inheritance is not supported
through class in java, but it is possible
by an interface, why?
• As we have explained in the inheritance
chapter, multiple inheritance is not supported
in the case of class
• because of ambiguity. However, it is supported
in case of an interface because there is no
ambiguity. It is because its implementation is
provided by the implementation class
• interface Printable{
• void print();
• }
• interface Showable{
• void print();
• }
•
• class TestInterface3 implements Printable, Showable{
• public void print(){System.out.println("Hello");}
• public static void main(String args[]){
• TestInterface3 obj = new TestInterface3();
• obj.print();
• }
• }
• interface Calculator {
• int add(int a,int b);
• int subtract(int a,int b);
• int multiply(int a,int b);
• int divide(int a,int b);
• }
• class Normal_Calculator implements Calculator
• {
• public int add(int a,int b){
• return a+b;}
• public int subtract(int a,int b){
• return a-b;}
• public int multiply(int a,int b){
• return a*b;}
• public int divide(int a,int b){
• return a/b;}
• public static void main(String args[]){
• Normal_Calculator c=new Normal_Calculator();
• System.out.println(“Value after addition = “+c.add(5,2));
• System.out.println(“Value after Subtraction = “+c.subtract(5,2));
• System.out.println(“Value after Multiplication = “+c.multiply(5,2));
• System.out.println(“Value after division = “+c.divide(5,2)); }}
Multiple inheritance
• interface Shape
• {
• void area(double a);
• }
• interface student
• {
• void info(int roll);
• }
• class Oper19 implements
• Shape,student
{
public void area(double a){
System.out.println("square area
="+(a*a));
}
public void info(int roll)
{
System.out.println("student roll no
="+roll);
}
• public static void main(String args[])
• {
Oper19 ob=new Oper19();
ob.area(3.2);
ob.info(12);
}
}
Packages In Java
• Package in Java is a mechanism to encapsulate
a group of classes, sub packages and
interfaces.

More Related Content

PPTX
This keyword in java
Hitesh Kumar
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
Applets in java
Wani Zahoor
 
PPT
Strings
naslin prestilda
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
PPTX
Inheritance in java
RahulAnanda1
 
PPT
Final keyword in java
Lovely Professional University
 
This keyword in java
Hitesh Kumar
 
Abstract class in java
Lovely Professional University
 
Data types in java
HarshitaAshwani
 
Applets in java
Wani Zahoor
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Inheritance in java
RahulAnanda1
 
Final keyword in java
Lovely Professional University
 

What's hot (20)

PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
Java packages
BHUVIJAYAVELU
 
PPTX
Access modifier and inheritance
Dikshyanta Dhungana
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Type casting in java
Farooq Baloch
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
History Of JAVA
ARSLANAHMED107
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Java Method, Static Block
Infoviaan Technologies
 
PPTX
Data Types, Variables, and Operators
Marwa Ali Eissa
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
JAVA AWT
shanmuga rajan
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
Constructor in java
SIVASHANKARIRAJAN
 
PPTX
Java string handling
Salman Khan
 
PPTX
constructors in java ppt
kunal kishore
 
PPS
Java Exception handling
kamal kotecha
 
Control statements in java
Madishetty Prathibha
 
Java packages
BHUVIJAYAVELU
 
Access modifier and inheritance
Dikshyanta Dhungana
 
Arrays in Java
Naz Abdalla
 
Type casting in java
Farooq Baloch
 
9. Input Output in java
Nilesh Dalvi
 
History Of JAVA
ARSLANAHMED107
 
Dynamic method dispatch
yugandhar vadlamudi
 
Method overloading
Lovely Professional University
 
Constructor in java
Pavith Gunasekara
 
Java Method, Static Block
Infoviaan Technologies
 
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
JAVA AWT
shanmuga rajan
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Constructor in java
SIVASHANKARIRAJAN
 
Java string handling
Salman Khan
 
constructors in java ppt
kunal kishore
 
Java Exception handling
kamal kotecha
 
Ad

Similar to Super Keyword in Java.pptx (20)

PPTX
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
inheritance.pptx
sonukumarjha12
 
PPT
Java Basics for selenium
apoorvams
 
PPT
Java static keyword
Lovely Professional University
 
PPT
Java static keyword
Lovely Professional University
 
PPTX
Java For beginners and CSIT and IT students
Partnered Health
 
PPTX
Introduction to java programming
shinyduela
 
PPTX
PROGRAMMING IN JAVA
SivaSankari36
 
ODP
Synapseindia reviews.odp.
Tarunsingh198
 
PPTX
Pi j3.1 inheritance
mcollison
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PPTX
Objects and classes in OO Programming concepts
researchveltech
 
PPTX
#_ varible function
abdullah al mahamud rosi
 
PPTX
OOP_with_Java_Beginner explanation .pptx
OmarMoDiaa
 
PPTX
PROGRAMMING IN JAVA
SivaSankari36
 
PPT
Java tutorials
saryu2011
 
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Method overloading
Lovely Professional University
 
inheritance.pptx
sonukumarjha12
 
Java Basics for selenium
apoorvams
 
Java static keyword
Lovely Professional University
 
Java static keyword
Lovely Professional University
 
Java For beginners and CSIT and IT students
Partnered Health
 
Introduction to java programming
shinyduela
 
PROGRAMMING IN JAVA
SivaSankari36
 
Synapseindia reviews.odp.
Tarunsingh198
 
Pi j3.1 inheritance
mcollison
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
Objects and classes in OO Programming concepts
researchveltech
 
#_ varible function
abdullah al mahamud rosi
 
OOP_with_Java_Beginner explanation .pptx
OmarMoDiaa
 
PROGRAMMING IN JAVA
SivaSankari36
 
Java tutorials
saryu2011
 
Ad

Recently uploaded (20)

PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
CDH. pptx
AneetaSharma15
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 

Super Keyword in Java.pptx

  • 2. • The super keyword in Java is a reference variable which is used to refer immediate parent class object. • Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
  • 3. Usage of Java super Keyword • super can be used to refer immediate parent class instance variable. • super can be used to invoke immediate parent class method. • super() can be used to invoke immediate parent class constructor.
  • 4. Use of super with variables • This scenario occurs when a derived class and base class has the same data members. In that case, there is a possibility of ambiguity for the JVM.
  • 5. • class Vehicle { • int maxSpeed = 120; • } • // sub class Car extending vehicle • class Car extends Vehicle { • int maxSpeed = 180; • void display() • { • // print maxSpeed of base class (vehicle) • System.out.println("Maximum Speed: " • + super.maxSpeed); } • }
  • 6. • // Driver Program • class Test { • public static void main(String[] args) • { • Car small = new Car(); • small.display(); • } • } • OutputMaximum Speed: 120
  • 7. • class Animal{ • String color="white"; • } • class Dog extends Animal{ • String color="black"; • void printColor(){ • System.out.println(color);//prints color of Dog class • System.out.println(super.color);//prints color of Animal class • } • } • class TestSuper1{ • public static void main(String args[]){ • Dog d=new Dog(); • d.printColor(); • }}
  • 8. super can be used to invoke parent class method • The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.
  • 9. • class Animal{ • void eat(){System.out.println("eating...");} • } • class Dog extends Animal{ • void eat(){System.out.println("eating bread...");} • void bark(){System.out.println("barking...");} • void work(){ • super.eat(); • bark(); • } • } • class TestSuper2{ • public static void main(String args[]){ • Dog d=new Dog(); • d.work(); • }}
  • 10. Use of super with constructors • The super keyword can also be used to access the parent class constructor • class Person { • Person() • { • System.out.println("Person class Constructor"); • } • }
  • 11. • class Student extends Person { • Student() • { • // invoke or call parent class constructor • super(); • • System.out.println("Student class Constructor"); • } • } • class Test { • public static void main(String[] args) • { • Student s = new Student(); • } • }
  • 13. Final Variables • When a variable is declared with the final keyword, its value can’t be modified, essentially, a constant. • Initializing a final Variable • We must initialize a final variable, otherwise, the compiler will throw a compile-time error. A final variable can only be initialized once.
  • 14. • class Bike9{ • final int speedlimit=90;//final variable • void run(){ • speedlimit=400; • } • } • Class Demo • { public static void main(String args[]){ • Bike9 obj=new Bike9(); • obj.run(); • } • }//end of class • Output:Compile Time Error
  • 15. final method • If you make any method as final, you cannot override it. • class Bike{ • final void run(){System.out.println("running");} • } • • class Honda extends Bike{ • void run(){System.out.println("running safely with 100kmph");} • class Demo{ • public static void main(String args[]){ • Honda honda= new Honda(); • honda.run(); • } • } Output:Compile Time Error
  • 16. Final classes • When a class is declared with final keyword, it is called a final class. A final class cannot be extended(inherited). • final class A • { • // methods and fields • } • // The following class is illegal • class B extends A • { • // COMPILE-ERROR! Can't subclass A • }
  • 17. Interfaces in Java • The interface in Java is a mechanism to achieve abstraction. • There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. • In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. • Java Interface also represents the IS-A relationship.
  • 18. • Like abstract classes, interfaces cannot be used to create objects. • Interface methods do not have a body - the body is provided by the "implement" class • On implementation of an interface, you must override all of its methods • Interface methods are by default abstract and public • Interface attributes are by default public, static and final • An interface cannot contain a constructor (as it cannot • be used to create objects)
  • 19. How to declare an interface? • An interface is declared by using the interface keyword. • Syntax: • interface <interface_name>{ • • // declare constant fields • // declare methods that abstract • // by default. • }
  • 20. • interface printable{ • void print(); • } • class A6 implements printable{ • public void print(){System.out.println("Hello");} • • public static void main(String args[]){ • A6 obj = new A6(); • obj.print(); • } • } •
  • 21. Why And When To Use Interfaces? • To achieve security - hide certain details and only • show the important details of an object (interface). • Java does not support "multiple inheritance" (a • class can only inherit from one superclass). • However, it can be achieved with interfaces, • because the class can implement multiple • interfaces. • Note: To implement multiple interfaces , separate • them with a comma
  • 22. Multiple inheritance is not supported through class in java, but it is possible by an interface, why? • As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class • because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class
  • 23. • interface Printable{ • void print(); • } • interface Showable{ • void print(); • } • • class TestInterface3 implements Printable, Showable{ • public void print(){System.out.println("Hello");} • public static void main(String args[]){ • TestInterface3 obj = new TestInterface3(); • obj.print(); • } • }
  • 24. • interface Calculator { • int add(int a,int b); • int subtract(int a,int b); • int multiply(int a,int b); • int divide(int a,int b); • }
  • 25. • class Normal_Calculator implements Calculator • { • public int add(int a,int b){ • return a+b;} • public int subtract(int a,int b){ • return a-b;} • public int multiply(int a,int b){ • return a*b;} • public int divide(int a,int b){ • return a/b;}
  • 26. • public static void main(String args[]){ • Normal_Calculator c=new Normal_Calculator(); • System.out.println(“Value after addition = “+c.add(5,2)); • System.out.println(“Value after Subtraction = “+c.subtract(5,2)); • System.out.println(“Value after Multiplication = “+c.multiply(5,2)); • System.out.println(“Value after division = “+c.divide(5,2)); }}
  • 27. Multiple inheritance • interface Shape • { • void area(double a); • } • interface student • { • void info(int roll); • }
  • 28. • class Oper19 implements • Shape,student { public void area(double a){ System.out.println("square area ="+(a*a)); } public void info(int roll) { System.out.println("student roll no ="+roll); }
  • 29. • public static void main(String args[]) • { Oper19 ob=new Oper19(); ob.area(3.2); ob.info(12); } }
  • 30. Packages In Java • Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.