Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Encapsulation vs Inheritance  (Read 1545 times)

0 Members and 1 Guest are viewing this topic.
Encapsulation vs Inheritance
« on: February 23, 2008, 11:10:05 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
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

Code: [Select]
Animal dog = new Animal(4,"Bob");
which creates object from:

Code: [Select]
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?
Logged
the a o d c
Re: Encapsulation vs Inheritance
« Reply #1 on: February 23, 2008, 11:27:00 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
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,
Code: [Select]
class person : public animalis inheritance (C++).

And
Code: [Select]
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.
« Last Edit: February 23, 2008, 11:30:24 am by TheDarkJay »
Logged
Re: Encapsulation vs Inheritance
« Reply #2 on: February 23, 2008, 02:35:23 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
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.
« Last Edit: February 23, 2008, 02:38:06 pm by Infinitus »
Logged
Re: Encapsulation vs Inheritance
« Reply #3 on: February 23, 2008, 10:09:53 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
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?
Logged



i love big weenies and i cannot lie
Re: Encapsulation vs Inheritance
« Reply #4 on: February 24, 2008, 12:38:48 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
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.
Logged
Re: Encapsulation vs Inheritance
« Reply #5 on: February 25, 2008, 11:52:11 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
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.
Logged
  • My Myspace?
Re: Encapsulation vs Inheritance
« Reply #6 on: February 26, 2008, 12:05:43 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
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.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.237 seconds with 48 queries.