Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,262 members, 7,829,523 topics. Date: Thursday, 16 May 2024 at 08:29 AM

Oop: States And Behaviours. Glitches - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Oop: States And Behaviours. Glitches (856 Views)

Object Oriented Programming (OOP) Tutorial / Learn Object Oriented Programming (OOP) AT SANDCROFT SOFTWARE / After 5 Months Of Learning OOP In Visual C#-here Is A Little Project I Made. (2) (3) (4)

(1) (Reply)

Oop: States And Behaviours. Glitches by asalimpo(m): 12:13pm On Feb 06, 2017
OOP : an object is anything that has state and behaviours.
classical oop definition.
e.g *Roll your eyes*
A car has 4 doors and 4 tires.
A dog has 4 legs, 1 tail and can bark.

Lets model them in java.
class Car{
int doors = 4;
int tires = 4;
//behaviours
//drive, brake
//represented using methods
public void drive(){}
public void brake(){}
public void accelerate(){}
public void decelerate(){}
]

class Dog{
int legs=4;
int tails=1;
//behaviours
//bark,run,wag tail
public void bark(){}
public void run(){}
public void wagTail(){}
}

All good.
But .. they're some similarities between objects in the universe.
Vehicles,Airplanes, birds, have something in common.
Vehicles move,decelerate,accelerate etc
Animals make sound, move,etc.
Rewriting the same similar behaviours will be ,uhm, tiresome and inefficient.
That's right.
So, in oop, you can use inheritance and some other techniques to address these issues.

Abstractness is employed.

abstract class Vehicle{
abstract void move()
abstract void accelerate()
abstract void decelerate()

}

Sedan extends Vehicle{
//overrides abstract methods, move, accelerate etc
}

Abstract class Animal
{
public abstract void animalSound(){}
public abstract void move()
}

Horse extends Animal
{
//animal sound is unique
public void animalSound()
{}
}


Dog extends Animal
{
//overrides abstract methods
//bark = produce dog sound
//move
}

Bird extends Animal
{
//animal sound is different etc
}

So through inheritance and abstraction.
behaviours and atrributes can be inherited/reused and overriden.
Code can be re-used.

// But in the real world, there's something else.
// Objects can acquire new behaviours and lose others!

e.g
Dog behaviours= bark,sleep,eat,shit,play.
after being trained by owner it can, shake visitors hand, drive car, etc

Person behaviour= read,write,talk etc
After learning coding,
he acquires new behaviour= can program.
he can also lose old behaviours = sucking thumb, feeding from feeding bottle
acquire new bad habits= doing drugs, gambling etc

lose some tastes = watching cartoons, taking waterbottle to school etc.
So objects acquire and lose behaviours in time. from point a to point b on a time line,
an object may not be the same in terms of the set of attributes and behaviours it has.

Problem
Modelling object behavious and states as it transitions in time is hard using traditional oop.

e.g
for person :

class Object
{
Set<Attributes> attributes ;
Set<Behaviour> behaviours;

protected addAttribute(Attribute at){}
protected removeAttribute(Attribute at){}

protected addBehaviour(Behaviour bh){}
protected removeBehaviour(Behaviour bh){}
}

compare that to:

class Person
{
String name;
int age;
boolean male;
//all these are attributes (states)
public void setAge(int age){}//same as changeAttribute
public int getAge()
... etc
}

In summary.
Oop aint as easy as some textbook try to make it seem. Cuz it's about modelling the world. Physical or imaginary.
The world and the interaction of objects within it aint simple. Nature of objects aint simple.
And programming/describing these things aint so simple. A simple model eventually breaks down when the complexity gets closer to reality.

1 Like 1 Share

(1) (Reply)

Empower Yourself Today,become Professional Web Developer And So On / Why Dont You Learn An It Skill Today Through Our Free Empowertech Training / Trusted And Quality Ponzi Site In 3days.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.