4 Replies - 608 Views - Last Post: 02 October 2011 - 08:33 PM Rate Topic: -----

#1 ajiska  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-August 11

Encapsulation and Inheritance

Posted 01 October 2011 - 11:14 AM

Why we use encapsulation and inheritance in a java code?
I know that inheritance reduce the number of lines in a code.But i want to know more advantages of using this two OOP Principle.Explain with points.
:rolleyes: :rolleyes: :rolleyes:

Is This A Good Question/Topic? 0
  • +

Replies To: Encapsulation and Inheritance

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Encapsulation and Inheritance

Posted 01 October 2011 - 11:33 AM

Inheritance is not only to reduce the code, but also allow you to define common things for a type such that every class inherit that one will be required to follow those orders. For example I know all animals eat(), drink().. So if I define these common behavior in super class I can have all subs to follow the style(same names for methods, same return types and so on) so my code will be well organized.

Another thing is re usability, means I can add any subclass any times without have to know it while developing the super one. This make my code re usable and I can manage my sub classes

Also maintainability is easy, if I need to change the behavior of all animals which is defined in super class, I just change the super class code and all sub classes get the changes.

Also other things like flexibility of code, which means code can be easily changed and also the logic of tree view of inheritance is easy to be understood and developed.

And encapsulation, is to make sure instance code are accessed by those who should do and in organized order. Also help in secure your product
Was This Post Helpful? 1
  • +
  • -

#3 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Encapsulation and Inheritance

Posted 01 October 2011 - 11:34 AM

So. I'm gonna answer this in a way that can't be used to answer a homework question.

Encapsulation means making all data members of a class private or protected, so that only the class itself or it's children can change the data. Why would you want only the class or it's children to have this ability?

Inheritance means that children of a given class have access to all of the "stuff" in the parent class, why would you need that in OOP?
Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10340
  • View blog
  • Posts: 38,264
  • Joined: 27-December 08

Re: Encapsulation and Inheritance

Posted 01 October 2011 - 09:23 PM

The whole idea behind encapsulation is that you can use something without knowing how it works. You use Integer.parseInt(). You know what it accepts and what it returns. Do you really care how the method returns the result? Not really. Same with any class you use- you know if works without knowing how it works.

I don't really have anything to add to smohd's explanation on inheritance. :)

Supplementary Reading:
japanir's tutorial on Abstract Classes vs. Inheritance
My tutorial on Encapsulation
Was This Post Helpful? 2
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Encapsulation and Inheritance

Posted 02 October 2011 - 08:33 PM

View Postsmohd, on 01 October 2011 - 02:33 PM, said:

Another thing is re usability, means I can add any subclass any times without have to know it while developing the super one. This make my code re usable and I can manage my sub classes

Also maintainability is easy, if I need to change the behavior of all animals which is defined in super class, I just change the super class code and all sub classes get the changes.

Don' really agree with that. You shouldn't really know how your super class behave
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1