ZFGC
Resources => Coding => Topic started by: AoDC on February 23, 2008, 11:10:05 am
-
Could somebody please explain the difference? I've read alot on it and still do not fully understand.
Java EG (excuse any mistakes, I'm tired):
somewhere in main method, I'd have
Animal dog = new Animal(4,"Bob");
which creates object from:
static class Animal{
byte legs = 4;
String name = "";
Animal(){}
Animal(byte Legs,String Name){
legs = Legs;
name = Name;
}
}
This is what I've always done in OOP (in Java at least). What have I done exactly? Is it wrong? I know I've created an object of Animal and constructors blah but yes. Can somebody please explain?
-
As I understand it, encapsulation is restricting direct access to the variables in an object via a function. Inheritance is when an object inherits functions and what-not from another object.
So,
class person : public animal
is inheritance (C++).
And
class person
{
public bool GetIsAlive(){return m_bAlive;}
private bool m_bAlive;
};
is encapsulation and generally considered good practice (C++).
I may be wrong though.
-
First google result; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/107576 :D, basic explanation.
Anyhow, basically inheritence refers to deriviving a class from another class and the dirived class being given acccess to all its internal members and methods of the base class. Encapsulation is where, when you inherit the class the dirived class only get access to what you should require, thus stoping the dirived class from, say modifiying internal variables that may break methods of the base class.
Hope you understand.
As for your code, best advice is to make those variables private :). Its horribly bad OOP practice to allow external modification of classes members.
-
First google result; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/107576 :D, basic explanation.
Encapsulation is where, when you inherit the class the dirived class only get access to what you should require, thus stoping the dirived class from, say modifiying internal variables that may break methods of the base class.
protected scope, right?
-
First google result; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/107576 :D, basic explanation.
Encapsulation is where, when you inherit the class the dirived class only get access to what you should require, thus stoping the dirived class from, say modifiying internal variables that may break methods of the base class.
protected scope, right?
Essentially yeh.
-
As for your code, best advice is to make those variables private :). Its horribly bad OOP practice to allow external modification of classes members.
Unless that's how the class/struct was designed.
I've written some pretty extensive vector/matrix classes, and would be quite pissy if I couldn't get at the individual components as easy as vec.x, vec.z, matrix.x_axis, etc.
-
As for your code, best advice is to make those variables private :). Its horribly bad OOP practice to allow external modification of classes members.
Unless that's how the class/struct was designed.
I've written some pretty extensive vector/matrix classes, and would be quite pissy if I couldn't get at the individual components as easy as vec.x, vec.z, matrix.x_axis, etc.
Thats true enough, but in the majority of cases its usually bad practice.