SlideShare uma empresa Scribd logo
1 de 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.

Mais conteúdo relacionado

Mais procurados

Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.netilakkiya
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Packages in java
Packages in javaPackages in java
Packages in javajamunaashok
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Java modifiers
Java modifiersJava modifiers
Java modifiersSoba Arjun
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxRkGupta83
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptxParvizMirzayev2
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 

Mais procurados (20)

Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java arrays
Java arraysJava arrays
Java arrays
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java modifiers
Java modifiersJava modifiers
Java modifiers
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptx
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 

Semelhante a Super Keyword in Java.pptx

Semelhante a 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
 

Último

Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Último (20)

Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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.