Java Object-Oriented Programming Concepts
Java Object-Oriented Programming Concepts
of ISE
ARUN K H
Assistant Professor,
Department of ISE,
Appian Certified Associate Developer,
Dept. of ISE Appian Authorized Instructor,
Acharya Institute of Technology,
18+ Years of Experience
Syllabus Overview
Module–3
● Method Overriding
● Dynamic Method Dispatch
● Using Abstract Classes
● Using final with Inheritance
● Local Variable Type Inference and
Inheritance
● The Object Class.
Syllabus Overview
Text Books
● Java: The Complete Reference, Twelfth Edition, by Herbert Schildt, November 2021,
McGraw-Hill, ISBN: 9781260463422
Reference Books
Dept. of ISE
● Programming with Java, 6th Edition, by E Balagurusamy, Mar-2019, McGraw Hill
Education, ISBN: 9789353162337.
● Thinking in Java, Fourth Edition, by Bruce Eckel, Prentice Hall, 2006
([Link]
Module 3
Dept. of ISE
Chapter 8
Inheritance Basics
● Inheritance is one of the core concepts of object-oriented programming
(OOP) in Java.
● It allows a new class (subclass) to inherit properties and behavior (fields
and methods) from an existing class (superclass), promoting code reuse,
extensibility, and maintainability.
Dept. of ISE ● Inheritance forms an "is-a" relationship between classes, where the child
class is a type of the parent class.
Key Points
● The class that is inherited from is called the superclass or parent class or
base class.
● The class that inherits is called the subclass or child class or derived class.
● The extends keyword is used to inherit a class.
Example
class Animal { public class Main {
void eat() { public static void main(String[] args) {
[Link]("This animal eats food."); Animal a=new Animal();
} [Link]();
} Dog d = new Dog();
// Dog class inherits Animal class [Link](); // Inherited from Animal class
Dept. of ISE class Dog extends Animal { [Link](); // Method of Dog class
void bark() { }
[Link]("The dog barks."); }
}
} Output
This animal eats food.
This animal eats food.
The dog barks.
Method Overriding in Java
● Method overriding is a feature in object-oriented programming that allows a
subclass to provide a specific implementation for a method that is already
defined in its superclass.
● When a method is overridden, the method in the subclass has the same signature
(name, return type, and parameters) as the method in the superclass, but the
implementation in the subclass is different.
Dept. of ISE
Key Features of Method Overriding:
● Same Method Signature:
○ The method in the subclass must have the same name, return type (or a
subtype), and parameter list as the method in the superclass.
● Inheritance:
○ Method overriding can only occur when a subclass inherits a method from a
superclass.
Method Overriding in Java
● Run-time Polymorphism:
○ Method overriding is a key part of achieving runtime polymorphism in Java.
○ This allows Java to determine at runtime which method to call, based on the
object that is referenced.
● Access Modifier Rules:
○ The overridden method in the subclass must not have a more restrictive
Dept. of ISE
access modifier than the method in the superclass.
○ For example, if the superclass method is public, the overriding method must
also be public or more accessible.
● Use of super:
○ The super keyword can be used within the subclass to call the superclass's
version of the overridden method.
Example
/class Animal { public class Main {
// Method to be overridden public static void main(String[] args) {
public void sound() {
Animal myAnimal = new Animal();
[Link]("Animal makes a sound");
Dog myDog = new Dog();
}
}
Cat myCat = new Cat();
class Dog extends Animal {
// Overriding the sound() method in Animal class [Link]();
public void sound() { [Link]();
Dept. of ISE [Link]("Dog barks"); [Link]();
}
}
}
}
class Cat extends Animal {
// Overriding the sound() method in Animal class
public void sound() { Output
[Link]("Cat meows"); Animal makes a sound
Dog barks
}
Cat meows
}
Dynamic Method Dispatch in java
● Dynamic Method Dispatch, also known as runtime polymorphism or late
binding, is a mechanism in Java that allows method calls to be resolved
at runtime rather than compile-time.
● This feature is achieved by using a superclass reference to refer to a
subclass object, enabling the Java Virtual Machine (JVM) to decide
Dept. of ISE which method implementation (from the superclass or subclass) to
invoke based on the actual object type.
Key Concepts of Dynamic Method Dispatch
Method Overriding:
● Dynamic method dispatch relies on method overriding, where a subclass
provides a specific implementation of a method that is already defined in
its superclass.
Dynamic Method Dispatch in java
Superclass Reference and Subclass Object:
● In Java, a reference variable of a superclass type can hold a reference to
an object of any subclass of that superclass.
Runtime Decision:
● At runtime, Java uses the actual type of the object (not the type of the
Dept. of ISE reference) to determine which overridden method to call. This is the
essence of dynamic method dispatch.
Example for Dynamic Method Dispatch in java
class Animal {
void sound() { public class Main {
[Link]("This is a generic animal public static void main(String[] args) {
sound."); // Superclass reference pointing to a subclass object
} Animal a;
}
class Dog extends Animal { // Dynamic dispatch to Dog's sound method
void sound() { a = new Dog();
[Link]("Dog barks."); [Link](); // Output: Dog barks.
Dept. of ISE
}
} // Dynamic dispatch to Cat's sound method
class Cat extends Animal { a = new Cat();
void sound() { [Link](); // Output: Cat meows.
[Link]("Cat meows."); }
} }
}
Output
Dog barks.
Cat meows.
Inheritance Types
● Java supports several types of inheritance, though with some restrictions like
the absence of multiple inheritance using classes.
● Types of inheritance
○ Single Inheritance
○ Multilevel Inheritance
Dept. of ISE
Hierarchical Inheritance
Single Inheritance
● In single inheritance, a class (subclass) inherits from one superclass.
● This is the most basic form of inheritance and establishes a simple
parent-child relationship between two classes.
Key Points:
● The subclass inherits fields and methods from the superclass.
Dept. of ISE ● Java allows a subclass to override methods from the superclass to
provide specific functionality.
Single Inheritance Example
class Animal { public class Main {
void eat() { public static void main(String[] args) {
[Link]("This animal eats food."); Dog d = new Dog();
} [Link](); // Inherited method
} [Link](); // Dog's own method
// Dog inherits from Animal }
Dept. of ISE
Example for Explicit super() Call:
class SuperClass {
SuperClass(int x) {
[Link]("Superclass constructor with value: " + x);
}
}
class SubClass extends SuperClass {
SubClass(int y) {
super(y); // Explicit call to superclass constructor
[Link]("Subclass constructor with value: " + y);
Dept. of ISE }
}
public class Main {
public static void main(String[] args) {
SubClass sub = new SubClass(5);
}
}
Output:
Superclass constructor with value: 5
Subclass constructor with value: 5
Using super in Java
● The super keyword refers to the immediate parent class of an object. It is
commonly used in two scenarios:
○ Accessing Parent Class Members: When there is a conflict between
the parent and child class variables or methods (for example, if both
have variables with the same name).
Dept. of ISE ○ Calling Parent Class Constructor: A child class constructor can
invoke the parent class constructor using super().
Key Points:
● super can be used to refer to the immediate parent class's methods and
variables.
● super() is used to call the constructor of the parent class.
● super() must be the first statement in a subclass constructor.
Example
class Animal { public class Main {
String color = "Brown"; public static void main(String[] args) {
} Dog d = new Dog();
class Dog extends Animal { [Link]();
String color = "Black"; }
// same name as parent variable
}
void printColor() {
Dept. of ISE
[Link]("Dog's color: " + color);
// refers to Dog's color
[Link]("Animal's color: " + Output
[Link]);
Dog's color: Black
Animal's color: Brown
// refers to Animal's color using super
}
}
Here, [Link] is used to differentiate between the color field in the Dog and Animal classes.
Example of super() to Call Parent Class Constructor:
class Animal { public class Main {
Animal() { public static void main(String[] args) {
[Link]("Animal constructor is Dog d = new Dog();
called."); }
} }
}
Dept. of ISE
class Dog extends Animal {
Dog() {
super(); // calls the constructor of Animal Output
[Link]("Dog constructor is called."); Animal constructor is called.
Dog constructor is called.
}
}
final Keyword
● The final keyword in Java is a modifier used to apply restrictions on
variables, methods, and classes. It serves different purposes depending on
how it is used.
● The final keyword ensures that certain elements cannot be modified or
Dept. of ISE
overridden once they are defined.
There are three main uses of the final keyword:
● Final Variables: A constant value that cannot be changed.
● Final Methods: A method that cannot be overridden by subclasses.
● Final Classes: A class that cannot be subclassed (extended).
final Variables
● When a variable is declared as final, its value cannot be modified after it has
been initialized.
● Essentially, this makes it a constant.
● A final variable can be initialized only once, either at the time of declaration or
in the constructor.
Characteristics:
Dept. of ISE
● Once assigned, a final variable cannot be reassigned.
● Final variables are often used for constants.
● If the final variable is not initialized at the time of declaration, it is called a blank
final variable and must be initialized in the constructor.
final varibale example
class TestFinalVariable {
final int maxSpeed = 120; // final variable initialized at declaration
void display() {
maxSpeed = 150; // Error: cannot assign a value to final variable
[Link]("Max Speed: " + maxSpeed);
}
Dept. of ISE
Dept. of ISE
Implementing an Interface
● A class uses the implements keyword to implement an interface.
● The implementing class must provide concrete implementations for all
abstract methods in the interface.
● A class can implement multiple interfaces, allowing it to inherit behavior
from more than one source.
Dept. of ISE ● Example: