OOP Principles (Encapsulation and Inheritance)
OOP Principles (Encapsulation and Inheritance)
❖ Public
❖ Protected
❖ Private
❖ Default
Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package and
outside the package.
Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
The principle of encapsulation is that all of an object’s data is contained
and hidden in the object and access to it restricted to methods of that
class.
We can create a fully encapsulated class in java by making all the data
members of the class Private. Then we can use public setter(mutator) and
getter(accessor) methods.
for access control. we need to build setter method for all instance
variable, and find a way to force other code to call the setters rather
than access the data directly.
Advantage of Encapsulation
✓ By providing only a setter or getter method, you can make the class
read-only or write-only. In other words, you can skip the getter or setter
methods.
✓ It provides you the control over the data. Suppose you want to set the
value of id which should be greater than 100 only, you can write the
logic inside the setter method. You can write the logic not to store the
negative numbers in the setter methods.
✓ It is a way to achieve data hiding in Java because other class will not
be able to access the data through the private data members.
✓ The encapsulate class is easy to test. So, it is better for unit testing.
this keyword
field (instance variable) of the class, the field is hidden until the block
terminates execution this is called Shadowing.
✓ If the field is an instance variable, precede its name with this keyword
and a dot(.) as in general form this.x, where x is the instance variable.
public class Motorcycle
{
public int driverIntensity;
public String name;
public void setDriverName(String name){
name= name;
}
The formal parameter name will shadow the instance variable name.
So, in this case we are assigning a variable back to itself.
Solution
Inform the compiler that you want to set the current objects name field to
the incoming name parameter, simply use this to resolve the ambiguity.
public class Motorcycle
{
public int driverIntensity;
public String name;
public void setDriverName(String name){
this.name= name;
}
❖ Constructor Channing
✓ This design is helpful when you have a class that defines multiple
constructors.
✓ A subclass inherits all the variables and methods from its super classes,
including its immediate parent as well as all the ancestors.
✓ We can refer to these inherited methods and variables if they were
declared locally in the class.
✓ A subclass is not a "subset" of a superclass. In contrast, subclass is a
"superset" of a superclass.
✓ It is because a subclass inherits all the variables and methods of the
superclass; in addition, it extends the superclass by providing more
variables and methods.
}
✓ A child constructor is responsible for calling the parent Constructor.
✓ If the child constructor invokes the parent (constructor) by using the
super reference, it must be the first line of code of the constructor.
✓ In a subclass, if we want to use both the superclass version and the
overriding subclass version of a method super key word is used to
invoke the parent version of the method.
✓ This means we don’t want to completely replace the superclass version
rather we want to extend the functionality.
Overriding Methods
✓ Overriding just means that a subclass redefines one of its inherited
methods when it needs to change or extend the behavior of that
method.
✓ Instance variable are not overridden because they don’t define any
special behavior, so a subclass can give an inherited instance
variable any value it chooses.
✓ The new method must have the same signature as the parent’s
method, but can have a different body.
✓ The type of the object executing the method determines which
version of the method is invoked.
parked.message();
dates.message(); // overridden
}
✓ When you call a method on an object Reference, you are calling the
most specific version of the method for that object type. In the other
words, the “lowest one wins” in the inheritance tree.
❖ Final Method
✓ If a method is declared with the final modifier, it cannot be
overridden.
✓ This guarantees that the final method implementation will be used by
all direct and indirect subclasses in the hierarchy.
✓ Methods that are declared private are implicitly final, because it is
not possible to override them in a sub class. This Is the same with
Static methods.
❖ Final Class
✓ Cannot extended to create a subclass. E.g., String Class
❖ Types of Inheritance
1) Single Inheritance: A subclass inherit the features of one super class