0% found this document useful (0 votes)
9 views23 pages

Ssca2022: Core Java: Class and Inheritance Module-5

Inheritance in Java allows one class (child) to inherit properties and behaviors from another class (parent), promoting code reusability and extensibility. Key concepts include the use of super keyword, method overriding for polymorphism, and the distinction between different types of inheritance such as single, multilevel, and hierarchical. Constructors are not inherited but can be invoked using super(), ensuring proper initialization of parent class members before child class members.

Uploaded by

jkns5kjyvd
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)
9 views23 pages

Ssca2022: Core Java: Class and Inheritance Module-5

Inheritance in Java allows one class (child) to inherit properties and behaviors from another class (parent), promoting code reusability and extensibility. Key concepts include the use of super keyword, method overriding for polymorphism, and the distinction between different types of inheritance such as single, multilevel, and hierarchical. Constructors are not inherited but can be invoked using super(), ensuring proper initialization of parent class members before child class members.

Uploaded by

jkns5kjyvd
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

SSCA2022: CORE JAVA

Module-5 Class and Inheritance


WHAT IS 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 : new classes that are
built upon existing classes.
 The Base Class is also known as Parent Class and
the derived class also known as Child Class.
 Inheritance represents the IS-A relationship
which is also known as a parent-child relationship.
TERMS USED IN INHERITANCE
 Class: A class is a group of  Super Class/Parent Class:
objects which have common Superclass is the class from
properties. It is a template or where a subclass inherits the
blueprint from which objects features. It is also called a base
are created. class or a parent class.
 Sub Class/Child Class:  Reusability: As the name
Subclass is a class which inherits specifies, reusability is a
the other class. It is also called mechanism which facilitates you
a derived class, extended class, to reuse the fields and methods
or child class. of the existing class when you
create a new class.
USE OF INHERITANCE
 Code Reusability – Common properties & methods of parent class can
be reused in child class.
 Extensibility – New features can be added to existing classes without
modifying them.
 Method Overriding – Enables runtime polymorphism (dynamic method
dispatch).
 Hierarchical Representation – Helps represent real-world
relationships (e.g., Student is a Person).
 Maintainability – Reduces redundancy and makes code easier to
maintain.
BENEFITS OF INHERITANCE
 Saves Development Time – Avoids rewriting common code.
 Improves Readability – Clear structure of base & derived classes.
 Encourages Modularity – Each class handles its own specific
responsibility.
 Supports Polymorphism – Enhances flexibility by allowing multiple
implementations.
 Scalability – Easy to extend system with minimal changes.
TYPES OF INHERITANCE
Note:
On the basis of class, there
can be three types of
inheritance in java:

1. Single level Inheritance.


2. Multilevel Inheritance.
3. Hierarchical Inheritance.

In java programming, multiple


and hybrid inheritance is
supported through interface
only.
SYNTAX OF INHERITANCE
Example of Inheritance
class Person { void display() {
String name; // calling parent’s method
int age; [Link](("Person: " + name + ", Age: " + age+ “Salary: " +
salary);
Person(String name, int age) {
}
[Link] = name;
[Link] = age; }
public class InheritanceDemo {
}
public static void main(String[] args) {
void display() {
[Link]("Person: " + name + ", Age: " + age); Employee emp = new Employee("Rahul Sharma", 30,
75000);
} }
[Link]();
class Employee extends Person {
}
double salary;
}
Employee(String Name, int Age, double salary) {
[Link] = name;
[Link] = age;
[Link] = salary;
}
Super keyword & its use
 Access Parent Class Data Members: Used when child class has a variable with the same
name as the parent class.
class Person {
String name = "Parent Name";
}
class Employee extends Person {
String name = "Child Name"; //hides parent class variable “name”
void show() {
[Link]([Link]); // Parent variable
[Link](name); // Child variable
}
}
Super keyword & its use
 Access Parent Class Methods: Used to call the parent class method if it is overridden in child.
class Person {
void display() {
[Link]("Person details");
}
}
class Employee extends Person {
@Override
void display() {
[Link](); // calls parent method
[Link]("Employee details");
}
}
Super keyword & its use
 Invoke Parent Class Constructor:super() is used inside child constructor to call parent constructor.
 Must be the first statement in child constructor.
class Person {
String name;
Person(String name) {
[Link]=name;
}
}
class Employee extends Person {
Employee(String name) {
super(name); // calls parent constructor
[Link]("Employee created");
} }
Inheriting Data Members
 Data members (variables/fields) of the parent class are inherited by the
child class.
 They can be accessed directly in the child class (if not private).
 public and protected data members → inherited by child.
 default (package-private) → inherited only if child is in the same package.
 private → not inherited directly (can only be accessed through
public/protected getters/setters of parent).
 If the child defines a variable with the same name, then the child’s variable
hides the parent’s variable (not overridden).
 Parent’s hidden variable can be accessed using [Link].
Inheriting Methods
 The child class can Use them directly (without rewriting).
 public and protected methods → inherited by child.
 default (package-private) → inherited only within the same package.
 private methods → not inherited.
 Methods can be overridden (same signature in child).
 Parent’s version can still be invoked using [Link]().
Role of Constructors in Inheritance
 Not Inherited → Constructors are not inherited by child classes.
 Automatic Call → When a child object is created, parent constructor is
invoked first, then child’s.
 Default Constructor Rule → If parent has no constructor defined, Java
provides a default no-arg constructor which runs automatically.
 Using super() → Child constructor can explicitly call a specific parent
constructor using super(arguments) (must be first statement).
 Initialization → Ensures parent class members are initialized before
child class members.
Role of Constructors in Inheritance
class Person { public class Test {
Person() { public static void main(String[] args) {
[Link]("Person Constructor"); Employee emp = new Employee();
} }
} }

class Employee extends Person { Output:


Employee() { Person Constructor
super(); // calls parent constructor Employee Constructor
[Link]("Employee
Constructor");
}
}
Polymorphism in Inheritance
 Polymorphism = “many forms” → the ability of an object to take
different forms.
 In Java inheritance, polymorphism mainly refers to method overriding +
dynamic method dispatch.
 A parent class reference can refer to a child class object, and at runtime
the child’s method is executed.
 Types of Polymorphism in Java
 Compile-time (Static) Polymorphism → Achieved by method
overloading.
 Runtime (Dynamic) Polymorphism → Achieved by method overriding in
inheritance.
Overriding Superclass Methods
 Method Overriding happens when a subclass provides its own
implementation of a method that already exists in its superclass with
the same signature (name, parameters, and return type).
 It is used to achieve runtime polymorphism (dynamic method dispatch).
Overriding Superclass Methods: Example
class Person { public class TestOverride {
void displayInfo() { public static void main(String[] args) {
[Link]("Person details"); Person p = new Employee(); // upcasting
}
[Link](); // runtime: calls Employee's
} version
class Employee extends Person { p. showBoth(); // Error: No candidates found for
@Override method call [Link]().
void displayInfo() { Employee e = new Employee();
[Link]("Employee details"); [Link]();
}
}
void showBoth() {
}
[Link](); // calls parent version
displayInfo(); // calls child version
}
}
Overriding Superclass Methods: Rules
 Method must have the same name, parameters, and return type (or covariant type).
 Access level cannot be more restrictive than in the superclass i.e.
 You can keep the same access level as the superclass method.
 You can make it more accessible (wider scope).
 But you cannot make it more restrictive (narrower scope).
 Example: Suppose someone has a Person reference (public method accessible everywhere).
 If child (Employee) overrides it with protected, then callers outside the package won’t be able to
use it → breaks polymorphism.
 To maintain consistent accessibility, Java doesn’t allow reducing visibility.
 Only inherited methods can be overridden (not static, not final, not private).
 The overriding method may use the @Override annotation (recommended).
 You can still call the parent’s version using [Link]().
Type Compatibility
 In Java, a superclass reference can point to a subclass object → this is
called upcasting.
 This is possible because a subclass object “is-a” superclass object.

 Example:

Person p = new Employee(); // Employee IS-A Person


 Allowed automatically (implicit).
 But using p, you can only access methods/variables defined in Person (unless
overridden).
Conversion Types
Upcasting (Implicit Conversion)
 Subclass → Superclass.

 Always safe.

 Done automatically.

 Example:

Employee e = new Employee();


Person p = e; // Upcasting
Conversion Types
Downcasting (Explicit Conversion)
 Superclass → Subclass.

 Requires explicit type cast.

 Works only if the object actually belongs to the subclass, else →


ClassCastException.
 Example:

Person p = new Employee(); // upcast


Employee e = (Employee) p; // downcast, safe

Person p2 = new Person();


Employee e2 = (Employee) p2; // Runtime error

You might also like