Sunteți pe pagina 1din 4

II.

POLYMORPHISM
It is an object-oriented concept that allows us to create versatile software
designs
A.Y. 2019-2020 • The term polymorphism literally means "having many forms“
REVIEWER FOR 2nd QUARTERLY ASSESSMENT IN • It allows creating programs that process objects sharing the same
COMPUTER PROGRAMMING III superclass in a class hierarchy.
• A polymorphic reference is a variable that can refer to different types
I. IMPLEMENTING INHERITANCE of objects at different points in time
INHERITANCE - is a kind of software reuse. A new class is constructed • The method invoked through a polymorphic reference can change
by holding an existing class’ members enhancing them with a new or from one invocation to the next
revised capability • All object references in Java are potentially polymorphic
Syntax:
class sub extends Super{ References and Inheritance
• An object reference can refer to an object of its class, or to an object
▪ Software reusability of any class related to it by inheritance. For example, if the Employee
▪ Create new class from existing class class is derived from a class called Manager, then a Manager
▪ Absorb existing class’s data and behaviors reference could be used to point to an Employee object.
▪ Enhance with new capabilities
The existing class is known as superclass while the new class is known Manager
as the subclass. Manager rate;
▪ Subclass extends superclass rate = new Employee();
▪ Subclass
▪ More specialized group of objects
▪ Behaviors inherited from superclass
▪ Can customize Employee
▪ Additional behaviors
Class hierarchy – has one root class called object Polymorphism via Inheritance
▪ Direct superclass
• It is the type of the object being referenced, not the reference type,
Inherited explicitly (one level up hierarchy – it is the
that determines which method is invoked. Suppose the Manager class
subclass that openly inherits)
has a method called EmployeeRate, and the Employee class
▪ Indirect superclass
overrides it.
Inherited two or more levels up hierarchy (it is a class
• Now consider the following invocation:
beyond direct superclass
Superclasses and subclasses
rate.EmployeeRate();
▪ Object of one class “is an” object of another class
Example: Rectangle is quadrilateral. • If rate refers to a Manager object, it invokes the Manager version of
Class Rectangle inherits from class Quadrilateral EmployeeRate; if it refers to an Employee object, it invokes the
Quadrilateral: superclass Employee version.
Rectangle: subclass
III. USING SUPER TO ACCESS OBJECT AND CONSTRUCTORS IV. USING ABSTRACT CLASSES AND INTERFACES

The super keyword in Java is a reference variable which is used to refer Abstract class in Java is class which is declared with the abstract
immediate parent class object. Whenever you create the instance of keyword. It can have abstract and non-abstract methods (method with the
subclass, an instance of parent class is created implicitly which is referred by body).
super reference variable.
An interface is a design of a class that contains static constants and
Usage of Java super Keyword abstract methods.

• super can be used to refer immediate parent class instance variable. Abstraction is a way of converting real world objects in terms of class. It
• super can be used to invoke immediate parent class method. is a process of hiding the implementation details and showing only
• super() can be used to invoke immediate parent class constructor. functionality to the user.

Syntax of an Abstract Method:

abstract type name (parameter-list);

RULES FOR JAVA ABSTRACTION

ABSTRACT CLASS THAT HAS AN ABSTRACT METHOD

• Super can be used to call superclass constructors from a subclass. Bike is an abstract class that contains only one abstract method run. Its
• Super can also be used to call a superclass instance in a subclass. implementation is provided by the Honda
• Super can also be call to a superclass instance in a subclass.
ACCESS MODIFIERS
There are two types of modifiers in Java: access modifiers and non-access
modifiers.

The access modifiers in Java specifies the accessibility or scope of a field,


method, constructor, or class. We can change the access level of fields,
constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:


1. Private: The access level of a private modifier is only within the class.
It cannot be accessed from outside the class.
Output: running safely 2. Default: The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you do

HANDLING EXCEPTIONS not specify any access level, it will be the default.
An Exception is a setback results from the implementation of a program. 3. Protected: The access level of a protected modifier is within the
package and outside the package through child class. If you do not
When an exception happens, the typical movement of the program is
interrupted and the program or the application stops. make the child class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be
These exceptions are may be initiated by the user, by the program developer, accessed from within the class, outside the class, within the package
and other computer resources that have crashed. and outside the package.

There are three categories of exceptions, namely:

CHECKED EXCEPTIONS. These happen at compile time and are also known Accessible in the Accessible in different
as Compile Time Exception. An example is opening a file does not exist. Modifier
same package package
Private No No
UNCHECKED EXCEPTIONS. These happen at compile time and are also
known as Runtime Exceptions. An Example is logic errors or inappropriate use Yes, only if the class extends
Protected Yes
of API (Application Programming Interface). the main class
Default Yes No
ERRORS. These are glitches that arise beyond the control of the user or
program developer. Public Yes Yes

There are five keywords used in handling exceptions, namely: try, catch, throw,
throws and finally. The program statements that you want to check for
exceptions a delimited within a try block.
V. ENCAPSULATION Object-Oriented Language - focus of OOP languages is not on structure,
but on modeling data.
ENCAPSULATION – it achieved by combining the methods and attribute • Programmers code using “blueprints” of data models called classes.
in a class. It is also known as binding (or wrapping) code and data together • Examples of OOP languages include C++, Visual Basic.NET and Java.
into a single unit are known as encapsulation. For example, a capsule, it
is wrapped with different medicines. A java class is the example of SIMULA is considered the first object-oriented programming language.
encapsulation. Java bean is the fully encapsulated class because all the The programming paradigm where everything is represented as an object
data members are private here. is known as a truly object-oriented programming language.

OBJECT
Is any entity that has state and behavior is known as an object. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical. An Object can be defined as an instance of a class. An object
contains an address and takes up some space in memory.

CLASS
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an
individual object. Class doesn't consume any space.

VI. OBJECT ORIENTED PROGRAMMING


Object-Oriented Programming is a methodology or paradigm to design
a program using classes and objects. It simplifies software development
and maintenance by providing some concepts:

• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

S-ar putea să vă placă și