SlideShare a Scribd company logo
Inheritance
Inheritance
• Inheritance in java is a mechanism in which one
object acquires all the properties and behaviors
of parent object
• 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 parent class, and you can add new methods
and fields also.
• Inheritance represents the IS-A relationship, also
known as parent-child relationship.
Why use inheritance in java
• For Method Overriding (so runtime
polymorphism can be achieved).
• For Code Reusability.
• Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a
new class that derives from an existing class.
Example
• As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. Relationship between two classes is Programmer IS-A Employee.It
means that Programmer is a type of Employee.
• In the above example, Programmer object can access the field of own class as
well as of Employee class i.e. code reusability.
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
super keyword in java
• The super keyword in java is a reference variable that is
used to refer immediate parent class object.
• Whenever you create the instance of subclass, an
instance of parent class is created implicitly i.e.
referred by super reference variable.
• Usage of java super Keyword
– super is used to refer immediate parent class instance
variable.
– super() is used to invoke immediate parent class
constructor.
– super is used to invoke immediate parent class method.
• super is used to refer immediate parent class
instance variable.
class Vehicle{
int speed=50;
}
class Bike3 extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[]){
Bike3 b=new Bike3();
b.display();
}
}
Output:100
//example of super keyword
class Vehicle{
int speed=50;
}
class Bike4 extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike4 b=new Bike4();
b.display();
}
}
Output:50
super is used to invoke parent class
constructor.
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike5 extends Vehicle{
Bike5(){
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike5 b=new Bike5();
}
}
Output:
Vehicle is created
Bike is created
Program in case super is not required
class Person{
void message(){System.out.println("welcome");}
}
class Student17 extends Person{
void display(){
message();//will invoke parent class message() method
}
public static void main(String args[]){
Student17 s=new Student17();
s.display();
}
}
Output:welcome
super can be used to invoke parent
class method
class Person{
void message(){System.out.println("welcome");}
}
class Student16 extends Person{
void message(){System.out.println("welcome to java");}
void display(){
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[]){
Student16 s=new Student16();
s.display();
}
}
Output:
welcome to java
welcome
public class Car {
public void m1() {
System.out.println("car 1");
}
public void m2() {
System.out.println("car 2");
}
public String toString() {
return "vroom";
}
}
public class Truck extends Car {
public void m1() {
System.out.println("truck 1");
}
}
What is the output from the following code?
Truck mycar = new Truck();
System.out.println(mycar);
mycar.m1();
mycar.m2();
public class Car {
public void m1() {
System.out.println("car 1");
}
public void m2() {
System.out.println("car 2");
}
public String toString() {
return "vroom";
}
}
public class Truck extends Car {
public void m1() {
System.out.println("truck 1");
}
public void m2() {
super.m1();
}
public String toString() {
return super.toString() +
super.toString();
}
}
Suppose the Truck code changes as shown above. What is the output now?
Truck mycar = new Truck();
System.out.println(mycar);
mycar.m1();
mycar.m2();
Types of inheritance in java
Inheritance1
Inheritance1
Inheritance1
Inheritance1
Inheritance1

More Related Content

PPTX
PROGRAMMING IN JAVA
PPTX
PROGRAMMING IN JAVA-unit 3-part II
PPTX
Java component
PPTX
Function different types of funtion
PPTX
Super keyword in java
DOC
New Microsoft Word Document.doc
PPTX
constructor , operator overloading ,friend function
PPTX
Invoke component
PROGRAMMING IN JAVA
PROGRAMMING IN JAVA-unit 3-part II
Java component
Function different types of funtion
Super keyword in java
New Microsoft Word Document.doc
constructor , operator overloading ,friend function
Invoke component

What's hot (18)

PPTX
PROGRAMMING IN JAVA- unit 4-part I
PPTX
Java Inheritance - sub class constructors - Method overriding
PPTX
Lec 1.10 Object Oriented Programming
PPTX
Java 102 intro to object-oriented programming in java
PPT
Java interface
PPTX
PPTX
Building maintainable web apps with Angular MS TechDays 2017
PPTX
Mule java part-4
PPT
java packages
PDF
Packages
PDF
Java - Interfaces & Packages
DOCX
Differences between method overloading and method overriding
PPTX
Inheritance
PPTX
Pi j4.1 packages
PPT
Java htp6e 09
PPTX
PROGRAMMING IN JAVA
PPTX
Java unit1 a- History of Java to string
PPS
Interface
PROGRAMMING IN JAVA- unit 4-part I
Java Inheritance - sub class constructors - Method overriding
Lec 1.10 Object Oriented Programming
Java 102 intro to object-oriented programming in java
Java interface
Building maintainable web apps with Angular MS TechDays 2017
Mule java part-4
java packages
Packages
Java - Interfaces & Packages
Differences between method overloading and method overriding
Inheritance
Pi j4.1 packages
Java htp6e 09
PROGRAMMING IN JAVA
Java unit1 a- History of Java to string
Interface
Ad

Similar to Inheritance1 (20)

PPTX
Java inheritance
PPTX
Chap-3 Inheritance.pptx
PDF
OOPs Concepts - Android Programming
PPT
A457405934_21789_26_2018_Inheritance.ppt
PPTX
OOP with Java - Part 3
PPTX
Inheritance
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
inheritance and interface in oops with java .pptx
PPSX
OOP with Java - Part 3
PPTX
Basics to java programming and concepts of java
PPT
web program-Inheritance,pack&except in Java.ppt
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPTX
Session 09 - OOP with Java - Part 3
PPT
Java: Inheritance
PPTX
inheritance in Java with sample program.pptx
PPTX
Oop concepts classes Method Overriding.pptx
PPTX
Inheritance in oop
PDF
java_inheritance.pdf
PPT
Java inheritance
PDF
Inheritance and interface
Java inheritance
Chap-3 Inheritance.pptx
OOPs Concepts - Android Programming
A457405934_21789_26_2018_Inheritance.ppt
OOP with Java - Part 3
Inheritance
Modules 333333333³3444444444444444444.pptx
inheritance and interface in oops with java .pptx
OOP with Java - Part 3
Basics to java programming and concepts of java
web program-Inheritance,pack&except in Java.ppt
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Session 09 - OOP with Java - Part 3
Java: Inheritance
inheritance in Java with sample program.pptx
Oop concepts classes Method Overriding.pptx
Inheritance in oop
java_inheritance.pdf
Java inheritance
Inheritance and interface
Ad

More from Daman Toor (14)

DOCX
Lab exam 5_5_15
DOCX
Lab exam setb_5_5_2015
PPT
String slide
PPTX
Nested class
DOCX
Uta005
DOC
Lab exp (creating classes and objects)
DOCX
Practice
DOCX
Parking ticket simulator program
DOC
Lab exp (declaring classes)
DOC
Lab exp declaring arrays)
DOCX
Java assignment 1
PPT
Operators
PPTX
Identifiers, keywords and types
PPTX
Classes & object
Lab exam 5_5_15
Lab exam setb_5_5_2015
String slide
Nested class
Uta005
Lab exp (creating classes and objects)
Practice
Parking ticket simulator program
Lab exp (declaring classes)
Lab exp declaring arrays)
Java assignment 1
Operators
Identifiers, keywords and types
Classes & object

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Complications of Minimal Access Surgery at WLH
PPTX
master seminar digital applications in india
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Classroom Observation Tools for Teachers
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Microbial disease of the cardiovascular and lymphatic systems
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
Sports Quiz easy sports quiz sports quiz
Classroom Observation Tools for Teachers
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Renaissance Architecture: A Journey from Faith to Humanism
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
Basic Mud Logging Guide for educational purpose
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems

Inheritance1

  • 2. Inheritance • Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object • 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 parent class, and you can add new methods and fields also. • Inheritance represents the IS-A relationship, also known as parent-child relationship.
  • 3. Why use inheritance in java • For Method Overriding (so runtime polymorphism can be achieved). • For Code Reusability. • Syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields } The extends keyword indicates that you are making a new class that derives from an existing class.
  • 4. Example • As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee. • In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability. 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
  • 5. super keyword in java • The super keyword in java is a reference variable that is used to refer immediate parent class object. • Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable. • Usage of java super Keyword – super is used to refer immediate parent class instance variable. – super() is used to invoke immediate parent class constructor. – super is used to invoke immediate parent class method.
  • 6. • super is used to refer immediate parent class instance variable. class Vehicle{ int speed=50; } class Bike3 extends Vehicle{ int speed=100; void display(){ System.out.println(speed);//will print speed of Bike } public static void main(String args[]){ Bike3 b=new Bike3(); b.display(); } } Output:100
  • 7. //example of super keyword class Vehicle{ int speed=50; } class Bike4 extends Vehicle{ int speed=100; void display(){ System.out.println(super.speed);//will print speed of Vehicle now } public static void main(String args[]){ Bike4 b=new Bike4(); b.display(); } } Output:50
  • 8. super is used to invoke parent class constructor. class Vehicle{ Vehicle(){System.out.println("Vehicle is created");} } class Bike5 extends Vehicle{ Bike5(){ super();//will invoke parent class constructor System.out.println("Bike is created"); } public static void main(String args[]){ Bike5 b=new Bike5(); } } Output: Vehicle is created Bike is created
  • 9. Program in case super is not required class Person{ void message(){System.out.println("welcome");} } class Student17 extends Person{ void display(){ message();//will invoke parent class message() method } public static void main(String args[]){ Student17 s=new Student17(); s.display(); } } Output:welcome
  • 10. super can be used to invoke parent class method class Person{ void message(){System.out.println("welcome");} } class Student16 extends Person{ void message(){System.out.println("welcome to java");} void display(){ message();//will invoke current class message() method super.message();//will invoke parent class message() method } public static void main(String args[]){ Student16 s=new Student16(); s.display(); } } Output: welcome to java welcome
  • 11. public class Car { public void m1() { System.out.println("car 1"); } public void m2() { System.out.println("car 2"); } public String toString() { return "vroom"; } } public class Truck extends Car { public void m1() { System.out.println("truck 1"); } } What is the output from the following code? Truck mycar = new Truck(); System.out.println(mycar); mycar.m1(); mycar.m2();
  • 12. public class Car { public void m1() { System.out.println("car 1"); } public void m2() { System.out.println("car 2"); } public String toString() { return "vroom"; } } public class Truck extends Car { public void m1() { System.out.println("truck 1"); } public void m2() { super.m1(); } public String toString() { return super.toString() + super.toString(); } } Suppose the Truck code changes as shown above. What is the output now? Truck mycar = new Truck(); System.out.println(mycar); mycar.m1(); mycar.m2();