Encapsulation Inheritance and Polymorphism., Slides of Java Programming

Java Encapsulation Inheritance and Polymorphism.

Typology: Slides

2022/2023

Uploaded on 06/20/2023

emy-alinsod
emy-alinsod 🇵🇭

2 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OBJECT-
ORIENTED
PROGRAMMING
IN JAVA
E.G. Alinsod
Inheritance.
Encapsulation.
Polymorphism.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Encapsulation Inheritance and Polymorphism. and more Slides Java Programming in PDF only on Docsity!

OBJECT-

ORIENTED

PROGRAMMING

IN JAVA

E.G. Alinsod

Inheritance.

Encapsulation.

Polymorphism.

Inheritance 02

Inheritance enables the acquisition

of data members and properties from

one class to another.

Inheritance is a mechanism by which

one class can acquire/inherit the

features(fields and methods) of

another class.

Inheritance 04

Inheritance 05

Base Class (Parent Class)

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.

Subclass (Child 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?

Run-Time Polymorphism

Runtime is a method call in the execution process that overrides a different method in the run time.

Code Reusability

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