Skip to main content

Python Object Oriented

Python Object Oriented
Python is an Object oriented programming Language. This means there exists a concept called ‘class’ that let’s programmer structure the codes of a software in a fashioned way. Because of the use of classes and objects, the programming became easy to understand and code.

Defining Class And Object

A class is a technique to group functions & data members and put them inside a container so that they can be accessed later by using dot (.) operator. Objects are the basic runtime entities of object oriented programming. It defines the instance of a class. Objects get their variables & functions from classes & the class we will be creating are the templates made to create object.

Object Oriented Terminologies

  • class: Classes are user defined data type that is used to encapsulate data and associated functions together. It also helps in binding data together into a single unit.
  • Data Member: A variable or memory location name that holds value to does specific task within a program.
  • Member Function: They are the functions; usually a block of code snippet that is written to re-use it.
  • Instance variable: A variable that is defined inside a method of a class.
  • Function Overloading: This technique is used to assign multiple tasks to a single function & the tasks are performed based on the number of argument or the type of argument the function has.
  • Operator Overloading: It is the technique of assigning multiple function/tasks to a particular operator.
  • Inheritance: It is the process of acquiring the properties of one class to another i.e. one class can acquire the properties of another.
  • Instantiation: It is the technique of creating an instance of a class.

Program For Class In Python

Example:
#!/usr/bin/python

class karl :
varabl
= 'Hello'

def function(self) :
print "This is a message Hello"
Another program to explain functions inside class:
Example:
#!/usr/bin/python

class karl(object) :
def __init__(self, x, y):
self.x = x
self.y = y

def sample(self) :
print " This is just a sample code"
In the above code, we created a class name karl using ‘class’ keyword. And two functions are used namely __init__() (for setting the instance variable) & sample(). Classes are used instead of modules because programmers can take this class ‘karl’ & use it or modify it as many times as we want & each one won’t interfere with each other. Importing a module brings the entire program into use.

Creating Objects (Instance Of A Class)

Let’s see an example to show how to create an object:
Example:
#!/usr/bin/pythonclass student:
class student:
def __init__(self, roll, name):
self.r = roll
self.n = name
print ((self.n))

#...
stud1
= student(1, "Alex")
stud2
= student(2, "Karlos")

print (("Data successfully stored in variables"))
Output:
Alex
Karlos
Data successfully stored in variables

Accessing Object Variables

We can access the object’s variable using dot (.) operator.
The syntax is:
my object_name.variable_name
Example:
print object.varabl

Accessing Attributes

Object attributes can also be accessed by using dot operator.
Example:
stud1.display()
stud2
.display()

print " total number of students are: %d" %  student.i

Use Of Pre-defined Functions

Instead of using general statements to access attributes, programmers can use the following functions:
  • getattr( obj, name [,default] ) : used to access object’s attribute
  • hasattr( object, name) : used for checking whether the attribute exists or not
  • setattr( obj, name, value ) : set or create an attribute if doesn’t exist
  • delattr( obj, name ) : used to delete an attribute

Built-In Class Attributes

All the Python built-in class attributes can be accessed using dot (.) operator like other attributes.
The built-in class attributes are:
  • __dict__: This attribute is a dictionary that contains class’s-namespace
  • __doc__: Used for class documentation string
  • __name__: used as class-name
  • __module__: used to define module name for the class in which it is defined. In interactive mode it is __main__.
  • __bases__: An empty tuple containing the base-class

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

Java Method Overriding

Java Method Overriding Declaring a method in the subclass which already exists there in the parent class is known as method overriding. When a class is inheriting a method from a superclass of its own, then there is an option of overriding the method provided it is not declared as final. The advantage of using overriding is the ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity. There are certain rules that a programmer should follow in order to implement overriding. These are: In Java, a method can only be written in the child class and not in same class. Argument list should be exactly the same as that of the overridden method of that class. Instance methods can also be overridden if they are inherited by the child class. A constructor cannot be overridden. Final – declared methods cannot be overridden. Any method that is static cannot be used to override. The return type must have to be the...