Here, I learned that sub-classing violates encapsulation:
it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled
For instance, If we consider below Java syntax, a class has access to all the fields and methods of its super class provided they are not declared private.
class Person{
private String name;
String address;
//methods of class Person
}
class Employee extends Person{
float salary;
private Employee immedateManager;
private raiseSalary(float raise){...}
}
class Manager extends Employee{
String department;
void test(){
name = "Fred"; //compiler error, could not break encapsulation
salary = 70000.00f;
address = "somewhere";
immediateManager = "Slate"; //compiler error, could not break encapsulation
raiseSalary(1234.0); //compiler error, could not break encapsulation
}
}
So, if the representation part of type abstraction (Java class) is declared private, then how does one understand before saying that, sub-classing violate encapsulation?
Caras well as aVolkswagen). Therefore, all the use that herbie makes of thefuel_tank_volumevalue is OK, because it's from within theCarclass and not from without.