SlideShare a Scribd company logo
Classes in java
• Java is an object-oriented programming language. Everything in Java is
associated with classes and objects, along with its attributes and methods.
• A Class is like an object constructor, or a "blueprint" for creating objects.
Create a class
To create a class, use the keyword class:
Syntax: class className {
// body of class
}
Example : class online{
public static void main(String[] ar){}
}
Objects in Java
• Object is an instance of a class.
• An object is created from a class.
Syntax to create an object:
ClassName objectName= new className();
• New is a keyword to assign memory location.
• className() is a constructor of a class.
Example: : class online{
public static void main(String[] ar){
online on= new online();
}
}
First java Program
Steps to write java program
1. Create a class
2. Write main method
public static void main(String[] ar){
Body of main method
}
To compile: javac filename.java
To run: java className
• For public class: file name and class name may or maynot be same.
Parameters used in First Java Program
• class keyword is used to declare a class in Java.
• public keyword is an access modifier that represents visibility. It means it is visible
to all.
• static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to create
an object to invoke the static method. The main() method is executed by the JVM,
so it doesn't require creating an object to invoke the main() method. So, it saves
memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String[] ar or String ar[] is used for command line argument. We will discuss it in
coming section.
• System.out.println() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class. We
will discuss the internal working of System.out.println()statement in the coming
section.
Inheritance in Java
• Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviours of a parent object. Or one class acquires the
properties and behaviours of another class.
• The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes. When you inherit from an existing class, you can
reuse methods and fields of the parent class.
• Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
• Why use inheritance in java?
1. For Method Overriding (so runtime polymorphism can be achieved).
2. For Code Reusability.
Inheritance in Java contd.
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
• Syntax:
Class subclassName extends SuperclassName{
//methods and fields
}
Note: extends keyword is used to inherit
• The extends keyword indicates that you are making a new class that derives
from an existing class. The meaning of "extends" is to increase the
functionality
Inheritance in Java contd.
• a class which is inherited is called a parent or superclass, and the new class is
called child or subclass.
Types of Inheritance in java:
On the basis of class, there can be three types of inheritance in java:
i. Single Inheritance
ii. Multilevel Inheritance
iii. Hierarchical Inheritance
But java has support of below Inheritance through interface only
iv. Multiple Inheritance: Java does not support multiple inheritance and Hybrid
inheritance because of complexity.
v. Hybrid Inheritance
Java Inheritance Example
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output :
Programmer salary is:40000.0
Bonus of programmer is:10000
Single Inheritance in java
• When a class inherits another class, it is known as a single inheritance.
• Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Single Inheritance Example
class Animal{
void eat(){
System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Output:
barking...
eating...
Multilevel Inheritance in Java
• When there is a chain of inheritance, it is known as multilevel inheritance.
• Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Class online extends off{
System.out.println(“this is multilevel”);
}
Multilevel Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping... barking... eating...
Hierarchical Inheritance in Java
• When two or more classes inherits a single class, it is known as hierarchical
inheritance.
• Example
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Class online extends on{
System.out.println(“this is multilevel”);
}
Hierarchical Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing... eating...
Multiple Inheritance in Java
• When one class inherits multiple classes, it is known as multiple inheritance
Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off {
System.out.println (“this is inherited”);
}
Class online extends off,on{ // suppose if it is true(but java does not support multiple
inheritance.
System.out.println(“this is multilevel”);
}
Multiple Inheritance Example
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
OUTPUT:
Compile Time Error
Thank You
Ad

More Related Content

Similar to inheritance.pptx (20)

Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
kumari36
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
inheritance
inheritanceinheritance
inheritance
Jay Prajapati
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
Inheritance Interface and Packags in java programming.pptx
Inheritance Interface and Packags in java programming.pptxInheritance Interface and Packags in java programming.pptx
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
CurativeServiceDivis
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Arnab Bhaumik
 
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUIChapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
HARIPRIYA M P
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
DrYogeshDeshmukh1
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
kumari36
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
Inheritance Interface and Packags in java programming.pptx
Inheritance Interface and Packags in java programming.pptxInheritance Interface and Packags in java programming.pptx
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUIChapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
DrYogeshDeshmukh1
 

Recently uploaded (20)

fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Ad

inheritance.pptx

  • 1. Classes in java • Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. • A Class is like an object constructor, or a "blueprint" for creating objects. Create a class To create a class, use the keyword class: Syntax: class className { // body of class } Example : class online{ public static void main(String[] ar){} }
  • 2. Objects in Java • Object is an instance of a class. • An object is created from a class. Syntax to create an object: ClassName objectName= new className(); • New is a keyword to assign memory location. • className() is a constructor of a class. Example: : class online{ public static void main(String[] ar){ online on= new online(); } }
  • 3. First java Program Steps to write java program 1. Create a class 2. Write main method public static void main(String[] ar){ Body of main method } To compile: javac filename.java To run: java className • For public class: file name and class name may or maynot be same.
  • 4. Parameters used in First Java Program • class keyword is used to declare a class in Java. • public keyword is an access modifier that represents visibility. It means it is visible to all. • static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory. • void is the return type of the method. It means it doesn't return any value. • main represents the starting point of the program. • String[] ar or String ar[] is used for command line argument. We will discuss it in coming section. • System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class. We will discuss the internal working of System.out.println()statement in the coming section.
  • 5. Inheritance in Java • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a parent object. Or one class acquires the properties and behaviours of another class. • The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. • Inheritance represents the IS-A relationship which is also known as a parent- child relationship. • Why use inheritance in java? 1. For Method Overriding (so runtime polymorphism can be achieved). 2. For Code Reusability.
  • 6. Inheritance in Java contd. • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. • Syntax: Class subclassName extends SuperclassName{ //methods and fields } Note: extends keyword is used to inherit • The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality
  • 7. Inheritance in Java contd. • a class which is inherited is called a parent or superclass, and the new class is called child or subclass. Types of Inheritance in java: On the basis of class, there can be three types of inheritance in java: i. Single Inheritance ii. Multilevel Inheritance iii. Hierarchical Inheritance But java has support of below Inheritance through interface only iv. Multiple Inheritance: Java does not support multiple inheritance and Hybrid inheritance because of complexity. v. Hybrid Inheritance
  • 8. Java Inheritance Example class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } } Output : Programmer salary is:40000.0 Bonus of programmer is:10000
  • 9. Single Inheritance in java • When a class inherits another class, it is known as a single inheritance. • Example: Class on { Void method{ System.out.println (“this is method”); } Class off extends on { System.out.println (“this is inherited”); }
  • 10. Single Inheritance Example class Animal{ void eat(){ System.out.println("eating...");} } class Dog extends Animal{ void bark(){ System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); }} Output: barking... eating...
  • 11. Multilevel Inheritance in Java • When there is a chain of inheritance, it is known as multilevel inheritance. • Example: Class on { Void method{ System.out.println (“this is method”); } Class off extends on { System.out.println (“this is inherited”); } Class online extends off{ System.out.println(“this is multilevel”); }
  • 12. Multilevel Inheritance Example class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ void weep(){System.out.println("weeping...");} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Output: weeping... barking... eating...
  • 13. Hierarchical Inheritance in Java • When two or more classes inherits a single class, it is known as hierarchical inheritance. • Example Class on { Void method{ System.out.println (“this is method”); } Class off extends on { System.out.println (“this is inherited”); } Class online extends on{ System.out.println(“this is multilevel”); }
  • 14. Hierarchical Inheritance Example class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class Cat extends Animal{ void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }} Output: meowing... eating...
  • 15. Multiple Inheritance in Java • When one class inherits multiple classes, it is known as multiple inheritance Example: Class on { Void method{ System.out.println (“this is method”); } Class off { System.out.println (“this is inherited”); } Class online extends off,on{ // suppose if it is true(but java does not support multiple inheritance. System.out.println(“this is multilevel”); }
  • 16. Multiple Inheritance Example class A{ void msg(){System.out.println("Hello");} } class B{ void msg(){System.out.println("Welcome");} } class C extends A,B{//suppose if it were public static void main(String args[]){ C obj=new C(); obj.msg();//Now which msg() method would be invoked? } } OUTPUT: Compile Time Error