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
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.
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.

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 themechanism 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: Classis 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 UseInheritance 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 Preparedby 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-classis 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 ofsingle 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 derivedclass 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.*; importjava.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 HierarchicalInheritance, one class serves as a superclass (base class) for more than one subclass.
  • 13.
    // Java programto 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 Multipleinheritances, 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 isa 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 Codereusability Abstraction Class Hierarchy Polymorphism
  • 17.
  • 18.
    Overriding Overriding is afeature 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 programto 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 Javarefers 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 superkeyword 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"; } classDog 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...");} } classDog 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 iscreated");} } 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 inJava 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 betweenclasses 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 extendInterface 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 { voidfuncA(); } 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 Javais 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-definedpackages: 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.