Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Java OOP Reviewer Encapsulation Polymorphism

A Reviewer for JAVA Object Oriented Programming

Uploaded by

jrobertzz616
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Java OOP Reviewer Encapsulation Polymorphism

A Reviewer for JAVA Object Oriented Programming

Uploaded by

jrobertzz616
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java OOP Concepts Reviewer: Encapsulation and Polymorphism

1. Encapsulation
Encapsulation is a fundamental concept in object-oriented programming that involves
wrapping code and data together into a single unit. It helps control access to the attributes
and methods within a class, improving security and flexibility in code management.

1.1 Purpose of Encapsulation


Encapsulation allows for information hiding, where the variables of a class are hidden from
other classes and can only be accessed or modified through methods within the same class.
This is achieved by declaring class attributes as private and using getter and setter methods
to access or modify these variables.

1.2 Benefits of Encapsulation


• Provides better control of class attributes and methods.
• Allows the class to be made read-only or write-only as required.
• Increases data security by hiding sensitive information.
• Enhances code maintainability and flexibility.

1.3 Using Encapsulation in Java


In Java, encapsulation is implemented by:
- Declaring attributes as private.
- Using public getter and setter methods to access or modify the values.
Example:
```java
public class Student {
private int userID;
private String username;

public int getUserID() {


return userID;
}

public void setUserID(int userID) {


this.userID = userID;
}
}
```

1.4 Java Packages and APIs


In Java, packages are used to group related classes and manage code organization. Java
offers both built-in packages (Java API) and user-defined packages that can be customized
as needed.
2. Polymorphism
Polymorphism is an OOP concept that allows objects of different classes to be treated as
objects of a common superclass. This provides the flexibility to define multiple methods
with the same name, each performing different functions, depending on the context.

2.1 Types of Polymorphism


• Compile-time (Static) Polymorphism:
Achieved through method overloading, where multiple methods in the same class have the
same name but different parameters.

• Run-time (Dynamic) Polymorphism:


Achieved through method overriding, where a subclass provides a specific implementation
of a method that is already defined in its superclass.

2.2 Relationship with Inheritance


Polymorphism and inheritance are closely related. While inheritance allows for code reuse
and establishes a hierarchy, polymorphism enables a unified interface for various
subclasses. Together, they enable flexible and reusable code that can operate at a higher
level of abstraction.

2.3 Key Differences: Polymorphism vs. Inheritance


• Purpose:
- Inheritance is mainly for code reuse and class hierarchy.
- Polymorphism is for dynamic method invocation and creating a common interface.

• Keywords:
- Inheritance uses the 'extends' keyword.
- Polymorphism primarily involves method overriding and dynamic binding.

2.4 Example of Polymorphism


Example of method overloading:
```java
public class Calculator {
public int multiply(int a, int b) {
return a * b;
}

public double multiply(double a, double b) {


return a * b;
}
}
```
2.5 Practical Uses of Polymorphism and Inheritance Together
Inheritance establishes a class hierarchy, while polymorphism enables methods to adapt to
different specific implementations dynamically. This combination supports scalable and
adaptable code structures.

You might also like