SlideShare a Scribd company logo
Inheritence
Java supports inheritance and thus, variables and methods of the superclass are inherited and can
be used by the subclass. but the private members of the superclass that cannot be accessed
directly from the subclass.
inheritenceexample.java
class Animal {//super class
public Animal() {
System.out.println("A new animal has been created!");
}
public void eat() {//super class methods eat and moves
System.out.println("An animal eats...");
}
public void moves() {
System.out.println("An animal movess...");
}
}
class Cow extends Animal {//Cow subclass
public Cow() {
super();//used to invoke super class constructer
System.out.println("A new cow has been created!");
}
@Override
public void eat() {
System.out.println("A Cow eats...");
}
@Override
public void moves() {
System.out.println("A Cow movess...");
}
}
class Dog extends Animal {//sub class Dog
public Dog() {
super();
System.out.println("A new dog has been created!");
}
@Override
public void eat() {
System.out.println("A dog eats...");
}
@Override
public void moves() {
System.out.println("A dog movess...");
}
}
public class inhertenceexample {//main class
public static void main(String[] args) {
Animal animal = new Animal();
Cow Cow = new Cow();
Dog dog = new Dog();
System.out.println();
animal.eat();
animal.moves();
Cow.eat();
Cow.moves();
dog.eat();
dog.moves();
}
}
output
A new animal has been created!
A new animal has been created!
A new cow has been created!
A new animal has been created!
A new dog has been created!
An animal eats...
An animal movess...
A Cow eats...
A Cow movess...
A dog eats...
A dog movess...
method overloading and method overiding
same multiple method's with different arguments is known as method overloading
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
method overloading example
calculationresult.java
class Calculationresult{
void mul(int a,int b){System.out.println(a*b);}
void mul(int a,int b,int c){System.out.println(a*b*c);}
public static void main(String args[]){
Calculationresult obj=new Calculationresult();
obj.mul(10,10,10);
obj.mul(20,20);
}
}
output
1000
400
method overiding
If subclass has the same method as declared in the parent class, it is known as method overriding
in java.
car.java
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class car extends Vehicle{
void run(){System.out.println("car is running safely");}
public static void main(String args[]){
car obj = new car();
obj.run();
}}
output
car is running safely
Solution
Inheritence
Java supports inheritance and thus, variables and methods of the superclass are inherited and can
be used by the subclass. but the private members of the superclass that cannot be accessed
directly from the subclass.
inheritenceexample.java
class Animal {//super class
public Animal() {
System.out.println("A new animal has been created!");
}
public void eat() {//super class methods eat and moves
System.out.println("An animal eats...");
}
public void moves() {
System.out.println("An animal movess...");
}
}
class Cow extends Animal {//Cow subclass
public Cow() {
super();//used to invoke super class constructer
System.out.println("A new cow has been created!");
}
@Override
public void eat() {
System.out.println("A Cow eats...");
}
@Override
public void moves() {
System.out.println("A Cow movess...");
}
}
class Dog extends Animal {//sub class Dog
public Dog() {
super();
System.out.println("A new dog has been created!");
}
@Override
public void eat() {
System.out.println("A dog eats...");
}
@Override
public void moves() {
System.out.println("A dog movess...");
}
}
public class inhertenceexample {//main class
public static void main(String[] args) {
Animal animal = new Animal();
Cow Cow = new Cow();
Dog dog = new Dog();
System.out.println();
animal.eat();
animal.moves();
Cow.eat();
Cow.moves();
dog.eat();
dog.moves();
}
}
output
A new animal has been created!
A new animal has been created!
A new cow has been created!
A new animal has been created!
A new dog has been created!
An animal eats...
An animal movess...
A Cow eats...
A Cow movess...
A dog eats...
A dog movess...
method overloading and method overiding
same multiple method's with different arguments is known as method overloading
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
method overloading example
calculationresult.java
class Calculationresult{
void mul(int a,int b){System.out.println(a*b);}
void mul(int a,int b,int c){System.out.println(a*b*c);}
public static void main(String args[]){
Calculationresult obj=new Calculationresult();
obj.mul(10,10,10);
obj.mul(20,20);
}
}
output
1000
400
method overiding
If subclass has the same method as declared in the parent class, it is known as method overriding
in java.
car.java
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class car extends Vehicle{
void run(){System.out.println("car is running safely");}
public static void main(String args[]){
car obj = new car();
obj.run();
}}
output
car is running safely

More Related Content

Similar to InheritenceJava supports inheritance and thus, variables and metho.pdf

ACM init() Day 6
ACM init() Day 6ACM init() Day 6
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
rithustutorials
 
Object orientation
Object orientationObject orientation
Object orientation
sagsharma
 
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
argsstring
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
SAGARDAVE29
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
Soham Sengupta
 
inheritance
inheritanceinheritance
inheritance
Jay Prajapati
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
Ahsan Raja
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
raksharao
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
anshu_atri
 
some basic knowledge about Java programming
some basic knowledge about Java programming some basic knowledge about Java programming
some basic knowledge about Java programming
ITTraining2
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Inheritance
InheritanceInheritance
Inheritance
Daman Toor
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Arnab Bhaumik
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
BHUVIJAYAVELU
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 

Similar to InheritenceJava supports inheritance and thus, variables and metho.pdf (20)

ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
 
Object orientation
Object orientationObject orientation
Object orientation
 
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
 
some basic knowledge about Java programming
some basic knowledge about Java programming some basic knowledge about Java programming
some basic knowledge about Java programming
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 

More from ravikapoorindia

1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
ravikapoorindia
 
1. According to morphological species concept focus on external phys.pdf
1. According to morphological species concept focus on external phys.pdf1. According to morphological species concept focus on external phys.pdf
1. According to morphological species concept focus on external phys.pdf
ravikapoorindia
 
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
ravikapoorindia
 
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdf
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdfViral genomes may be circular, as in the polyomaviruses, or linear, .pdf
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdf
ravikapoorindia
 
True. gaps are the reason for electrical conductivity.Solution.pdf
True. gaps are the reason for electrical conductivity.Solution.pdfTrue. gaps are the reason for electrical conductivity.Solution.pdf
True. gaps are the reason for electrical conductivity.Solution.pdf
ravikapoorindia
 
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdfThis is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
ravikapoorindia
 
The Vestibular System, which is a contributed to our balance system .pdf
The Vestibular System, which is a contributed to our balance system .pdfThe Vestibular System, which is a contributed to our balance system .pdf
The Vestibular System, which is a contributed to our balance system .pdf
ravikapoorindia
 
The inherent risk for an assertion about a derivative is its suscept.pdf
The inherent risk for an assertion about a derivative is its suscept.pdfThe inherent risk for an assertion about a derivative is its suscept.pdf
The inherent risk for an assertion about a derivative is its suscept.pdf
ravikapoorindia
 
Solution I ) Average inventory = $610,000 5 = $122000Total Inven.pdf
Solution I ) Average inventory = $610,000  5 = $122000Total Inven.pdfSolution I ) Average inventory = $610,000  5 = $122000Total Inven.pdf
Solution I ) Average inventory = $610,000 5 = $122000Total Inven.pdf
ravikapoorindia
 
1 D2 the decrease in entropy of the system is offset by an incr.pdf
1 D2  the decrease in entropy of the system is offset by an incr.pdf1 D2  the decrease in entropy of the system is offset by an incr.pdf
1 D2 the decrease in entropy of the system is offset by an incr.pdf
ravikapoorindia
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
NaCl + H2OSolutionNaCl + H2O.pdf
NaCl + H2OSolutionNaCl + H2O.pdfNaCl + H2OSolutionNaCl + H2O.pdf
NaCl + H2OSolutionNaCl + H2O.pdf
ravikapoorindia
 
Miller is US based investor and co-founder and current chairman of U.pdf
Miller is US based investor and co-founder and current chairman of U.pdfMiller is US based investor and co-founder and current chairman of U.pdf
Miller is US based investor and co-founder and current chairman of U.pdf
ravikapoorindia
 
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdf
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdfMarijuana plant belongs to the genus Cannabis. It is native to the C.pdf
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdf
ravikapoorindia
 
It is the temporal lobe of cerebrum. It is situated beneath the late.pdf
It is the temporal lobe of cerebrum. It is situated beneath the late.pdfIt is the temporal lobe of cerebrum. It is situated beneath the late.pdf
It is the temporal lobe of cerebrum. It is situated beneath the late.pdf
ravikapoorindia
 
In cat,The ductus deferens also called the vas deferens leaves the t.pdf
In cat,The ductus deferens also called the vas deferens leaves the t.pdfIn cat,The ductus deferens also called the vas deferens leaves the t.pdf
In cat,The ductus deferens also called the vas deferens leaves the t.pdf
ravikapoorindia
 
LeadCarbonate PbCO3 is ionic compound. electrostaticforces.pdf
  LeadCarbonate  PbCO3 is ionic compound. electrostaticforces.pdf  LeadCarbonate  PbCO3 is ionic compound. electrostaticforces.pdf
LeadCarbonate PbCO3 is ionic compound. electrostaticforces.pdf
ravikapoorindia
 
Vo.pdf
   Vo.pdf   Vo.pdf
Lithium has 3 electrons. Since an s orbital only .pdf
                     Lithium has 3 electrons. Since an s orbital only .pdf                     Lithium has 3 electrons. Since an s orbital only .pdf
Lithium has 3 electrons. Since an s orbital only .pdf
ravikapoorindia
 
IO3- only exhibitsresonance ,since the lone pairs.pdf
                     IO3- only exhibitsresonance ,since the lone pairs.pdf                     IO3- only exhibitsresonance ,since the lone pairs.pdf
IO3- only exhibitsresonance ,since the lone pairs.pdf
ravikapoorindia
 

More from ravikapoorindia (20)

1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
1.The Excavata includes taxa that are photosynthetic, parasitic, sym.pdf
 
1. According to morphological species concept focus on external phys.pdf
1. According to morphological species concept focus on external phys.pdf1. According to morphological species concept focus on external phys.pdf
1. According to morphological species concept focus on external phys.pdf
 
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
1)calcium(pH-dependent regulation of lysosomal calcium in macrophage.pdf
 
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdf
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdfViral genomes may be circular, as in the polyomaviruses, or linear, .pdf
Viral genomes may be circular, as in the polyomaviruses, or linear, .pdf
 
True. gaps are the reason for electrical conductivity.Solution.pdf
True. gaps are the reason for electrical conductivity.Solution.pdfTrue. gaps are the reason for electrical conductivity.Solution.pdf
True. gaps are the reason for electrical conductivity.Solution.pdf
 
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdfThis is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
This is an example of a Lewis Acid. The CO2 acts like an acid becaus.pdf
 
The Vestibular System, which is a contributed to our balance system .pdf
The Vestibular System, which is a contributed to our balance system .pdfThe Vestibular System, which is a contributed to our balance system .pdf
The Vestibular System, which is a contributed to our balance system .pdf
 
The inherent risk for an assertion about a derivative is its suscept.pdf
The inherent risk for an assertion about a derivative is its suscept.pdfThe inherent risk for an assertion about a derivative is its suscept.pdf
The inherent risk for an assertion about a derivative is its suscept.pdf
 
Solution I ) Average inventory = $610,000 5 = $122000Total Inven.pdf
Solution I ) Average inventory = $610,000  5 = $122000Total Inven.pdfSolution I ) Average inventory = $610,000  5 = $122000Total Inven.pdf
Solution I ) Average inventory = $610,000 5 = $122000Total Inven.pdf
 
1 D2 the decrease in entropy of the system is offset by an incr.pdf
1 D2  the decrease in entropy of the system is offset by an incr.pdf1 D2  the decrease in entropy of the system is offset by an incr.pdf
1 D2 the decrease in entropy of the system is offset by an incr.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
NaCl + H2OSolutionNaCl + H2O.pdf
NaCl + H2OSolutionNaCl + H2O.pdfNaCl + H2OSolutionNaCl + H2O.pdf
NaCl + H2OSolutionNaCl + H2O.pdf
 
Miller is US based investor and co-founder and current chairman of U.pdf
Miller is US based investor and co-founder and current chairman of U.pdfMiller is US based investor and co-founder and current chairman of U.pdf
Miller is US based investor and co-founder and current chairman of U.pdf
 
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdf
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdfMarijuana plant belongs to the genus Cannabis. It is native to the C.pdf
Marijuana plant belongs to the genus Cannabis. It is native to the C.pdf
 
It is the temporal lobe of cerebrum. It is situated beneath the late.pdf
It is the temporal lobe of cerebrum. It is situated beneath the late.pdfIt is the temporal lobe of cerebrum. It is situated beneath the late.pdf
It is the temporal lobe of cerebrum. It is situated beneath the late.pdf
 
In cat,The ductus deferens also called the vas deferens leaves the t.pdf
In cat,The ductus deferens also called the vas deferens leaves the t.pdfIn cat,The ductus deferens also called the vas deferens leaves the t.pdf
In cat,The ductus deferens also called the vas deferens leaves the t.pdf
 
LeadCarbonate PbCO3 is ionic compound. electrostaticforces.pdf
  LeadCarbonate  PbCO3 is ionic compound. electrostaticforces.pdf  LeadCarbonate  PbCO3 is ionic compound. electrostaticforces.pdf
LeadCarbonate PbCO3 is ionic compound. electrostaticforces.pdf
 
Vo.pdf
   Vo.pdf   Vo.pdf
Vo.pdf
 
Lithium has 3 electrons. Since an s orbital only .pdf
                     Lithium has 3 electrons. Since an s orbital only .pdf                     Lithium has 3 electrons. Since an s orbital only .pdf
Lithium has 3 electrons. Since an s orbital only .pdf
 
IO3- only exhibitsresonance ,since the lone pairs.pdf
                     IO3- only exhibitsresonance ,since the lone pairs.pdf                     IO3- only exhibitsresonance ,since the lone pairs.pdf
IO3- only exhibitsresonance ,since the lone pairs.pdf
 

Recently uploaded

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 

Recently uploaded (20)

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 

InheritenceJava supports inheritance and thus, variables and metho.pdf

  • 1. Inheritence Java supports inheritance and thus, variables and methods of the superclass are inherited and can be used by the subclass. but the private members of the superclass that cannot be accessed directly from the subclass. inheritenceexample.java class Animal {//super class public Animal() { System.out.println("A new animal has been created!"); } public void eat() {//super class methods eat and moves System.out.println("An animal eats..."); } public void moves() { System.out.println("An animal movess..."); } } class Cow extends Animal {//Cow subclass public Cow() { super();//used to invoke super class constructer System.out.println("A new cow has been created!"); } @Override public void eat() { System.out.println("A Cow eats..."); } @Override public void moves() { System.out.println("A Cow movess..."); } } class Dog extends Animal {//sub class Dog public Dog() { super(); System.out.println("A new dog has been created!"); }
  • 2. @Override public void eat() { System.out.println("A dog eats..."); } @Override public void moves() { System.out.println("A dog movess..."); } } public class inhertenceexample {//main class public static void main(String[] args) { Animal animal = new Animal(); Cow Cow = new Cow(); Dog dog = new Dog(); System.out.println(); animal.eat(); animal.moves(); Cow.eat(); Cow.moves(); dog.eat(); dog.moves(); } } output A new animal has been created! A new animal has been created! A new cow has been created! A new animal has been created! A new dog has been created! An animal eats... An animal movess... A Cow eats...
  • 3. A Cow movess... A dog eats... A dog movess... method overloading and method overiding same multiple method's with different arguments is known as method overloading There are two ways to overload the method in java By changing number of arguments By changing the data type method overloading example calculationresult.java class Calculationresult{ void mul(int a,int b){System.out.println(a*b);} void mul(int a,int b,int c){System.out.println(a*b*c);} public static void main(String args[]){ Calculationresult obj=new Calculationresult(); obj.mul(10,10,10); obj.mul(20,20); } } output 1000 400 method overiding If subclass has the same method as declared in the parent class, it is known as method overriding in java. car.java class Vehicle{ void run(){System.out.println("Vehicle is running");} } class car extends Vehicle{ void run(){System.out.println("car is running safely");} public static void main(String args[]){ car obj = new car(); obj.run(); }} output
  • 4. car is running safely Solution Inheritence Java supports inheritance and thus, variables and methods of the superclass are inherited and can be used by the subclass. but the private members of the superclass that cannot be accessed directly from the subclass. inheritenceexample.java class Animal {//super class public Animal() { System.out.println("A new animal has been created!"); } public void eat() {//super class methods eat and moves System.out.println("An animal eats..."); } public void moves() { System.out.println("An animal movess..."); } } class Cow extends Animal {//Cow subclass public Cow() { super();//used to invoke super class constructer System.out.println("A new cow has been created!"); } @Override public void eat() { System.out.println("A Cow eats..."); } @Override public void moves() { System.out.println("A Cow movess..."); } } class Dog extends Animal {//sub class Dog public Dog() {
  • 5. super(); System.out.println("A new dog has been created!"); } @Override public void eat() { System.out.println("A dog eats..."); } @Override public void moves() { System.out.println("A dog movess..."); } } public class inhertenceexample {//main class public static void main(String[] args) { Animal animal = new Animal(); Cow Cow = new Cow(); Dog dog = new Dog(); System.out.println(); animal.eat(); animal.moves(); Cow.eat(); Cow.moves(); dog.eat(); dog.moves(); } } output A new animal has been created! A new animal has been created! A new cow has been created! A new animal has been created! A new dog has been created!
  • 6. An animal eats... An animal movess... A Cow eats... A Cow movess... A dog eats... A dog movess... method overloading and method overiding same multiple method's with different arguments is known as method overloading There are two ways to overload the method in java By changing number of arguments By changing the data type method overloading example calculationresult.java class Calculationresult{ void mul(int a,int b){System.out.println(a*b);} void mul(int a,int b,int c){System.out.println(a*b*c);} public static void main(String args[]){ Calculationresult obj=new Calculationresult(); obj.mul(10,10,10); obj.mul(20,20); } } output 1000 400 method overiding If subclass has the same method as declared in the parent class, it is known as method overriding in java. car.java class Vehicle{ void run(){System.out.println("Vehicle is running");} } class car extends Vehicle{ void run(){System.out.println("car is running safely");} public static void main(String args[]){ car obj = new car();