SlideShare a Scribd company logo
Inheritance, Method Overriding,
Abstraction & Super keyword
Prepared by
Yaswanth Narikamalli
Assistant Professor
Kristu Jayanti College
Bangalore
Course code:23BCL2T331
Inheritance
It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class.
Inheritance means creating new classes based on existing ones.
A class that inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and methods
to your current class as well.
Terminologies
● Class: Class is a set of objects which shares common characteristics/ behavior and
common properties/ attributes.
● Super Class/Parent Class: The class whose features are inherited is known as a superclass
● Sub Class/Child Class: The class that inherits the other class is known as a subclass.The
subclass can add its own fields and methods in addition to the superclass fields and
methods.
● Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we
can derive our new class from the existing class.
How to Use Inheritance in Java?
The extends keyword is used for inheritance in Java. Using the
extends keyword indicates you are derived from an existing class.
Syntax
class DerivedClass extends BaseClass
{
//methods and fields
}
Example
import java.io.*;
class Employee {
int salary = 60000;
}
class Engineer extends Employee {
int benefits = 10000;
}
class Worker{
public static void main(String args[])
{
Engineer E1 = new Engineer();
System.out.println("Salary : " + E1.salary + "nBenefits : " + E1.benefits);
}
}
Types of Inheritance
Prepared by
Yaswanth Narikamalli
Assistant Professor
Kristu Jayanti College
Bangalore
Course code:23BCL2T331
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Single inheritance
A sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes, it is also
known as simple inheritance.
// concept of single inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
class One {
public void print_class()
{
System.out.println("BCA cloud computing");
}
}
class Two extends One {
public void print_sec(){ System.out.println("Section B"); }
}
public class Main {
public static void main(String[] args)
{
Two g = new Two();
g.print_class();
g.print_sec();
}
}
Multilevel Inheritance
A derived class will be inheriting a base class, and as well as the
derived class also acts as the base class for other classes.
//multilevel inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
class One {
public void print_class() { System.out.println("BCA cloud computing"); }
}
class Two extends One {
public void print_sec(){ System.out.println(" Section B"); }
}
class Three extends Two{
public void print_CA(){ System.out.println(" Class Animator"); }
}
public class Main {
public static void main(String[] args)
{
Three g = new Three();
g.print_class();
g.print_sec();
g.print_CA();
}
}
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base
class) for more than one subclass.
// Java program to illustrate the hierarchical inheritance
class A { public void print_A() { System.out.println("Class A"); }}
class B extends A { public void print_B() { System.out.println("Class B"); }}
class C extends A { public void print_C() { System.out.println("Class C"); }}
class D extends A { public void print_D() { System.out.println("Class D"); } }
public class Test {
public static void main(String[] args)
{
B objB = new B();
objB.print_A();
objB.print_B();
C objC = new C();
objC.print_A();
objC.print_C();
D objD = new D();
objD.print_A();
objD.print_D();
}
}
Multiple inheritances
In Multiple inheritances, one class can have more than one superclass
and inherit features from all parent classes. Please note that Java
does not support multiple inheritances with classes. In Java, we can
achieve multiple inheritances only through Interfaces.
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java
doesn’t support multiple inheritances with classes, hybrid inheritance
involving multiple inheritance is also not possible with classes. In
Java, we can achieve hybrid inheritance only through Interfaces
Advantages of Inheritance
Code reusability
Abstraction
Class Hierarchy
Polymorphism
Disadvantages of Inheritance
Complexity
Tight Coupling
Overriding
Overriding is a feature that allows a subclass or child class to provide
a specific implementation of a method that is already provided by one
of its super-classes or parent classes.
// Java program to demonstrate
// method overriding in java
// Base Class
class Parent {
void show() { System.out.println("Parent's show"); }
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override void show()
{
System.out.println("Child's show");
} }
// Driver class
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
Abstraction
Abstraction in Java refers to hiding the implementation details of a
code and exposing only the necessary information to the user.
It provides the ability to simplify complex systems by ignoring
irrelevant details and reducing complexity.
Abstraction allows developers to represent complex systems by
simplifying them into their essential characteristics while hiding
unnecessary implementation
Inheritance & interface ppt  Inheritance
Abstract class example
1. abstract class Bike{
2. abstract void run();
3. }
4. class Honda extends Bike{
5. void run(){System.out.println("running safely");}
6. public static void main(String args[]){
7. Honda obj = new Honda();
8. obj.run();
9. }
10. }
Super Keyword
The super keyword in Java is a reference variable which is used to
refer immediate parent class object.
1. super can be used to refer immediate parent class instance
variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.
Inheritance & interface ppt  Inheritance
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of
Animal class
}
}
class Test2{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
}
class Test3{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class Test4{
public static void main(String args[]){
Dog d=new Dog();
}}
Interface
An interface in Java is a blueprint of a class. It has static constants
and abstract methods.
○ It is used to achieve abstraction.
○ By interface, we can support the functionality of multiple
inheritance.
○ It can be used to achieve loose coupling.
Syntax
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
The relationship between classes and interfaces
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11.}
How to extend Interface
An interface extends another interface like a class implements
an interface in interface inheritance.
When one interface inherits from another, the sub-interface
inherits all of the methods and constants declared by the super-
interface.
It can declare new abstract procedures and constants.
To extend an interface, use the extends keyword
interface A {
void funcA();
}
interface B extends A {
void funcB();
}
class C implements B {
public void funcA() {
System.out.println("This is funcA");
}
public void funcB() {
System.out.println("This is funcB");
}
}
public class Demo5 {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
}
}
Packages
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better
maintainable code.
Packages are used for:
● Preventing naming conflicts.
● Making searching/locating and usage of classes easier.
● Providing controlled access
● Packages can be considered as data encapsulation
Built-in Packages
1. Java.lang
2. java.io
3. java.util
4. java.applet
5. java.awt
6. java.net
User Defined Packages
User-defined packages: These are the packages that are defined by the user.
First we create a directory myPackage Then create the MyClass inside the directory with the first
statement being the package names.

More Related Content

Similar to Inheritance & interface ppt Inheritance (20)

PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
PPTX
Inheritance in oop
MuskanNazeer
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPTX
Java Inheritance
Manish Tiwari
 
PPTX
Interesting Concept of Object Oriented Programming
Prognoz Technologies Pvt. Ltd.
 
PPTX
UNIT 5.pptx
CurativeServiceDivis
 
PPTX
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
PPTX
Basics to java programming and concepts of java
1747503gunavardhanre
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
ITTutor Advanced Java (1).pptx
kristinatemen
 
PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
Inheritance ppt
Nivegeetha
 
PDF
Inheritance and interface
Shubham Sharma
 
PPT
Ap Power Point Chpt7
dplunkett
 
PPTX
Java(inheritance)
Pooja Bhojwani
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PDF
java_vyshali.pdf
Vyshali6
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
Inheritance in oop
MuskanNazeer
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
itft-Inheritance in java
Atul Sehdev
 
Java Inheritance
Manish Tiwari
 
Interesting Concept of Object Oriented Programming
Prognoz Technologies Pvt. Ltd.
 
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Basics to java programming and concepts of java
1747503gunavardhanre
 
Inheritance in java
Tech_MX
 
ITTutor Advanced Java (1).pptx
kristinatemen
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Ch5 inheritance
HarshithaAllu
 
Inheritance ppt
Nivegeetha
 
Inheritance and interface
Shubham Sharma
 
Ap Power Point Chpt7
dplunkett
 
Java(inheritance)
Pooja Bhojwani
 
object oriented programming unit two ppt
isiagnel2
 
java_vyshali.pdf
Vyshali6
 

More from narikamalliy (6)

PPTX
Introduction to Software Engineering and Models pptx
narikamalliy
 
PPTX
Unit 5: Open Source as a Culture and Aspects
narikamalliy
 
PPTX
Entity Relationship Diagram and Constraints
narikamalliy
 
PPTX
Procedural Language Extension to Structured Query Language
narikamalliy
 
PPTX
IoT System Management ppt SNMP simple network
narikamalliy
 
PPTX
UNIT 2_cloud Computing.pptx Virtualization
narikamalliy
 
Introduction to Software Engineering and Models pptx
narikamalliy
 
Unit 5: Open Source as a Culture and Aspects
narikamalliy
 
Entity Relationship Diagram and Constraints
narikamalliy
 
Procedural Language Extension to Structured Query Language
narikamalliy
 
IoT System Management ppt SNMP simple network
narikamalliy
 
UNIT 2_cloud Computing.pptx Virtualization
narikamalliy
 
Ad

Recently uploaded (20)

PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Digital water marking system project report
Kamal Acharya
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Ad

Inheritance & interface ppt Inheritance

  • 1. Inheritance, Method Overriding, Abstraction & Super keyword Prepared by Yaswanth Narikamalli Assistant Professor Kristu Jayanti College Bangalore Course code:23BCL2T331
  • 2. Inheritance It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.
  • 3. Terminologies ● Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. ● Super Class/Parent Class: The class whose features are inherited is known as a superclass ● Sub Class/Child Class: The class that inherits the other class is known as a subclass.The subclass can add its own fields and methods in addition to the superclass fields and methods. ● Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class.
  • 4. How to Use Inheritance in Java? The extends keyword is used for inheritance in Java. Using the extends keyword indicates you are derived from an existing class. Syntax class DerivedClass extends BaseClass { //methods and fields }
  • 5. Example import java.io.*; class Employee { int salary = 60000; } class Engineer extends Employee { int benefits = 10000; } class Worker{ public static void main(String args[]) { Engineer E1 = new Engineer(); System.out.println("Salary : " + E1.salary + "nBenefits : " + E1.benefits); } }
  • 6. Types of Inheritance Prepared by Yaswanth Narikamalli Assistant Professor Kristu Jayanti College Bangalore Course code:23BCL2T331
  • 7. Types of Inheritance 1. Single Inheritance 2. Multilevel Inheritance 3. Hierarchical Inheritance 4. Multiple Inheritance 5. Hybrid Inheritance
  • 8. Single inheritance A sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance.
  • 9. // concept of single inheritance import java.io.*; import java.lang.*; import java.util.*; class One { public void print_class() { System.out.println("BCA cloud computing"); } } class Two extends One { public void print_sec(){ System.out.println("Section B"); } } public class Main { public static void main(String[] args) { Two g = new Two(); g.print_class(); g.print_sec(); } }
  • 10. Multilevel Inheritance A derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes.
  • 11. //multilevel inheritance import java.io.*; import java.lang.*; import java.util.*; class One { public void print_class() { System.out.println("BCA cloud computing"); } } class Two extends One { public void print_sec(){ System.out.println(" Section B"); } } class Three extends Two{ public void print_CA(){ System.out.println(" Class Animator"); } } public class Main { public static void main(String[] args) { Three g = new Three(); g.print_class(); g.print_sec(); g.print_CA(); } }
  • 12. Hierarchical Inheritance In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass.
  • 13. // Java program to illustrate the hierarchical inheritance class A { public void print_A() { System.out.println("Class A"); }} class B extends A { public void print_B() { System.out.println("Class B"); }} class C extends A { public void print_C() { System.out.println("Class C"); }} class D extends A { public void print_D() { System.out.println("Class D"); } } public class Test { public static void main(String[] args) { B objB = new B(); objB.print_A(); objB.print_B(); C objC = new C(); objC.print_A(); objC.print_C(); D objD = new D(); objD.print_A(); objD.print_D(); } }
  • 14. Multiple inheritances In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces.
  • 15. Hybrid Inheritance It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple inheritances with classes, hybrid inheritance involving multiple inheritance is also not possible with classes. In Java, we can achieve hybrid inheritance only through Interfaces
  • 16. Advantages of Inheritance Code reusability Abstraction Class Hierarchy Polymorphism
  • 18. Overriding Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
  • 19. // Java program to demonstrate // method overriding in java // Base Class class Parent { void show() { System.out.println("Parent's show"); } } // Inherited class class Child extends Parent { // This method overrides show() of Parent @Override void show() { System.out.println("Child's show"); } } // Driver class class Main { public static void main(String[] args) { Parent obj1 = new Parent(); obj1.show(); Parent obj2 = new Child(); obj2.show(); } }
  • 20. Abstraction Abstraction in Java refers to hiding the implementation details of a code and exposing only the necessary information to the user. It provides the ability to simplify complex systems by ignoring irrelevant details and reducing complexity. Abstraction allows developers to represent complex systems by simplifying them into their essential characteristics while hiding unnecessary implementation
  • 22. Abstract class example 1. abstract class Bike{ 2. abstract void run(); 3. } 4. class Honda extends Bike{ 5. void run(){System.out.println("running safely");} 6. public static void main(String args[]){ 7. Honda obj = new Honda(); 8. obj.run(); 9. } 10. }
  • 23. Super Keyword The super keyword in Java is a reference variable which is used to refer immediate parent class object. 1. super can be used to refer immediate parent class instance variable. 2. super can be used to invoke immediate parent class method. 3. super() can be used to invoke immediate parent class constructor.
  • 25. class Animal{ String color="white"; } class Dog extends Animal{ String color="black"; void printColor(){ System.out.println(color);//prints color of Dog class System.out.println(super.color);//prints color of Animal class } } class Test2{ public static void main(String args[]){ Dog d=new Dog(); d.printColor(); }}
  • 26. class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void eat(){System.out.println("eating bread...");} void bark(){System.out.println("barking...");} void work(){ super.eat(); bark(); } } class Test3{ public static void main(String args[]){ Dog d=new Dog(); d.work(); }}
  • 27. class Animal{ Animal(){System.out.println("animal is created");} } class Dog extends Animal{ Dog(){ super(); System.out.println("dog is created"); } } class Test4{ public static void main(String args[]){ Dog d=new Dog(); }}
  • 28. Interface An interface in Java is a blueprint of a class. It has static constants and abstract methods. ○ It is used to achieve abstraction. ○ By interface, we can support the functionality of multiple inheritance. ○ It can be used to achieve loose coupling.
  • 29. Syntax 1. interface <interface_name>{ 2. 3. // declare constant fields 4. // declare methods that abstract 5. // by default. 6. }
  • 30. The relationship between classes and interfaces
  • 31. 1. interface printable{ 2. void print(); 3. } 4. class A6 implements printable{ 5. public void print(){System.out.println("Hello");} 6. 7. public static void main(String args[]){ 8. A6 obj = new A6(); 9. obj.print(); 10. } 11.}
  • 32. How to extend Interface An interface extends another interface like a class implements an interface in interface inheritance. When one interface inherits from another, the sub-interface inherits all of the methods and constants declared by the super- interface. It can declare new abstract procedures and constants. To extend an interface, use the extends keyword
  • 33. interface A { void funcA(); } interface B extends A { void funcB(); } class C implements B { public void funcA() { System.out.println("This is funcA"); } public void funcB() { System.out.println("This is funcB"); } } public class Demo5 { public static void main(String args[]) { C obj = new C(); obj.funcA(); obj.funcB(); } }
  • 34. Packages Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are used for: ● Preventing naming conflicts. ● Making searching/locating and usage of classes easier. ● Providing controlled access ● Packages can be considered as data encapsulation
  • 35. Built-in Packages 1. Java.lang 2. java.io 3. java.util 4. java.applet 5. java.awt 6. java.net
  • 36. User Defined Packages User-defined packages: These are the packages that are defined by the user. First we create a directory myPackage Then create the MyClass inside the directory with the first statement being the package names.