13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.4 Encapsulati<strong>on</strong>, Inheritance, and Polymorphism85is reminiscent of the C/C++ include preprocessor directive, their functi<strong>on</strong> isnot the same. An import statement does not include any new code; it <strong>on</strong>ly aidsname resoluti<strong>on</strong>. Sec<strong>on</strong>dly, the “*” can <strong>on</strong>ly be used at the end of the packagename; it is not a true wildcard in the regular expressi<strong>on</strong> sense of the word.Thirdly, every class has what is in effect an implicit import java.lang.* sothat you d<strong>on</strong>’t need to put <strong>on</strong>e there. References to String or System or othercore language classes can be made without the need for either the importstatement or the fully qualified name (except as described in the nextparagraph).If you need to use two different classes that have the same name but comefrom different packages, you will still need to refer to them by their full names;import can’t help you here. As an example of this, c<strong>on</strong>sider the two classesjava.util.Date and java.sql.Date (though with any luck you w<strong>on</strong>’t needto refer to both of them within the same class).3.4ENCAPSULATION, INHERITANCE, AND POLYMORPHISMThe classic troika of OOP buzzwords is “encapsulati<strong>on</strong>, inheritance, andpolymorphism.” How does Java do each of these things?3.4.1 Encapsulati<strong>on</strong>Encapsulati<strong>on</strong> is the grouping of data and algorithms together into units, andit’s also about hiding implementati<strong>on</strong> details that are not important to the usersof the unit. The basic unit of encapsulati<strong>on</strong> is the class. All Java code exists inclasses. A class is declared with the class keyword (Example 3.24).Example 3.24 A sample Java class declarati<strong>on</strong> that doesn’t actually do anything usefulpublic classSample{private int id;}public void method(){System.out.println(id);}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!