Principles of OOP

Principles of OOP

If you come from a structured-programming background, the OOP value proposition might not be clear yet. After all, the attributes of a person and any logic to retrieve (and convert) those values can be written in C or COBOL. This section clarifies the benefits of the OOP paradigm by explaining its defining principles:encapsulationinheritance, and polymorphism.

Encapsulation

Recall that an object is above all discrete, or self-contained. This characteristic is the principle ofencapsulation at work. Hiding is another term that is sometimes used to express the self-contained, protected nature of objects.

Regardless of terminology, what's important is that the object maintains a boundary between its state and behavior and the outside world. Like objects in the real world, objects used in computer programming have various types of relationships with different categories of objects in the applications that use them.

On the Java platform, you can use access modifiers (which I introduce later in the tutorial) to vary the nature of object relationships from public to private. Public access is wide open, whereas private access means the object's attributes are accessible only within the object itself.

The public/private boundary enforces the object-oriented principle of encapsulation. On the Java platform, you can vary the strength of that boundary on an object-by-object basis, depending on a system of trust. Encapsulation is a powerful feature of the Java language.

Inheritance

In structured programming, it's common to copy a structure, give it a new name, and add or modify the attributes that make the new entity (such as an Account record) different from its original source. Over time, this approach generates a great deal of duplicated code, which can create maintenance issues.

OOP introduces the concept of inheritance, whereby specialized classes — without additional code — can "copy" the attributes and behavior of the source classes that they specialize. If some of those attributes or behaviors need to change, you override them. The only source code you change is the code needed for creating specialized classes. As you know from the "Object-oriented programming concepts" section, the source object is called the parent, and the new specialization is called the child.

Inheritance at work

Suppose you are writing a human-resources application and want to use the Person class as the basis (called the super class) for a new class called Employee. Being the child of PersonEmployee would have all of the attributes of a Person class, along with additional ones, such as:

  • Taxpayer identification number
  • Employee number
  • Salary

Inheritance makes it easy to create the new Employee class without needing to copy all of the Personcode manually.

You'll see plenty of examples of inheritance in Java programming later in the tutorial, especially in Part 2.

Polymorphism

Polymorphism is a harder concept to grasp than encapsulation and inheritance. In essence, it means that objects that belong to the same branch of a hierarchy, when sent the same message (that is, when told to do the same thing), can manifest that behavior differently.

To understand how polymorphism applies to a business-application context, return to the Personexample. Remember telling Person to format its attributes into a String? Polymorphism makes it possible for Person to represent its attributes in a variety of ways depending on the type of Person it is.

Polymorphism is one of the more complex concepts you'll encounter in OOP on the Java platform and is beyond the scope of an introductory tutorial.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值