15 [Instructor] Java Essentials ch07
| access controller
private within the same class
(default) Within the same package
protected Within the same package, child classes of other packages can be accessed
public No access restrictions, all
In front of the class is public, (default)
Anything before a member, including void, is allowed.
| encapsulation
Use access controllers to protect data from the outside
Direct access is blocked, but indirect access is allowed through methods.
class Time {
private int hour;
private int minute;
private int second;
public void setHour(int hour) { //Method provided
if( isNotVaildHour(hour) ) return;
this.hour = hour ;
}
//Method that checks and informs whether the hour passed as a parameter is valid
//Methods that are only used internally are made private.
private boolean isNotVaildHour(int hour) {
return hour 23;
}
public int getHour() {return hour;};
}
public class TimeTest {
public static void main(String[] args) {
Time t = new Time();
t.hour = 100;
t.setHour(21);
System.our.println(t.getHour);
}
}
//Narrow the access controller scope as much as possible
| polymorphism
Handling descendant type objects with ancestor type reference variables
//The opposite case is not allowed
Tv t = new SmartTv(); //Available even with type mismatch
The number of available members varies depending on whether the reference variable is an ancestor or descendant.
| Type conversion of reference variables
Controlling the number of members that can be used
//Conversion of basic type: value changes
Reference variables in ancestor and descendant relationships can be converted to each other.
FrieEngine f = new FireEngine();
Car c = (Car)f; //Type conversion can be omitted by reducing members from descendant to ancestor type, safe
FireEngine f2 = (FireEngine)c; //Type conversion cannot be omitted by increasing members from ancestors to descendants
Ambulance a = (Ambulance)f; //error
public static void main(String args[]) {
Car c = new Car();
FireEngine fe = new FireEngine(); //It matters what the actual instance is
FireEngine fe2 = (FireEngine)c; //copy what c points to fe2, compiler says OK, actual type conversion error
Car car2 = (Car)fe2;
car2.drive(); //In the end, a NullPointerException occurs because the value stored in car2 is null.
| instanceof operator
Used to check whether reference variable type conversion is possible; prior confirmation is required.
void doWork(Car c) {
if ( c instanceof FireEngine) {
FireEngine fe = (FireEngine)c;
fe.water();
else if ( c instanceof Ambulnce) { .... }
FireEngine → Car → Object
FireEngine fe = new FireEngine ();
//all true
System.out.println(fe instanceof Object);
System.out.println(fe instanceof Car);
System.out.println(fe instanceof FireEngine);
| Polymorphism of parameters
Advantages of polymorphism: polymorphic parameters, handling multiple types of objects in one array
Tv t = new SmartTv();
Type conversion of reference variables: Controlling the number of available members
Check whether instanceof operator type conversion is possible
When calling a method, a reference type parameter can pass an instance of the same type or descendant type.
class Product {
int price;
int bonusPoint;
}
class Tv extends Product { }
class Computer extends Product { }
class Audio extends Product { }
void buy(Product p) { //You can point to descendant objects as ancestors
money -= p.price;
bonusPoint += p.bonusPoint;
}
Buyer b = new Buyer();
Tv tv = new Tv();
Computer com = new Computer();
b.buy(tv);
b.buy(com);
class Product {
int price;
int bonusPoint;
Product(int price) {
this.price = price;
bonusPoint = (int)(price/10.0);
}
}
class Tv1 extends Product {
Tv1() {
super(100);
}
public String toString() {return "Tv"; }
}
class Computer extends Product {
Computer() { super(200); }
public String toString() { return "Computer"; }
}
class Buyer {
int money = 1000;
int bonusPoint = 0;
void buy (Product p) {
if(money < p.price) {
System.out.println("You cannot purchase items due to insufficient balance.");
return;
}
money -= p.price;
bonusPoint += p.bonusPoint;
System.out.println(p + "You purchased");
}
}
class Ex 7_8 {
public static void main(String args[]) {
Buyer b = new Buyer();
b.buy(new Tv1());
b.buy(new Computer());
System.out.println("Currently the remaining money is " + b.money + "10,000 won.");
System.out.println("Current bonus points are " + b.bonusPoint + " points.");
}
}
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going