Tag Archives: Inheritance

Java Video Tutorial 14

Java Video Tutorial 14In this Java video tutorial I cover Polymorphism, Inheritance, Protected, Final, Instanceof and a bunch more.

These topics are not complicated, but for some reason they are normally presented in complicated ways.

Use the code that follows the video to help you gain a complete understanding of these topics. If anything isn’t 100% clear leave a comment below and I’ll help.

Continue reading Java Video Tutorial 14

Python 2.7 Tutorial Pt 8

Python 2.7 TutorialIn this tutorial, I continue to explain how Object Oriented Programming is used with Python 2.7. I cover some of the more complicated subjects including how to:

  • Allow the user to define an infinite number of attributes
  • Use Inheritance and what it is
  • Override Methods
  • Use Polymorphism and what it is
  • Inherit from 2 more more classes
  • Use many of the built in object methods in Python

If you don’t completely understand Object Oriented Programming after this and the first part of this tutorial Python Object Oriented Programming. Please leave a comment below and I’ll do whatever I can to explain this important subject.

Like always, a lot of code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:

Here is All the Code from the Video

Note: You have to insert the white space and everything will work. I could have styled it with HTML, but that would have required you to erase all of the tags. Hope this helps?

#! /usr/bin/python

__metaclass__ = type

class Animal:

__name = “No Name”
__owner = “No Owner”

def __init__(self, **kvargs): # The constructor function called when object is created
self._attributes = kvargs

# There is a function called a destructor __del__, but its best to avoid it

def set_attributes(self, key, value): # Accessor Method
self._attributes[key] = value
return

def get_attributes(self, key):
return self._attributes.get(key, None)

def noise(self): # self is a reference to the object
print(‘errr’) # You use self so you can access attributes of the object
return

def move(self):
print(‘The animal moves forward’)
return

def eat(self):
print(‘Crunch, crunch’)
return

def __hiddenMethod(self): # A hidden method
print “Hard to Find”
return

class Dog(Animal):

def __init__(self, **kvargs):   # Not needed unless you plan to override the super
super(Dog, self).__init__() # This wouldn’t work without the second line
self._attributes = kvargs

def noise(self):        # Overriding the Animal noise function
print(‘Woof, woof’)
Animal.noise(self)
return

class Cat(Animal):

def __init__(self, **kvargs):  # Not needed unless you plan to override the super
super(Cat, self).__init__()
self._attributes = kvargs

def noise(self):
print(‘Meow’)
return

def noise2(self):
print(‘Purrrrr’)
return

class Dat(Cat,Dog):

def __init__(self, **kvargs):  # Not needed unless you plan to override the super
super(Dat, self).__init__()
self._attributes = kvargs

def move(self):
print(‘Chases Tail’)
return

def playWithAnimal(Animal): # This is polymorphism
Animal.noise()
Animal.eat() # Works even if the method isn’t in Cat because Cat is an Animal
Animal.move()
print(Animal.get_attributes(‘__name’))
print(Animal.get_attributes(‘__owner’))
print ‘\n’
Animal.set_attributes(‘clean’,”Yes”)
print(Animal.get_attributes(‘clean’))

jake = Dog(__name = ‘Jake’, __owner = ‘Paul’)
sophie = Cat(__name = ‘Sophie’, __owner = ‘Sue’)
playWithAnimal(sophie)
playWithAnimal(jake)

# print sophie.__hiddenMethod() Demonstrating private methods

print issubclass(Cat, Animal) # Checks if Cat is a subclass of Animal
print Cat.__bases__           # Prints out the base class of a class
print sophie.__class__        # Prints the objects class
print sophie.__dict__         # Prints all of an objects attributes

japhie = Dat(__name = ‘Japhie’, __owner = ‘Sue’)
japhie.move()
print japhie.get_attributes(‘__name’)

Python How To Video Pt 5

Python PictureIn this video, I completely cover how Object Oriented Programming (OOP) works in Python. This is also how it works in other OOP languages though.

Inheritance, Polymorphism, Classes and Objects are jargon terms specific to the concept of programming in OOP. These concepts do not change based off of the language you use.

All of the code from the video, follows after the video below. If you haven’t watched the other video’s, watch them first or you will be confused.

Like always, if you have any questions or comments, leave them in the comment section below.

Continue reading Python How To Video Pt 5

iPhone Development: Objective C Pt 5

Object Oriented ProgrammingObject Oriented Programming with Objective C

In a previous article, I explained how you create object’s, instance variable’s and method’s in Objective C. In this article, I’ll explain Inheritance, Polymorphism and go over other OOP topic’s I missed previously.

Briefly, Inheritance provide’s you with a way to build additional capability’s onto existing class’s.

Polymorphism is a little more complicated. It allows you to call a parent object method and it will sort out which class object method should be executed.

Continue reading iPhone Development: Objective C Pt 5