0% found this document useful (0 votes)
16 views52 pages

Presentation 4

The document provides an overview of inheritance in Object-Oriented Programming, detailing concepts such as derived classes, method overriding, and access modifiers. It explains the relationships between base and derived classes, the use of UML diagrams, and the significance of keywords like 'super' and 'this'. Additionally, it covers the implications of access modifiers and the distinction between 'is a' and 'has a' relationships in class design.

Uploaded by

kakefon876
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)
16 views52 pages

Presentation 4

The document provides an overview of inheritance in Object-Oriented Programming, detailing concepts such as derived classes, method overriding, and access modifiers. It explains the relationships between base and derived classes, the use of UML diagrams, and the significance of keywords like 'super' and 'this'. Additionally, it covers the implications of access modifiers and the distinction between 'is a' and 'has a' relationships in class design.

Uploaded by

kakefon876
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/ 52

IT 214

Object
Oriented
Programming
Dr. Abdulaziz Saleh Algablan
Email: [email protected]
2024
Outline
• Introduction to Inheritance
• Derived Classes
• Overriding Method Definitions
• Overriding vs. Overloading
• The final Modifier
• Protected and Default Modifier
• Summary of Access Modifiers
• UML Inheritance Diagrams
• Super and this keyword
• “Is a” and “Has a” Relationships 2
Introduction to Inheritance
• Inheritance allows us to define a general class
and then define more specialized classes
• by adding new details to the more general class
definition.
• A more specialized class inherits the
properties of the more general class, so that
only new features need to be programmed.

3
Inheritance
• Inheritance is a mechanism in which one object acquires all the
properties and behaviours of a parent object.
• It is an important part of OOP (Object Oriented programming).
• The idea behind inheritance is that you can create new classes that
are built upon existing classes.
• When a class inherits from an existing class, methods and fields of the
parent class can be reused.
• new methods and fields can be added the child class
Terms Related to Inheritance

• Subclass/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 reusing the fields and methods of the existing
class by inheritance.
Inheritance in UML
• The white filled arrow is used in UML to
represent inheritance.
• The Programmer class is a subclass of the
Employee class.
Shape Example
Types of Inheritance in Java
Base and Derived Classes Example
Consider a college record-keeping system with records about
students, faculty and staff.
Base and
Derived
Classes,
cont.

10
Base and Derived Classes Example

11
Derived Classes, cont.
• Even though your program may not need any
Person or Employee objects, these classes
can be useful for consolidating and
representing features common to all
subclasses.
• For example, all students, faculty, and staff have
names, and these names may need to be
initialized, changed, retrieved, or printed.

12
Derived Classes, cont.

• class Student is a derived class of class


Person and class Person is called the
base class.

13
Derived Classes, cont.
• public class Student extends Person

14
Derived Classes, cont.
• Syntax:
public class Derived_Class_Name extends Base_Class_Name
{
// Declarations_of_Added_Instance_Variables
// Definitions_of_Added_and_Overridden_Methods
}

15
Derived Classes, cont.
• When you define a derived class:
• You declare only the added instance variables
• You define only the added and overridden
methods.
• The variables and methods of the parent class
which are not declared private are inherited
automatically.

16
Derived Classes, cont.
• class InheritanceDemo

17
Derived Classes, cont.

18
Overriding Method Definitions
• Notice that class Student has a method
writeOutput with no parameters
• The class Person also has a method
writeOutput with no parameters, that class
Student inherits.
• When a derived class defines a method with
the same name and the same number and
types of parameters as a method in the base
class, the method in the derived class
overrides the method in the base class.
19
Overriding Method Definitions,
cont.
• When overriding a method, you can change
the method body with new behavior.
• However, you cannot change the method’s
heading or the method’s return type.

20
Overriding vs. Overloading
• When you override a method, the new
method definition in the derived class has the
same name and the same number of types of
parameters as the method definition in the
base class.
• When the name is the same, but the number
or types of the parameters differs, whether in
the base class or in the derived class, the
method is overloaded in the derived class.

21
Overriding vs. Overloading,
cont.
• example:
public String getName(String title)
in class Student and
public String getName()
in class Person overload method getName since
the two methods have different parameter lists.
• Both methods are available in class Student.

22
The final Modifier
• You can prevent a method definition from
being overridden by adding the word final to
the method heading.
• example
public final void someMethod(){ }
• This is used rarely, but it produces more
efficient code.

23
The final Modifier, cont.
• An entire class can be declared final, in which
case it cannot be used as a base class to
derive another class.

24
Private Instance Variables in
the Base Class
• Private instance variables inherited from a
base class cannot be accessed directly.
• Instead, they must be accessed using a
method that is not declared private.
• While this may seem inconvenient, it provides
an important mechanism for controlling access
and changes to instance variables in the base
class.

25
Private Methods in the Base
Class
• Like private instance variables, private
methods inherited from a base class cannot
be accessed directly.
• Instead, they, too, must be accessed using a
method that is not declared private.
• This provides an important mechanism for
controlling access to methods in the base
class.

26
Private Methods in the Base
Class

• Since private methods typically serve as


helping methods, their use always is limited to
the class in which they are defined.

27
Protected Modifier
class A
{
protected int att1;
}

class childOfA extends A


{
void m1()
{
att1++;
}
} 28
Protected Modifier
package firstPackage;
• Protected modifier allows class A
subclass to access superclass {
protected int att1;
methods and attributes in }
different package.
package secondPackage;

class childOfA extends A


{
void m1()
{
att1++;
}
}
29
Protected Modifier
• Protected fields and methods of a class called BaseClass:
• Accessible from other classes within the same package.
• In addition, Subclasses of BaseClass in different packages can access
BaseClass’s attributes and methods.

30
Default Modifier
• When no access modifier is specified: package firstPackage;
• Fields and methods have default, or package level class A
access. {
int att1;
• Fields and methods accessible within the same }
package.
• Unlike protected modifier, subclass in different
package has no access to default attributes and
methods.

31
Default Modifier
package firstPackage;
• Default modifier does not class A
allow subclass to access { att1 has
int att1; default
superclass methods and } modifier
attributes when they are in
different package. package secondPackage;

class childOfA extends A


{
void m1()
{
att1++; Error
}
}
32
Summary of Access Modifiers
Access modifier/
attributes, or methods public protected default private

Same class YES YES YES YES

Sub class in the same package YES YES YES NO

None-sub class in the same package YES YES YES NO

Sub class in different package YES YES NO NO

None-sub class in different package YES NO NO NO

33
UML Inheritance Diagrams
• Typically, a UML class diagram shows only as
much as is needed for the design task at
hand.

34
UML Inheritance Diagrams,
cont.

35
UML Inheritance Diagrams,
cont.
• An arrowhead points from a derived class to
its base class, indicating an is-a relationship.
• For example, a Student is-a Person.
• Inherited instance variables and methods can
be found by following the arrowheads.

36
UML Inheritance Diagrams,
cont.
• more details of the inheritance hierarchy

37
Constructors in Derived
Classes
• A base class has its own constructors.
• Their purpose typically is to initialize the instance
variables declared in the base class.
• A derived class has its own constructors.
• Their purpose typically is to call a constructor in
the base class, and then to initialize the instance
variables declared in the derived class.

38
Constructors in Derived
Classes, cont.
• To call a constructor in the base class, use
super(Values_for_Instance_Variables_Declared_i
n_the_Base_Class);
• example
super(initialName);
not
Person(initialName); //ILLEGAL

39
Using super
• The call to the constructor in the base class
(using super) must be the first action taken in
the constructor of a derived class.
• When no call to the constructor in the base
class is included, Java automatically includes
a call to the default constructor in the base
class.

40
Using super, cont.
• For a Student class which extends Person, the
following are equivalent definitions:
• First:
public Student()
{
super();
studentNumber= 0;
}
• Second:
public Student()
{
studentNumber= 0;
}
41
Why super() Then ?
• Super allows direct the call to one constructor of the base class.
• V1:
public Student()
{
super(“Not Assigned”); // force calling Person(String aString)
studentNumber= 0;
}

• V2:
public Student()
{
studentNumber= 0;
}
V2 calls the default super as first action, while V1 allows to call a different constructors.

42
The this Method
• Within the definition of one constructor, it can
be appropriate to call another constructor in
the same class.
• The keyword this is used to call another
constructor in the same class.
• example
this(initialName, 0)

43
The this Method, cont.
• Any use of this must be the first action in the
constructor definition.
• Thus, a constructor definition cannot
contain a call using super and a call using
this.
• To use both super and this, include a call
using this in one constructor and a call using
super in the constructor called using this.

44
Calling an Overridden Method
• super can be used to call a method in the
base class that has been overridden in the
derived class.
• example
super.writeOutput();
• However, you cannot repeat the use of super
to invoke a method in some ancestor class
other than the immediate base (parent) class.

45
Multilevel Derived Classes

• Class Undergraduate can be derived from class


Student which is derived from class Person.
• Class Undergraduate will have all the instance
variables and methods of class Student which
has all the instance variables and methods of
class Person.

46
Programming Example:
Multilevel Derived Classes,
cont.
• class Undergraduate

47
Multilevel Derived Classes,
cont.

48
Multilevel Derived Classes,
cont.

• A chain of derived classes permits code to be


reused which improves efficiency.

49
Terminology
• A base class often is called a parent class.
• A derived class then is called a child class.
• A class that is a parent of a parent of…a
parent of (with one or more “parent of”s)
another class often is called an ancestor
class.
• A class than is a child of a child of…a child of
(with one or more “child of”s) often is called a
descendant.

50
“Is a” and “Has a”
Relationships
• A derived class is a more specialized class
than its base class or any of its ancestor
classes.
• For example, a student is a person.
• A more complex object has as one or more of
its instance variables one or more references
to objects of a simple class, exhibiting a has a
relationship.
• For example, a person has a name.

51
The End

52

You might also like