0% found this document useful (0 votes)
14 views31 pages

Week 7 - L19-L21

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

Week 7 - L19-L21

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Subject Name: OOP

Unit No:4
Unit Name: Inheritance and Packages

Faculty Name : Mr. Dayanand Dhongade


Index

Lecture 19 – Importance of Inheritance, Different Types of Inheritance 3

Lecture 20 – Super keyword 18

Lecture 21 – Abstract class and Abstract method 27

2
Module No 4: Inheritance and Packages

Lecture No: 19
Importance of Inheritance, Different Types of
Inheritance
Importance of Inheritance

• Inheritance in Java is a mechanism in which one object acquires all the


properties and behaviors of a parent object.
• The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes.
• When you inherit from an existing class, you can reuse methods and fields
of the parent class.
• Moreover, you can add new methods and fields in your current class also

Why to use inheritance in java


• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.

4
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Defining a Subclass

• Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
• The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to increase the
functionality.
• Extends keyword signifies that properties of the super class are extended to
sub class
• Sub class will not inherit private members of super class

6 Lecture 19-Importance of Inheritance, Different Types of Inheritance


Program 1

class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output
Programmer salary is:40000.0
Bonus of programmer is:10000

7
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Inheritance and it’s Types

• A class can inherit the properties of another class and add its own
modification.
• The class whose properties are extended is known as super or base or
parent class.
• The class which extends the properties of super class is known as sub or
derived or child class
• To declare inherited classes ‘extends’ keyword is used

• class B extends A { ….. } A

• A is super class
• B is sub class
B

8 Lecture 19-Importance of Inheritance, Different Types of Inheritance


Types of Inheritance

Single Inheritance Hierarchical Inheritance

A X

B A B C

MultiLevel Inheritance

9
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 2:Single Inheritance

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
OUTPUT
barking...
eating...

10
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Single Inheritance)

• When a class inherits another class, it is known as a single inheritance. In


the example given below, Dog class inherits the Animal class, so there is
the single inheritance.

11
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 3: Multilevel Inheritance

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
OUTPUT:
weeping...
barking...
eating...

12
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Multilevel Inheritance)

• When there is a chain of inheritance, it is known as multilevel inheritance.


As you can see in the example given below, BabyDog class inherits the
Dog class which again inherits the Animal class, so there is a multilevel
inheritance.

13
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 4: Hierarchical Inheritance

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}}
OUTPUT
meowing...
eating...

14
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Hierarchical Inheritance)

• When two or more classes inherits a single class, it is known


as hierarchical inheritance. In the example given below, Dog and Cat
classes inherits the Animal class, so there is hierarchical inheritance.

15
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 4

class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were

public static void main(String args[]){


C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
OUTPUT
Compile Time Error

16
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Multiple Inheritance)

• Multiple inheritance is not supported in java.

• Consider a scenario where A, B, and C are three classes. The C class


inherits A and B classes. If A and B classes have the same method and
you call it from child class object, there will be ambiguity to call the method
of A or B class.

• Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have same
method or different, there will be compile time error.

17
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Module No 4: Inheritance and Packages

Lecture No: 20
Super keyword
Using super

super keyword is used :


1.To call the superclass' constructor.
2.To access a member of the superclass that has been hidden by a
member of a subclass.

first use of super


number of parameters in subclass constructor is equal to sum of
number of data members in super class and its own, if all are
distinct.

super must be a very first statement in subclass constructor.

19 Lecture 20- Super keyword


Super Keyword

• The super keyword in Java is a reference variable which is used to


refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.

Usage of Java super Keyword


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.

20
Lecture 20- Super keyword
1.super can be used to refer immediate parent class instance variable.

• We can use super keyword to access the data member or field of


parent class. It is used if parent class and child class have same
fields.

• In the given example, Animal and Dog both classes have a


common property color. If we print color property, it will print the
color of current class by default. To access the parent property, we
need to use super keyword.

21
Lecture 20- Super keyword
1.super can be used to refer immediate parent class instance variable.

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 TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
OUTPUT:
black
white

22
Lecture 20- Super keyword
2. Super can be used to invoke parent class method

• The super keyword can also be used to invoke parent class


method. It should be used if subclass contains the same method
as parent class. In other words, it is used if method is overridden.

• In the given example Animal and Dog both classes have eat()
method if we call eat() method from Dog class, it will call the eat()
method of Dog class by default because priority is given to local.

• To call the parent class method, we need to use super keyword.

23
Lecture 20- Super keyword
2. Super can be used to invoke parent class method

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 TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
OUTPUT
eating...
barking...

24
Lecture 20- Super keyword
3. super() can be used to invoke immediate parent class constructor.

• super() is added in each class constructor automatically by


compiler if there is no super()

• As we know well that default constructor is provided by compiler


automatically if there is no constructor. But, it also adds super() as
the first statement.

25
Lecture 20- Super keyword
3. super() can be used to invoke immediate parent class constructor.

class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
OUTPUT
animal is created
dog is created

26
Lecture 20- Super keyword
Module No 4: Inheritance and Packages

Lecture No: 21
Abstract class and Abstract method
Abstract Class in Java

• An abstract class must be declared with an abstract keyword.


• It can have abstract and non-abstract methods.
• It needs to be extended and its method implemented. It cannot be
instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change
the body of the method.

public abstract class Product


{
// contents
}

28 Lecture 21- Abstract class and Abstract method


Abstract Method in Java

• A method which is declared as abstract and does not have


implementation is known as an abstract method.

abstract void printStatus(); //no method body and abstract

29
Lecture 21- Abstract class and Abstract method
Example 1

abstract class Bike{


abstract void run();
}

class Honda4 extends Bike{


void run(){System.out.println("running safely");}

public static void main(String args[]){


Bike obj = new Honda4();
obj.run();
}
}

OUTPUT
running safely

30
Lecture 21- Abstract class and Abstract method
Example 2

abstract class Shape{


abstract void draw(); • In this example, Shape is the
} abstract class, and its
implementation is provided by
class Rectangle extends Shape{ the Rectangle and Circle class
void draw(){System.out.println("drawing rectangle");}
}
• In this example, if you create
class Circle1 extends Shape{ the instance of Rectangle
void draw(){System.out.println("drawing circle");} class, draw() method of
} Rectangle class will be
invoked.
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
s.draw();
}
}

OUTPUT
drawing circle

31
Lecture 21- Abstract class and Abstract method
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy