fundamental principles of object oriented programming language
TRANSCRIPT
Fundamental Principles of Object Oriented
Programming Language
Lecture Code: CIT 301
Bowen University, Iwo Nigeria
Learning Objectives: To discuss the principles of OOP with respect to JAVA
Programming Language
Targeted Students: Year Three (3)
Course Title: Java Programming Language
Reminder: Introduction to Object Oriented Programming
Literally – an Object as STATE and BEHAVIOUR
STATE: are the Descriptive characteristics of the Object
BEHAVIOUR: What the Object can do or what can be done to an Object
Object-oriented programming is a programming
paradigm where everything is represented as an object.
Objects pass messages to each other. Each object decides
what to do with a received message. OOP focuses on each
object’s states and behaviors.
4 Basic Principles of OOP
Encapsulation
Encapsulation is a process of wrapping code and data
together into a single unit.
Encapsulation is the mechanism of hiding of data
implementation by restricting access to public methods.
Instance variables are kept private and accessor methods are
made public to achieve this.
We can create a fully encapsulated class in Java
by making all the data members of the class
private. Now we can use setter and getter
methods to set and get the data in it.
To achieve encapsulation in Java −
•Declare the variables of a class as private.
•Provide public setter and getter methods to modify and
view the variables values.
It provides you the control over the data.
It is a way to achieve data hiding
The encapsulate class is easy to test.
It improves maintainability and flexibility and re-usability:
User would not be knowing what is going on behind the scene.
public class EncapTest {private String name;public void setName(String newName) {
name = newName;}
public void setIdNum( String newId) {idNum = newId;
}}
Advantages of Encapsulation
Inheritance
Inheritance is a mechanism in which one object
acquires all the states and behaviors of a parent object.
Inheritance uses a parent-child relationship (IS-A relationship).
The following are inherited:
• public instance methods.
• private instance variables (private instance variables can be
accessed only through public getter and setter methods).
Inheritance allows us to reuse of code, it improves reusability
in your java application.
class Subclass-name extends Superclass-name {
//methods and fields }
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
How does Inheritance Work?
A class can extend only one class however it can implement any
number of interfaces. An interface can extend more than one
interfaces.
The extends keyword is used to indicate that the class which is
being defined is derived from the base class using inheritance. So
basically, extends keyword is used to extend the functionality of
the parent class to the subclass.
The implements keyword is used to implement an interface. An interface is a special type
of class which implements a complete abstraction and only contains abstract methods.
Extend VS Implement Keyword
We discuss Abstraction and Polymorphism
Questions and Contributions are Welcome