Docsity

Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, Interfaces, and Abs, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, Interfaces, and Abs and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Chapter#11: Main Pillars of the

Object Oriented Programming

Paradigm

Anasse Bari, Ph.D. Courant Institute of Mathematical Sciences Department of Computer Science CS101 Introduction to Computer Science

Objectives

❖ Introducing the concepts of Encapsulation and Inheritance ❖ Learning the Super keyword, superclass methods and data fields ❖ Introducing the concept of polymorphism ❖ Learning how to use interfaces and abstract classes in Java ❖ Introducing the comparable java interface ❖ Introducing casting and instance of ❖ Understanding where to use an interface or an abstract class 2

Review Summary on OOP

❖ Object-oriented Design: A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects ❖ Object: A thing or entity that makes sense within the context of the problem ❖Class: A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class. 4

Encapsulation

❖Encapsulation means that all data members (fields) of a class are declared private. Some methods may be private, too. ❖The class interacts with other classes (called the clients of this class) only through the class’s constructors and public methods. ❖Constructors and public methods of a class serve as the interface to class’s clients. ❖Hides the fine detail of the inner workings of the class ❖The implementation is hidden ❖Often called "information hiding" ❖Part of the class is visible ❖The necessary controls for the class are left visible ❖The class interface is made visible ❖The programmer is given only enough information to use the class 5

Encapsulation

❖ Changes in the code create software maintenance problems ❖ Usually, the structure of a class (as defined by its fields) changes more often than the class’s constructors and methods 7

Inheritance

❖Inheritance allows a software developer to derive a new class from an existing one ❖The existing class is called the parent class, or superclass, or base class ❖The derived class is called the child class or subclass ❖As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class ❖A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. ❖Inheritance represents the is a relationship between data types 8

Inheritance – SoccerPlayer example

Class Goalkeeper extends SoccerPlayer {......} …

Inheritance – Person example

Inheritance – 2D/3D point example

Inheritance - example

Inheritance: Super keyword

❖The keyword “super” is used to access superclass constructors, methods, and data

fields

❖Examples:

super(); invokes the default constructor of the superclass.

This call can be made only from within a constructor of a subclass

❖super(paramList);

Invokes the constructor with parameters of the superclass (assuming it exists).

This call can only be made from within a constructor of a subclass.

Inheritance: Super keyword

❖super.methodName(paramList);

Invokes a method of the superclass. This call can be made from anywhere in the

subclass.

❖super.dataField;

Accesses a data Field of the superclass. This call can be made from anywhere in the

subclass.

Inheritance: Superclass Example 1 public class C1{ protected int x; public void setX (int x) { this.x = x; } //the rest of the class definition } public class C2 extends C1{ protected float c; public C2 (float c, int x) { this.c = c; setX(x); } //the rest of the class definition }

Inheritance: Superclass Example 1 (cont’d)

In this example, the class C2 can access the data field “x” and the method “setX()”

without defining them because they are inherited from C1.