Oop's(object oriented programming) is one of the most powerful tools of Python,java or any programming language. but without oop's you can't design a powerful program for any application.   

The four main concepts of object orientation are: 

1-Encapsulation 

2-Data Abstraction

3-Polymorphism

4-Inheritance

Oop

in Oop's, each object can receive messages, process data, and send messages to other objects.

other rest Oop's and some genral terms use in Oops as shown below: 

Attributes: Attributes are created inside of a class definition.

Methods: mathods are those attribute objects which define in classes.

The __init__ Method: 

code :

>>> class A: 
def __init__(self):          print("__init__ has been executed!")
>>> x = A()
__init__ has been executed!

Data Abstraction, Data Encapsulation :

Encapsulation is seen as the bundling of data with the methods that operate on that data. Encapsulation is often accomplished by providing two kinds of methods for attributes . the eq:  (Data Abstraction = Data Encapsulation + Data Hiding ) 

__str__- and __repr__-Methods:

OopOop

code:

>>> l = ["Python", "Java", "C++", "Perl"] 
>>> print(l)
['Python', 'Java', 'C++', 'Perl']
>>> str(l)
"['Python', 'Java', 'C++', 'Perl']"
>>> repr(l)
"['Python', 'Java', 'C++', 'Perl']"
>>> d = {"a":3497, "b":8011, "c":8300}
>>> print(d)
{'a': 3497, 'c': 8300, 'b': 8011}
>>> str(d)
"{'a': 3497, 'c': 8300, 'b': 8011}"
>>> repr(d)
"{'a': 3497, 'c': 8300, 'b': 8011}"
>>> x = 587.78
>>> str(x)
'587.78'
>>> repr(x)
'587.78'


Public,Protected and Private Attributes:

1:name - Public : used inside or outside of a class  

2:_name - Protected: attributes should't be used in class as outside.

3:__name - Private: This type of attribute are invisible.

class A(): 
def __init__(self): 
        self.__priv = "I am private"
        self._prot = "I am protected"
        self.pub = "I am public"

Inheritance :

it means avoid redundant the code for developers.

Two Built-in function : 

*isinstance()

*issubclass()

code: (clock calendar problem) 

from clock import Clock 
from calendar import Calendar
class CalendarClock(Clock, Calendar):
   def __init__(self, day,month,year,hours=0, minutes=0,seconds=0):         Calendar.__init__(self, day, month, year)
        Clock.__init__(self, hours, minutes, seconds)
   def __str__(self):
       return Calendar.__str__(self) + ", " + Clock.__str__(self)
if __name__  == "__main__":
x = CalendarClock(24,12,57)
  print(x)    for i in range(1000):       x.tick()
   for i in range(1000):       x.advance()
   print(x)

Polymorphism :

Polymorphism is the characteristic of being able assign a different things to a particular symbol. it  means they have many shapes.   

polymorphism are two types: 

1-Overloading :

Two or more method with different signatures.lets perform a arithmetic addition of two numbers, the feature in python that allow some operator to have different meaning according to the context is called operator overloading. 

Ex: "CUT" are three type of meanings for Surgeon,Hair stylist and Actor.  

Pseudo code in java: 

void draw(int x1, int y1, int x2, int y2) { 
}  
   void draw(float x1, float y1, float x2, float y2) {
    }

2-Overriding :

Replacing an inherited method with another having the same signature. 

or

two or more methods having the same name with same argument in parents class and child class in java. 

Pseudo code in java: 

class Company  
{
public void rule() {
System.out.println("company having rules");
}
}
class employee extends company
{ public void rule()
{
System.out.println("amployee must follow rules"); }
public void work() {
System.out.println("employee work for company"); }
}
public class TestEmployee {
public static void main(String args[]) {
Company = new Company();
} }
Oop

Happy Coding :)

  Modified On Sep-07-2019 12:18:16 AM

Leave Comment