Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 3 Java Class Design

A.Compiler error: attempting to assign weaker access privileges; toString was public in Object.

B.Compiles without errors, and when run, it prints the following: The color is: red = 10 green = 10 blue = 10.

C.Compiles without errors, and when run, it prints the following: The color is: red = 0 green = 0 blue = 0.

D.Compiles without errors, and when run, it throws ClassCastException.

Answer: A. Compiler error: attempting to assign weaker access privileges; toString was public in Object.

(No access modifier is specified for the toString() method. Object's toString() method has a public access modifier; you cannot reduce the visibility of the method. Hence, it will result in a compiler error).

Summary

Foundations of OOP

Encapsulation: Combining data and the functions operating on it as a single unit.

Abstraction: Hiding lower-level details and exposing only the essential and relevant details to the users.

Inheritance: Creating hierarchical relationships between related classes.

Polymorphism: Interpreting the same message (i.e., method call) with different meanings depending on the context.

Class Foundations

A “class” is a template (or blueprint) and an “object” is an instance of a class.

A constructor does not have a return type.

You cannot access the private methods of the base class in the derived class.

You can access the protected method either from a class in the same package (just like package private or default) as well as from a derived class.

You can also access a method with a default access modifier if it is in the same package.

You can access public methods of a class from any other class.

Overloading

Method overloading: Creating methods with same name but different types and/or numbers of parameters.

You can have overloaded constructors. You can call a constructor of the same class in another constructor using the this keyword.

Overload resolution is the process by which the compiler looks to resolve a call when overloaded definitions of a method are available.

84

Chapter 3 Java Class Design

Inheritance

Inheritance is also called an “is-a” relationship.

Resolving a method call based on the dynamic type of the object is referred to as runtime polymorphism.

In overriding, the name of the method, number of arguments, types of arguments, and return type should match exactly.

In covariant return types, you can provide the derived class of the return type in the overriding method.

You use the super keyword to call base class methods.

Overloading is an example of static polymorphism (early binding) while overriding is an example of dynamic polymorphism (late binding).

You don’t need to do an explicit cast for doing an upcast. An upcast will always succeed.

You need to do an explicit cast for doing a downcast. A downcast may fail. So you can use the instanceof operator to see if a downcast is valid.

Java Packages

A package is a scoping construct to categorize your classes and to provide namespace management.

85

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]