0% found this document useful (0 votes)
2 views32 pages

Lec 08

The document covers key concepts of Object-Oriented Programming (OOP), focusing on inheritance (IS-A relationship) and aggregation (HAS-A relationship). It explains how inheritance allows a subclass to inherit properties and methods from a superclass, promoting code reusability and method overriding, while also discussing the limitations of multiple inheritance in Java. Additionally, it introduces aggregation as a means to achieve code reusability when there is no IS-A relationship, providing examples and tasks for practical understanding.

Uploaded by

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

Lec 08

The document covers key concepts of Object-Oriented Programming (OOP), focusing on inheritance (IS-A relationship) and aggregation (HAS-A relationship). It explains how inheritance allows a subclass to inherit properties and methods from a superclass, promoting code reusability and method overriding, while also discussing the limitations of multiple inheritance in Java. Additionally, it introduces aggregation as a means to achieve code reusability when there is no IS-A relationship, providing examples and tasks for practical understanding.

Uploaded by

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

Object Oriented

Programming
Topics to be covered today
 Inheritance (IS-A relationship)
 Aggregation (HAS-A relationship)
Inheritance
 One of the pillars of OPPs(Object Oriented
programming system).
 a mechanism in which one object acquires all the

properties and behaviors of 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 parent class, and you can add new
methods and fields also.
 Inheritance represents the IS-A relationship
known as parent-child relationship.
Why use inheritance in java
 For Method Overriding
◦ so runtime polymorphism can be achieved
 For Code Reusability.
Terms used in Inheritance
 Class
 Sub Class/Child Class

◦ Subclass is a class which inherits the other class. It is


also called a derived class, extended class, or child
class.
 Super Class/Parent Class:
◦ Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent
class.
 Reusability:
◦ a mechanism which facilitates to reuse the fields and
methods of the existing class when create a new class.
Syntax of Java Inheritance
Class super {
…………
…………
}
class Sub extends Super {
……......
.…….....
}
 extends is the keyword used to inherit the
properties of a class
◦ meaning of "extends" is to increase the functionality.
IS-A Relationship
Superclass

Subclass

 Programmer IS-A Employee


 instanceof Operator???
Example

 Save as Programmer.java
Example 2
InnerView
 when an object to My_Calculation class is
created, a copy of the contents of the superclass
is made within it. That is why, using the object of
the subclass you can access the members of a
superclass
Superclass reference variable
 The Superclass reference variable can hold the
subclass object but using that variable you can
access only the members of the superclass
 So, it is recommended to always create reference

variable to the subclass, to access members of


both classes.
Note:
 A subclass inherits all the members (fields,
methods, and nested classes) from its superclass.
 Constructors are not members, so they are not

inherited by subclasses

 The constructor of the superclass can be invoked


from the subclass using super keyword.
The super keyword
 similar to this keyword
 a reference variable which is used to refer

immediate parent class object

 Usage: super can be used to


◦ refer immediate parent class instance variable.
◦ invoke immediate parent class method.
◦ invoke immediate parent class constructor.
1) to refer immediate parent class
instance variable.
2) to invoke parent class method
3) to invoke parent class constructor
Note:
 super() is added in each class constructor
automatically by compiler if there is no super()
or this().

 default constructor is provided by compiler


automatically if there is no constructor. But, it also
adds super() as the first statement
Types of inheritance in java
 On the basis of class, there can be three types of
inheritance in java: single, multilevel and hierarchical.
Multiple Inheritance
 When a class extends multiple classes i.e. known
as multiple inheritance

 multiple and hybrid inheritance is supported


through interface only
Single Inheritance Example
Multilevel Inheritance Example
Hierarchical Inheritance Example
Task (Scenario)
 Suppose a record of uni department. There are students and
teachers.
 Students have properties: Roll number, name, emailId,
cellNumber, Semester
 Teachers have properties: Name, EmailID, cellNumber,
Designation, Salary

 Write a code using constructors, super keyword and


inheritance concepts to display the records of students and
teachers.
Q) Why multiple inheritance is not supported in
java?
Why multiple inheritance is not
supported in java?
 To reduce the complexity and simplify the
language, multiple inheritance is not supported in
java.
 Scenario:

◦ Suppose there are three classes A, B and C.


◦ The C class inherits A and B.
◦ If A and B classes have same method and you call it from
child class object, there will be ambiguity to call 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 whether methods are same or
not
Example:
Aggregation in Java
 HAS-A relationship
 If a class has an entity reference, it is known as

Aggregation

 Why use Aggregation?


◦ For Code Reusability.
 Code reusability is also best achieved by aggregation when
there is no is-a relationship.
◦ Inheritance should be used only if the relationship is-a is
maintained throughout the lifetime of the objects
involved; otherwise, aggregation is the best choice
HAS-A relationship(Example)
 Employee object contains many information such
as id, name, email ID etc. It contains one more
object named address, which contains its own
information such as city, state, country, zipcode
etc.

 Employee has an entity reference address, so


relationship is Employee HAS-A address
Example

 Save as Address.java
Employee Class (Emp.java)
Task
 Question:
 In inheritance, a subclass inherits all the members

(fields, methods, and nested classes) from its


superclass. Does it also inherit private members
of superclass? What if some protected members
are used in superclass?
 Write a code to verify the response of members of

both types.
Questions?

You might also like