[go: up one dir, main page]

0% found this document useful (0 votes)
8 views20 pages

02 Java Inheritance

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)
8 views20 pages

02 Java Inheritance

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/ 20

JAVA ENTERPRISE EDITION

PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220

ENGR. RUELLA YARES MAGTAAS


JAVA INHERITANCE
CONTENT

• inheritance in Java
• aggregation in Java
INHERITANCE IN JAVA
INHERITANCE IN JAVA

• a mechanism in which one object acquires all the properties


and behaviors of a parent object. It is an important part of
OOPs (Object Oriented programming system).
• 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.

• inheritance represents the IS-A relationship which is also


known as a 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: A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created.
• 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: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class.
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.

• in the terminology of Java, a class which is inherited is called


a parent or superclass, and the new class is called child or
subclass.
TYPES OF INHERITANCE IN JAVA

• on the basis of class, there can be three types of inheritance


in java: single, multilevel and hierarchical.

• in Java programming, multiple and hybrid inheritance is


supported through interface only.
Multiple inheritance is not supported in Java
through class.
• When one class inherits multiple classes, it is known as
multiple inheritance.
SINGLE INHERITANCE

• when a class inherits another class, it is known as a single


inheritance.
MULTILEVEL INHERITANCE

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


inheritance.
HIERARCHICAL INHERITANCE

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


as hierarchical inheritance.
WHY MULTIPLE INHERITANCE IS NOT
SUPPORTED IN JAVA?

• to reduce the complexity and simplify the language, multiple


inheritance is not supported in java.
AGGREGATION IN JAVA
AGGREGATION IN JAVA

• if a class have an entity reference, it is known as


Aggregation. Aggregation represents HAS-A relationship.
WHY USE AGGREGATION?

• for code reusability.


WHEN USE AGGREGATION?

• code reuse 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.

You might also like