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

PPTX
Unit3 part2-inheritance
PPTX
PPTX
Oop inheritance chapter 3
PPTX
Unit3 inheritance
PPSX
Learn java objects inheritance-overriding-polymorphism
PPTX
Basics to java programming and concepts of java
PPTX
Modules 333333333³3444444444444444444.pptx
PDF
java_inheritance.pdf
Unit3 part2-inheritance
Oop inheritance chapter 3
Unit3 inheritance
Learn java objects inheritance-overriding-polymorphism
Basics to java programming and concepts of java
Modules 333333333³3444444444444444444.pptx
java_inheritance.pdf

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

PPTX
INHERITANCE.pptx
PDF
Presentation 3.pdf
PDF
Java object oriented programming - OOPS
PPTX
Object orientation
PPT
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
PPTX
Inheritance & Polymorphism
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPS
Inheritance chepter 7
PPSX
Java.lang.object
PPTX
inheritance
PPTX
Inheritance Slides
PPTX
Chap3 inheritance
PPTX
Pi j3.1 inheritance
PPT
Super and final in java
PPTX
some basic knowledge about Java programming
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PPTX
Inheritance
PDF
Inheritance In Java
INHERITANCE.pptx
Presentation 3.pdf
Java object oriented programming - OOPS
Object orientation
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
Inheritance & Polymorphism
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Inheritance chepter 7
Java.lang.object
inheritance
Inheritance Slides
Chap3 inheritance
Pi j3.1 inheritance
Super and final in java
some basic knowledge about Java programming
encapsulation, inheritance, overriding, overloading
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Inheritance
Inheritance In Java
Ad

More from ravikapoorindia (20)

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

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
Insiders guide to clinical Medicine.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
VCE English Exam - Section C Student Revision Booklet
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program

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();