Object Oriented Programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. It is based on several techniques, including encapsulation, modularity, polymorphism, and inheritance
What is a Class?
1 A Class
Ω Is a blue print used to create objects.
Ω Is a template that describes the kinds of state and behavior that objects of its type support
2 Examples :
• Animal, Human being, Automobiles, Bank Account, Customer
What is an Object?
1 An object
2 Is an instance of that class (image from class).
3 That object will have its own state, and access to all of the behaviors defined by its class.
• A software object is modeled after real world objects
• A software object is a representative of the real world object
• Can be viewed as a "black box" which receives and sends messages
• Examples
• Car
• Telephone
• Pen
Core concepts of OOP
1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation (Modularity and data hidden)
We will navigate with the first concept of the OOP and is consider it the big boss .Encapsulation contains the functional details of a class (state and behavior) and it's the Process of hiding the members from outside the class.
☼Encapsulate = “En” + “Capsulate”
☼ “En” = “In a”
☼Encapsulate = “In a Capsule”
1 Why we say Modularity?
Is a module that describes the kinds of state and behavior that objects of its type support and I will discuss state and behavior
1 Why we say Data hidden?
Encapsulation means localization of information of knowledge within an object.
Example: A car’s dashboard hides the complexity and internal workings of its engine.
►State (Member variables)
• The internal state of the object represented by values stored in member variables
• Variables defined inside a class form the state of the class
• Not exposed to external world
►Behavior (Member Methods)
• Behavior exhibited by the class to external world
• Functions defined inside the class form the behavior of the class
• Exposed to external world
Example:
Car object ►State – Current Speed – Current Gear – Engine State (Running, Not Running) ►Behavior Acts on the object and changes state – Slow down – Accelerate – Stop – Switch Off Engine – Start Engine | Dog Object ►State – Color – Breed – Activity (Barking/Not barking) – Tail Activity (Wagging/Not Wagging) ►Behavior – Bark – Wag Tail – Eat |
Inheritance
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, and current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Notes
1 Inheritance refers to a class replicating some features or properties from another class
2 Inheritance allows definition of new classes on similar lines of a base class (Also called parent or Super class)
3 The class which inherits from another class is called as ‘derived class’
Example
class Employee : Person
- class Person is inherited by the class Employee
►Note
Employee is a Person but Person is not Employee and this called " is a" relationships.
Polymorphism
1 Polymorphism is the ability to have many different forms.
2 Polymorphism appear with the following:
o Inheritance
o Overloading
o Overriding
For example, the Manager class has access to methods from Employee class.
Employee employee = new Manager(); //legal
►Note
• An object has only one form.
• A reference variable can refer to objects of different forms.
0 التعليقات:
إرسال تعليق