EContent 11 2025 04 17 11 57 27 Unit 4 Inheritance Interface and Packagespptx 2025 04 09 13 09 51
EContent 11 2025 04 17 11 57 27 Unit 4 Inheritance Interface and Packagespptx 2025 04 09 13 09 51
Prepared By
Prof. Twinkle Bhimani
Assistant Professor, CE Dept.
Contents
• Inheritance basics
• Super keyword
• Multilevel hierarchy
• Overriding methods
• Dynamic method dispatch
• Abstract class
• Using final with inheritance
• Object class
• Interfaces
• Packages: defining and importing
• Access protection
Inheritance basics
• It is defined as the process where derived class can borrow the
properties of base class.
• Inheritance can be achieved by using the “extends” keyword.
Advantages:
i. Code reusability
ii. Extensibility
iii. Data hiding
iv. Overriding
Inheritance basics
• Inheritance represents the IS-A relationship which is also known
as a parent-child relationship.
Example:
Hierarchical
Single
Multilevel
Inheritance Types
Multiple
Hybrid
Single Inheritance
class multilevelinh{
public static void main(String args[]) {
c C1 = new c();
C1.disp2();
C1.disp1();
C1.disp();
}}
Hierarchical Inheritance
class hierarInh{
public static void main(String args[]) {
c C1 = new c();
C1.disp2();
C1.disp1();// Question?
C1.disp();
}}
Multiple Inheritance
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?
} }
Compile time error occurs
Interface
Syntax:
modifier interface InterfaceName {
class Test
{ public static void main(String[] args) {
Car small = new Car();
small.display();
}}
Using Super Keyword
• Discussed in unit 3
Dynamic Method
Dispatch
• Discussed in unit 3
Abstraction
Hiding the internal implementation of the feature and only showing the
functionality to the users. i.e. what it works (showing), how it works
(hiding). Both abstract class and interface are used for abstraction.
Rules:
1. Abstract method must present in abstract class only.
2. Cannot be instantiated i.e not allowed to create object.
3. Method must be overridden.
4. It can have constructors & final and static methods
Abstract Class
Inheritance/ A Class can implement multiple The class can inherit only
Implementation interfaces one Abstract Class
class Encapsulate {
private String Name;
public String getName()
{ return Name; }
public void setName(String newName)
{
Name = newName;
}
public class Main{
public static void main(String[] args) {
Encapsulate obj = new Encapsulate();
obj.setName("Harsh");
System.out.println("name: " + obj.getName());
//System.out.println("name: " + obj.Name);
}}
Using Final with
Inheritance
• The final keyword in java is used to restrict the user.
• The java final keyword can be used in many context. Final
can be:
• Variable
• Method
• Class
Using Final with
Inheritance
• Java final variable
• If you make any variable as final, you cannot change the
value of final variable(It will be constant).
class A{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Aobj=new A();
obj.run();
} Output: Compile Time Error
Using Final with
Inheritance
• Java final method
• If you make any method as final, you cannot override it.
class A{
final void run(){System.out.println(“From Class A");}
}
class B extends A{
void run(){System.out.println(“From Class B");}
class H extends B{
void run(){System.out.println(“From class H");}
class Object { }
public class Main {
public static void main(String[] args)
{
String obj = new String("Hi");
Class c = obj.getClass();
System.out.println("Class of Object obj is: "+ c.getName());
}
}
Object Class
class Main{
int rollno;
String name;
• Advantages:
i. Code reused – from other package
ii. Same name – two classes from two different packages
iii. Possible to hide the classes
iv. Name of the directory becomes the package name.
• Two types:
i. Built-in packages
• Ex: java.lang, java.util, java.io, java.awt, java.applet
ii. User defined packages
Packages: Defining
and Importing
• Syntax to create package:
• package package_name;
• package abc;
STEP 2: Come out from the folder p1 and save the below code as “test”.
import p1.*;
public class test
{
public static void main(String args[])
{
testpackage tp = new testpackage();
tp.display();
}
}
Now compile and run the “test” file
Access Protection
• Inheritance basics
• Super keyword
• Multilevel hierarchy
• Overriding methods
• Dynamic method dispatch
• Abstract class
• Using final with inheritance
• Object class
• Interfaces
• Packages: defining and importing
• Access protection
Next