

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Java Encapsulation Inheritance and Polymorphism.
Typology: Slides
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































E.G. Alinsod
Inheritance 02
Inheritance 04
Inheritance 05
Provides the data members and methods in alternative words. Any base class that needs methods or data members will have to borrow them from their respective parent or base class.
The implementation of its parent class recreates a new class, which is the child class. To inherit the parent class, a child class must include a keyword called "extends."
Inheritance 07 Why Do We Need Inheritance?
Runtime is a method call in the execution process that overrides a different method in the run time.
The process of inheritance involves reusing the methods and data members defined in the parent class.Eliminates the need to write the same code in the child class—saving time as a result.
Inheritance 08 package inheritance; class Machine { protected String brand = "Xtreme"; public void wash() { System.out.println("Initiating Gentle-Wash Task"); } } public class WashingMachine extends Machine { private String modelName = "Top Load Washing Machine"; public static void main(String[] args) { WashingMachine washmachine = new WashingMachine(); washmachine.wash(); System.out.println(washmachine.brand + " - " + washmachine.modelName); } }
Types of Inheritance Multi-Level Inheritance package inheritance; class Student { void Play() { System.out.println("Playing Fooball..."); } } class Bob extends Student { void Study() { System.out.println("Studing Physic..."); } } public class Single { public static void main(String args[]) { Bob d = new Bob(); d.Study(); d.Play(); } } 10
Types of Inheritance Hierarchical Inheritance 11
Multiple Inheritance Types of Inheritance (^13)
14 HAS-A RELATIONSHIP IS-A RELATIONSHIP
Disadvantages of Inheritance The inherited methods lag in performance Some of the data members of the parent class may not be of any use—as a result, they waste memory Inheritance causes strong coupling between parent and child classes. This means, either of the two (Parent class or Child class) become incapable of being used independent of each other. 16
Inheritance 17
super Keyword in Java 17 public class Person { int age = 50; } public class Employee extends Person { int age = 30; void insertStudentAge() { int age = 20; // Here, we have two ways to call instance variable 'age' of the person. // 1st way: Person p = new Person(); System.out.println(p.age); // 50 } // 2nd way: System.out.println(super.age); // 50 // Calling Local variable. System.out.println(age); // Calling instance variable of //the same class. System.out.println(this.age); // 30 }
Use of super keyword in Java To refer immediate parent class’s instance variable. To call immediate parent class’s constructor. To invoke the immediate superclass method. To call the superclass instance variable or method, we have two options. 17