- Hands-On Design Patterns with Java
- Dr. Edward Lavieri
- 535字
- 2021-06-24 14:58:00
Being purposeful with inheritance
Inheritance is a powerful OOP construct. We can model objects efficiently, as was illustrated with the bicycle. We know that the following relationships exist:
- Bicycle is a two wheeled
- Bicycle is a vehicle
- Bicycle is a object
When we program inheritance, we should perform the "IS A" Checks using the following pseudo-code logic:
if ( <new child object> is a <parent object> ) then relationship = True; else relationship = False
If our "IS A" Checks fails, then inheritance should be avoided. This is an important test that, with dedicated use, can help ensure that inheritance lines between objects are valid.
Let's create the "IS A" Checks for our Bicycle class. We will do this by using Java's instanceof operator. Here is the code in three sections. The first section runs the checks for myBike6 and checks to see whether it is an instance of Bicycle, TwoWheeled, Vehicle, and Object:
// "IS A" Checks
System.out.println("\n\"IS A\" CHECKS");
// focus on myBike6
Bicycle myBike6 = new Bicycle();
if (myBike6 instanceof Bicycle)
System.out.println("myBike6 Instance of Bicycle: True");
else
System.out.println("myBike6 Instance of Bicycle: False");
if (myBike6 instanceof TwoWheeled)
System.out.println("myBike6 Instance of TwoWheeled: True");
else
System.out.println("myBike6 Instance of TwoWheeled: False");
if (myBike6 instanceof Vehicle)
System.out.println("myBike6 Instance of Vehicle: True");
else
System.out.println("myBike6 Instance of Vehicle: False");
if (myBike6 instanceof Object)
System.out.println("myBike6 Instance of Object: True");
else
System.out.println("myBike6 Instance of Object: False");
The second section runs the checks for myTwoWheeled and checks to see whether it is an instance of Bicycle, TwoWheeled, Vehicle, and Object:
// focus on TwoWheeled
TwoWheeled myTwoWheeled = new TwoWheeled();
if (myTwoWheeled instanceof Bicycle)
System.out.println("\nmyTwoWheeled Instance of Bicycle: True");
else
System.out.println("\nmyTwoWheeled Instance of Bicycle: False");
if (myTwoWheeled instanceof TwoWheeled)
System.out.println("myTwoWheeled Instance of TwoWheeled: True");
else
System.out.println("myTwoWheeled Instance of TwoWheeled: False");
if (myTwoWheeled instanceof Vehicle)
System.out.println("myTwoWheeled Instance of Vehicle: True");
else
System.out.println("myTwoWheeled Instance of Vehicle: False");
if (myTwoWheeled instanceof Object)
System.out.println("myTwoWheeled Instance of Object: True");
else
System.out.println("myTwoWheeled Instance of Object: False");
The third and final section runs the checks for myVehicle and checks to see whether it is an instance of Bicycle, TwoWheeled, Vehicle, and Object:
// focus on Vehicle
Vehicle myVehicle = new Vehicle();
if (myVehicle instanceof Bicycle)
System.out.println("\nmyVehicle Instance of Bicycle: True");
else
System.out.println("\nmyVehicle Instance of Bicycle: False");
if (myVehicle instanceof TwoWheeled)
System.out.println("myVehicle Instance of TwoWheeled: True");
else
System.out.println("myVehicle Instance of TwoWheeled: False");
if (myVehicle instanceof Vehicle)
System.out.println("myVehicle Instance of Vehicle: True");
else
System.out.println("myVehicle Instance of Vehicle: False");
if (myVehicle instanceof Object)
System.out.println("myVehicle Instance of Object: True");
else
System.out.println("myVehicle Instance of Object: False");
The output of the three sections of is a code is provided here. As you can see, the myBike6 object is an instance of Bicycle, TwoWheeled, Vehicle, and Object; the myTwoWheeled object is an instance of TwoWheeled, Vehicle, and Object; and the myVehicle object is an instance of Vehicle and Object. We can also see that the myTwoWheeled object is not an instance of Vehicle or Object; and that the myVehicle object is not an instance of Bicycle or TwoWheeled:

The preceding screenshot depicts this example.
- 計(jì)算機(jī)綜合設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- Mastering Ninject for Dependency Injection
- 大數(shù)據(jù)導(dǎo)論
- 云計(jì)算服務(wù)保障體系
- 大數(shù)據(jù)架構(gòu)和算法實(shí)現(xiàn)之路:電商系統(tǒng)的技術(shù)實(shí)戰(zhàn)
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- 大數(shù)據(jù)治理與安全:從理論到開源實(shí)踐
- 大數(shù)據(jù)分析:數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)
- 大數(shù)據(jù)時(shí)代系列(套裝9冊(cè))
- 大數(shù)據(jù)技術(shù)體系詳解:原理、架構(gòu)與實(shí)踐
- 云原生架構(gòu):從技術(shù)演進(jìn)到最佳實(shí)踐
- 數(shù)據(jù)庫技術(shù)與應(yīng)用:SQL Server 2008
- Hadoop與大數(shù)據(jù)挖掘
- 數(shù)據(jù)庫應(yīng)用技術(shù)