Object-Oriented Programming (OOP) Concepts
The Object-Oriented Programming (OOP) concepts are encapsulation, inheritance, and polymorphism.
1. Encapsulation
The main principles of the encapsulation:
- The instance variables should be kept protected (with an access modifier, usually private).
- The instance variable should be accessed only by public accessor methods.
- The accessor methods should follow the JavaBeans naming convention.
2. Inheritance
The inheritance relationships are implemented in Java by extending a class. Java supports only single inheritance - a class cannot extend more than one class, but a class can have multiple ancestors. The two most general reasons to use inheritance are:
- Promoting code reuse means that methods with generic functionality don't have to be reimplemented.
- Using polymorphism
2.1 IS-A Relationship
The IS-A relationship is based on class inheritance or interfaces implementation. In Java, it is expressed using the keywords extends and implements.
2.2 HAS-A Relationship
The HAS-A relationship is based on usage. A class A HAS-A B if code in class A has a reference to an instance of class B.
3. Polymorphism
Polymorphism means "many forms". If a Java object can pass more than one IS-A test, it can be considered polymorphic.
- A reference variable can be of only one type, and that type cannot be changed (but the object it references can change).
- A reference is a variable, so it can be reassigned to other objects.
- A reference variable's type determines the methods that can be called on the object the variable is referencing.
- A reference variable can refer to any object of the same type as the declared reference, or to any subtype of the declared type.
- A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.
- Polymorphic method invocations apply only to instance methods, not to static methods or variables.
Зарегистрируйтесь или войдите, чтобы иметь возможность оставить комментарий.