Wednesday 31 July 2013

03: abstraction, encapsulation, and inheritance



Learning Objectives
After completing this session, you will be able to:
  Describe abstraction, encapsulation, and inheritance
Abstraction
One of the chief advantages of object oriented programming is the idea that programmers can
essentially focus on the “big picture” and ignore specific details regarding the inner-workings of an
object. This concept is called abstraction.
Encapsulation
  Abstraction in OOP is closely related to a concept called encapsulation.
  The Object Orientation has two major promises/benefits. They are: flexibility and
maintainability.
  You have to write your classes and code in a way that supports flexibility and
maintainability.
  The ability to make changes in your implementation code without breaking the code of
others who use your code is a key benefit of encapsulation.
  If you want maintainability, flexibility and extensibility your design must include
encapsulation.
  The following are some of the ways to include encapsulation:
oKeep instance variables protected (with an access modifier, often private).
oMake public accessor methods, and force calling code to use those methods
rather than directly accessing the instance variable.
oFor the methods, use the JavaBeans naming convention of
set<someProperty> and get<someProperty>
  Encapsulation is the mechanism that binds together the code and the data it
manipulates, and keeps both safe from outside interference and misuse.




Encapsulation: Example
Bank machine
Hidden data
Account balance
Personal information
Interface
Deposit, Withdraw, Transfer
Display account information
Class Hierarchy
Classes are arranged in a tree hierarchy:
  A class’ “superclass” is the class preceding it in the hierarchy
  Classes following it are “subclasses”
Classes have all the properties of their superclasses:
General:Towards the root (top)
More specific:Towards the leaves (down)
NB:In Computer Science trees grow upside down!
















Inheritance
One of the main tenets of OOP is inheritance.
The process by which a class inherits the properties of its superclasses is called inheritance:
  Methods
  Instance variables
A child class inherits its properties and attributes from its parents.
Inheritance is the process by which one object acquires the properties of another object. By use of
inheritance, an object needs only to define all of its characteristics that make it unique within its
class; it can inherit its general attributes from its parent.
The inheriting class contains all the attributes and behaviors of its parent class. Moreover the
inheriting class can define its own attributes and behaviors.

The inheriting class can override the definition of existing methods by providing its own
implementation.
The code of the inheriting class consists only the changes and additions to the base class.
Need of inheritance
Inheritance is required for the following reasons:
Modular coding, which means less code and easier to get an idea about the code
  Code reuse:
  Do not break what is already working
  Easier updates


Tips and Tricks
1.The typical problems include real-time problems and challenges:
2.Are there any practical limits on the level of subclassing using Java?
3.Can you extend any class in Java?
Solution:With regard to Java API, there is no hard limit for the limits on the level of subclassing.
Using Java, you cannot extend any class as such. The factors like access control and access
modifier determine whether a class can be subclassed.
Summary
A class can inherit instance variables and methods from a more abstract superclass.



No comments:

Post a Comment