Lecture 04 - Inheritance
Lecture 04 - Inheritance
// setters and getters to be defined for x and y … // setters and getters to be defined for center and radius …
} }
int firstData; // visible in this class and in the other parts of the package my_pack_folder
protected int secondData; // visible in this class, in the subclasses of this class and in the package my_pack_folder
// filename: TheParent.java
Object Oriented Programming: Inheritance
package my_pack_folder;
// filename: TheChild.java
Object Oriented Programming: Inheritance
package my_pack_folder;
theReference.fourthData = 4; // is it accessible? NO
anotherReference.fourthData = 40; // NO
} // filename: TheUnrelated.java
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Nested Classes
Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Object Oriented Programming: Nested Classes
● Why do we use nested classes?
○ Compelling reasons for using nested classes include the following:
■ It is a way of logically grouping classes that are only used in one place: If a class is useful to
only one other class, then it is logical to embed it in that class and keep the two together.
Nesting such "helper classes" makes their package more streamlined.
■ It increases encapsulation: Consider two top-level classes, A and B, where B needs access to
members of A that would otherwise be declared private. By hiding class B within class A, A's
members can be declared private and B can access them. In addition, B itself can be hidden
from the outside world.
■ It can lead to more readable and maintainable code: Nesting small classes within top-level
classes places the code closer to where it is used.
Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Object Oriented Programming: Inheritance