OOP BASICS
Object-Oriented Programming (OOP)
Many things can be represented in code.
Pillars: Abstraction, Inheritance, Encapsulation, Polymorphism.
Model code using classes; classes are blueprints.
Physical objects can be modeled in code with classes.
Access Modifiers
Allow you to programmatically determine where certain variables and methods can be accessed in your code.
Access Modifiers in Java
Private: Only visible in the class that the member lives in.
No modifier (default): Visible in the package it lives in.
Protected: Visible to the package and all subclasses.
Public: Accessible everywhere within the program.
Concepts of OOP
Encapsulation
Keep it close, keep it tight!
Entity of name coming from Main Object Calling of an Object name and method
Private Variable
Getter returns the variable value
Setter sets the variable value
Object Calling of an Object name and method
Private Variable and Entity came from another class
Getter returns the variable value
Setter sets the variable value
Sample:
Name: Naevis
Age: 4
Add another private variable for Height in another class. The Height setter must be with the setter of Age(One setter only for Age and Height). Upon running the output must be: Age and Name Based on the given example:
Inheritance
A concept in OOP that allows a class to inherit all the properties and behavior of a parent class.
Creating new classes that are built upon existing classes.
Use of keyword:
extends– means to extend functionality.
Inheritance Terms
Class: A blueprint from which objects are created.
Child Class / Sub Class / Derived Class: A class which inherits another class.
Parent Class / Super Class / Base Class: A class from where a subclass inherits features.
Reusability: A mechanism which facilitates you to reuse the fields and methods of an existing class when you create a new class.
Inheritance Example
Biological father: Freckles gene
Biological mother: No-freckles gene
Child: Freckles
Analogy: Birds with the same feather flock together.
Code Example
public class Al_Inheritance { public static void main(String[] args) { child c1 = new child(); System.out.println("Skin Color: " + c1.SkinColor); } } class Parent { String SkinColor = "Pale Skin"; } class child extends Parent { }Parent Class: Super Class/ Base ClassChild Class: Sub Class/ Derived ClassObject: Calling of variable
Inheritance Levels
Single Level: Calc -> AdvCalc
Multi-Level: Calc -> AdvCalc -> veryAdv
Calculator Example
Create two classes named
CalcandAdvCalc.In the
Calcclass, create two methods for addition and subtraction.In the
AdvCalcclass, create another two methods for multiplication and division.Extend the
Calcclass toAdvCalc.In another class, where your main method is, create an object that calls the
AdvCalcclass.Put two int values on method arguments (e.g., 10, 10).
After that, display the outcome of those four methods.
Polymorphism
Comes from the Greek words: "Poly" means many, and "Morphs" means forms.
Polymorphism means having many forms.
The ability of a message to be displayed in more than one form.
Allows performing a single action in different ways.
Two Types of Polymorphism
Compile-time Polymorphism
Achieved by function overloading or operator overloading.
Multiple functions/methods with the same name but different parameters are said to be overloaded.
Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.
Compile-time Polymorphism Example
package weekl_oop; public class A3_Poly_Compile { public static void main(String[] args) { calculations c1 = new calculations(); System.out.println(c1.Multiply(2, 2)); System.out.println(c1.Multiply(2.5, 2.5)); } } class calculations { public int Multiply(int a, int b) { return a * b; } public double Multiply(double a, double b) { return a * b; } }Same method name but different parameters.
Different number of arguments.
Runtime Polymorphism
A process in which a function call to the overridden method is resolved at runtime.
Achieved by Method Overriding.
Occurs when a child class has the same method as the parent class, which is said to be overridden.
Runtime Polymorphism Example
public class A2_Polymorphism { public static void main(String[] args) { Robin b1 = new Robin(); b1.sing(); } } class Bird { public void sing() { System.out.println("Birds Singing"); } } class Robin extends Bird { @Override public void sing() { System.out.println("Birds Robin Tweet"); } }Same method name.
Method sing was override in class Robin