SlideShare a Scribd company logo
1 of 30
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

What's hot

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)Anwar Hasan Shuvo
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 

What's hot (20)

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 

Similar to Super Keyword in Java.pptx

Similar to Super Keyword in Java.pptx (20)

Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Cse java
Cse javaCse java
Cse java
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
C#2
C#2C#2
C#2
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 

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.