Unit - 006 - Inheritance and Polymorphism
Unit - 006 - Inheritance and Polymorphism
PRESENTED BY :
Mallikarjuna G D
Founder & Director
CONTENTS Unlimited Powerful Learning
INHERITANCE
ACCESS MODIFIERS
POLYMORPHISM
METHOD OVERLOADING
METHOD OVERRIDING
INTERFACE
OOPS Concepts Unlimited Powerful Learning
Inheritance:
• It is a mechanism in which one object acquires all the properties and behaviors of
parent object.
• Inheritance represents the IS-A relationship, also known as parent-child relationship.
Syntax:
class Subclass-name extends Superclass-name
Class Superclass-name {Proicere
{ //methods and fields
//methods and fields }
}
OOPS Concepts Unlimited Powerful Learning
e
Types of Inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
CL
CLASS B CLASS BB CLASS B
Class CS C
Multilevel
05/20/2024 Multilevel 4
OOPS Concepts Unlimited Powerful Learning
class Animal {
Void eat() {
System.out.println("eating..."); }
}
class Dog extends Animal {
Derived From Projectum
void bark() {
System.out.println("barking..."); }
}
class TestInheritance {
public static void main(String args[]) {
Dog d=new Dog();
d.bark(); Proicere
d.eat();
}} /* O/P - barking…
eating… */
OOPS Concepts Unlimited Powerful Learning
Encapsulation :
• Encapsulation in java is a process of wrapping data and the methods together into
a single unit.
• The Java Bean class is the example of fully encapsulated class.
Example:
Derived From Projectum
//save as Student.java
//save as Test.java
package com.pack;
package com.pack1;
public class Student {
class Test{
private String name;
public static void main(String[] args) {
public String getName() {
Student s=new Student();
Proicere
return name;
s.setName("vijay");
}
System.out.println(s.getName());
public void setName(String name) {
}
this.name=name
} // O/P - vijay
} }
OOPS Concepts Unlimited Powerful Learning
Access Modifiers:
• It specifies accessibility (scope) of a data member, method, constructor or class.
Polymorphism:
• It is a concept by which we can perform a single action by different ways.
Types of Polymorphism:
Derived From Projectum
Compile time polymorphism
Runtime polymorphism.
Proicere
OOPS Concepts Unlimited Powerful Learning
Method Overloading:
A class has multiple methods having same name but different in parameters. It
increases the readability of the program
Example:
class From
Derived Adder { Projectum
static int add(int a,int b) {
return a+b; }
static int add(int a,int b,int c) {
return a+b+c; }
}
class TestOverloading1 { Proicere
public static void main(String[] args) {
System.out.println(Adder.add(11,11)); // O/P – 22
System.out.println(Adder.add(11,11,11)); // O/P - 33
}}
OOPS Concepts Unlimited Powerful Learning
Method Overriding:
• Subclass provides the specific implementation of the method that has been
provided by one of its parent class.
Example:
Derived From Projectum
class Vehicle {
Void run() {
System.out.println("Vehicle is running"); }
}
class Bike2 extends Vehicle {
void run() {
Proiceresafely"); }
System.out.println("Bike is running
public static void main(String args[]) {
Vehicle obj = new Bike2();
obj.run();
} // O/P – Bike is running safely
OOPS Concepts Unlimited Powerful Learning
Abstraction:
• It is the process of hiding certain details and only show essential features of the
object.
• Abstraction in Java is achieved by using abstract class and interface.
Derived From Projectum
Interface:
Advantages of interface:
interface printable{
void print();
}
Derived From Projectum
class A1 implements printable {
public void print() {
System.out.println("Hello"); }
public static void main(String args[])
{
A1 obj = new A1(); Proicere
obj.print();
} } // O/P - Hello
Operations in Stacks Unlimited Powerful Learning
Proicere