SlideShare a Scribd company logo
1 of 26
Static keyword
 The static variable can be used to refer the
common property of all objects.
 The static variable gets memory only once in
class area at the time of class loading.
//Program for static keyword
class Exstatic
{
static void evenodd(int num)
{
if(num%2==0)
System.out.println("The given number is even");
else
System.out.println("The given number is odd");
}
class Example
{
public static void main(String args[])
{
Exstatic.evenodd(14);
}
}
Inheritance
Inheritance in java is a mechanism in which
one object acquires all the properties and
behaviors of parent object.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Types of Inheritance
//Single Inheritance
class Single
{
static int num1=10;
static int num2=5;
}
class Inherit extends Single
{
public static void main(String[] args)
{
int num3=2;
int result=num1+num2+num3;
System.out.println("Result of child class is "+result);
}
}
//Multilevel Inheritance
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
} }
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
} }
Method Overriding
If subclass (child class) has the same method as
declared in the parent class, it is known as
Method Overriding.
//Example of Method Overriding
class Vehicle
{
void run()
{System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
Dynamic Method Dispatch
Dynamic method dispatch is a mechanism by
which a call to an overridden method is
resolved at runtime.
This is how java implements runtime
polymorphism.
Example for Dynamic Method Dispatch
class Game
{
public void type()
{ System.out.println("Indoor & outdoor"); }
}
Class Cricket extends Game
{
public void type()
{ System.out.println("outdoor game"); }
public static void main(String[] args)
{
Game gm = new Game();
Cricket ck = new Cricket();
gm.type();
ck.type();
gm=ck; //gm refers to Cricket object
gm.type(); //calls Cricket's version of type
} }
abstract keyword
Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
 Syntax:
abstract class classname
{
………
abstract void method();
………}
abstract class A
{
abstract void display();
public void method1()
{
System.out.println(“This is concrete method");
}
}
class B extends A
{
void display()
{
System.out.println(“This is abstract");
}
public static void main(String[] args)
{
B b = new B();
b.display();
b.method1();
}
}
Example Program
Create an abstract class person. Derive two
classes Employee and Worker from it. Use
proper method to accept and display. The fields
of Employee are Emp_no, Emp_name and
address. Fields for worker are name and working
hours.
abstract class Person
{
public abstract void display();
}
class Employee extends Person
{
int Emp_no;
String Address;
String name;
public Employee(int Emp_no,String name,String Address)
{
this.Emp_no = Emp_no;
this.name=name;
this.Address=Address;
}
public void display()
{
System.out.println("Employee Details");
System.out.println("Employee Number"+Emp_no);
System.out.println("Employee Name"+name);
System.out.println("Employee Address"+Address);
} }
class Worker extends Person
{
int hours;
String name;
public Worker(String name,int hours)
{
this.name=name;
this.hours=hours;
}
public void display()
{ System.out.println("Worker Details");
System.out.println("Worker Name" +name);
System.out.println("Worker Hours" +hours);
} }
public class EmployeeDetails
{
public static void main(String args[])
{ Employee e=neww Employee(1,"Alok","Bangalore");
e.display();
Worker w=new Worker("Neelima",2);
} }
abstract class Shape
{ abstract void draw(); }
}
class Rectangle extends Shape{
void draw(){ System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){ System.out.println("drawing circle");}
}
class Test{
public static void main(String args[]){
Rectangle r=new Rectangle();
Circle1 c= new Circle1();
r.draw();
c.draw();
} }
final class
 A class declared as final cannot be inherited.
final class A
{…..}
class B extends A
{
……..}
Error – final classes cannot be inherited
Interfaces
Interfaces define only abstract methods and
final fields.
//Java Program to create an Interface and implement it in class.
import java.io.*;
import java.util.Scanner;
interface Shape
{
final static double pi=3.142;
public void area(float r);
}
class Circle implements Shape
{
float rad;
float area_circle;
public void area(float r)
{
rad=r;
area_circle=(float)pi*rad*rad;
System.out.println("Area is "+area_circle);
}
public static void main(String args[])
throws IOException
{
Circle c=new Circle();
float radius;
Scanner s=new Scanner(System.in);
System.out.println("Enter the radius");
radius=s.nextInt();
c.area(radius);
}
}
Multiple Inheritance
class Student
{
int roll_no;
void getNumber(int n)
{
roll_no=n;
}
void putNumber()
{
System.out.println("RollNo "+roll_no);
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println("Marks obtained");
System.out.println("part1 "+part1);
System.out.println("part2"+part2);
}
}
interface Sports
{
float sportwt=6.0f;
void putwt();
}
class Results extends Test implements Sports
{
float total;
public void putwt()
{
System.out.println("Sports weight"+sportwt);
}
void display()
{
total=part1+part2+sportwt;
putNumber();
putWt();
System.out.println("Total score" + total);
}
Extending interfaces
[public] interface InterfaceName
extends interfacel[, interface2, , interfaceN]
{//interface body}
interface I1
{
public void f1();
}
interface I2 extends I1
{
public void f2();
}
class x implements I2
{
public void f1()
{
System.out.println("Contents of Method f1() in Interface1");
}
public void f2()
{
System.out.println("Contents of Method f2() in Interface2");
}
public void f3()
{
System.out.println("Contents of Method f3() of Class X");
}
}
class ExtendingInterface
{
public static void main(String[] args)
{
Interface2 v2; //Reference variable of Interface2
v2 = new x(); //assign object of class x
v2.f1();
v2.f2();
x xl=new x();
xl.f3();
}
}

More Related Content

Similar to Inheritance.pptx

Core java concepts
Core java concepts Core java concepts
Core java concepts javeed_mhd
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphismUsama Malik
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 

Similar to Inheritance.pptx (20)

Core java concepts
Core java concepts Core java concepts
Core java concepts
 
Core Java
Core JavaCore Java
Core Java
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Day_2.1.pptx
Day_2.1.pptxDay_2.1.pptx
Day_2.1.pptx
 
Java oops features
Java oops featuresJava oops features
Java oops features
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
Introduccion del curso
Introduccion del cursoIntroduccion del curso
Introduccion del curso
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
7 inheritance
7 inheritance7 inheritance
7 inheritance
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Java unit2
Java unit2Java unit2
Java unit2
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
Java02
Java02Java02
Java02
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 

More from Karthik Rohan

More from Karthik Rohan (12)

unit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdfunit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdf
 
BSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptxBSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptx
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
SVM_Sample.pptx
SVM_Sample.pptxSVM_Sample.pptx
SVM_Sample.pptx
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
 
IT Unit III.pptx
IT Unit III.pptxIT Unit III.pptx
IT Unit III.pptx
 
13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
AI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptxAI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptx
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
UNIT4.2(VB).pptx
UNIT4.2(VB).pptxUNIT4.2(VB).pptx
UNIT4.2(VB).pptx
 
BD1.pptx
BD1.pptxBD1.pptx
BD1.pptx
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Inheritance.pptx

  • 1. Static keyword  The static variable can be used to refer the common property of all objects.  The static variable gets memory only once in class area at the time of class loading.
  • 2. //Program for static keyword class Exstatic { static void evenodd(int num) { if(num%2==0) System.out.println("The given number is even"); else System.out.println("The given number is odd"); } class Example { public static void main(String args[]) { Exstatic.evenodd(14); } }
  • 3. Inheritance Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. Syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields }
  • 5. //Single Inheritance class Single { static int num1=10; static int num2=5; } class Inherit extends Single { public static void main(String[] args) { int num3=2; int result=num1+num2+num3; System.out.println("Result of child class is "+result); } }
  • 6. //Multilevel Inheritance Class X { public void methodX() { System.out.println("Class X method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } } Class Z extends Y { public void methodZ() { System.out.println("class Z method"); } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); //calling grand parent class method obj.methodY(); //calling parent class method obj.methodZ(); //calling local method } }
  • 7. Method Overriding If subclass (child class) has the same method as declared in the parent class, it is known as Method Overriding.
  • 8. //Example of Method Overriding class Vehicle { void run() {System.out.println("Vehicle is running"); } } class Bike2 extends Vehicle { void run() { System.out.println("Bike is running safely"); } public static void main(String args[]) { Bike2 obj = new Bike2(); obj.run(); }
  • 9. Dynamic Method Dispatch Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how java implements runtime polymorphism.
  • 10. Example for Dynamic Method Dispatch class Game { public void type() { System.out.println("Indoor & outdoor"); } } Class Cricket extends Game { public void type() { System.out.println("outdoor game"); } public static void main(String[] args) { Game gm = new Game(); Cricket ck = new Cricket(); gm.type(); ck.type(); gm=ck; //gm refers to Cricket object gm.type(); //calls Cricket's version of type } }
  • 11. abstract keyword Abstraction is a process of hiding the implementation details and showing only functionality to the user.  Syntax: abstract class classname { ……… abstract void method(); ………}
  • 12. abstract class A { abstract void display(); public void method1() { System.out.println(“This is concrete method"); } } class B extends A { void display() { System.out.println(“This is abstract"); } public static void main(String[] args) { B b = new B(); b.display(); b.method1(); } }
  • 13. Example Program Create an abstract class person. Derive two classes Employee and Worker from it. Use proper method to accept and display. The fields of Employee are Emp_no, Emp_name and address. Fields for worker are name and working hours.
  • 14. abstract class Person { public abstract void display(); } class Employee extends Person { int Emp_no; String Address; String name; public Employee(int Emp_no,String name,String Address) { this.Emp_no = Emp_no; this.name=name; this.Address=Address; } public void display() { System.out.println("Employee Details"); System.out.println("Employee Number"+Emp_no); System.out.println("Employee Name"+name); System.out.println("Employee Address"+Address); } }
  • 15. class Worker extends Person { int hours; String name; public Worker(String name,int hours) { this.name=name; this.hours=hours; } public void display() { System.out.println("Worker Details"); System.out.println("Worker Name" +name); System.out.println("Worker Hours" +hours); } } public class EmployeeDetails { public static void main(String args[]) { Employee e=neww Employee(1,"Alok","Bangalore"); e.display(); Worker w=new Worker("Neelima",2); } }
  • 16. abstract class Shape { abstract void draw(); } } class Rectangle extends Shape{ void draw(){ System.out.println("drawing rectangle");} } class Circle1 extends Shape{ void draw(){ System.out.println("drawing circle");} } class Test{ public static void main(String args[]){ Rectangle r=new Rectangle(); Circle1 c= new Circle1(); r.draw(); c.draw(); } }
  • 17. final class  A class declared as final cannot be inherited. final class A {…..} class B extends A { ……..} Error – final classes cannot be inherited
  • 18. Interfaces Interfaces define only abstract methods and final fields.
  • 19. //Java Program to create an Interface and implement it in class. import java.io.*; import java.util.Scanner; interface Shape { final static double pi=3.142; public void area(float r); } class Circle implements Shape { float rad; float area_circle; public void area(float r) { rad=r; area_circle=(float)pi*rad*rad; System.out.println("Area is "+area_circle); }
  • 20. public static void main(String args[]) throws IOException { Circle c=new Circle(); float radius; Scanner s=new Scanner(System.in); System.out.println("Enter the radius"); radius=s.nextInt(); c.area(radius); } }
  • 21. Multiple Inheritance class Student { int roll_no; void getNumber(int n) { roll_no=n; } void putNumber() { System.out.println("RollNo "+roll_no); } class Test extends Student { float part1,part2; void getMarks(float m1,float m2) { part1=m1; part2=m2; }
  • 22. void putMarks() { System.out.println("Marks obtained"); System.out.println("part1 "+part1); System.out.println("part2"+part2); } } interface Sports { float sportwt=6.0f; void putwt(); }
  • 23. class Results extends Test implements Sports { float total; public void putwt() { System.out.println("Sports weight"+sportwt); } void display() { total=part1+part2+sportwt; putNumber(); putWt(); System.out.println("Total score" + total); }
  • 24. Extending interfaces [public] interface InterfaceName extends interfacel[, interface2, , interfaceN] {//interface body}
  • 25. interface I1 { public void f1(); } interface I2 extends I1 { public void f2(); } class x implements I2 { public void f1() { System.out.println("Contents of Method f1() in Interface1"); } public void f2() { System.out.println("Contents of Method f2() in Interface2"); }
  • 26. public void f3() { System.out.println("Contents of Method f3() of Class X"); } } class ExtendingInterface { public static void main(String[] args) { Interface2 v2; //Reference variable of Interface2 v2 = new x(); //assign object of class x v2.f1(); v2.f2(); x xl=new x(); xl.f3(); } }