Java tutorial 1

转载 2015年07月08日 18:17:02

1. Enums

Enums were introduced in java 5.0. Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums.

With the use of enums it is possible to reduce the number of bugs in your code.

For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium and large. This would make sure that it would not allow anyone to order any size other than the small, medium or large.


THE output:

2. Inheritance

In Java, classes can be derived from classes. Basically if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive your new class from the already existing code.

This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario the existing class is called the superclass and the derived class is called the subclass.

3. Interfaces

In Java language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance.

An interface defines the methods, a deriving class(subclass) should use. But the implementation of the methods is totally up to the subclass.

4. Java access modifiers

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

  • Visible to the package. the default. No modifiers are needed.

  • Visible to the class only (private).

  • Visible to the world (public).

  • Visible to the package and all subclasses (protected)

Private and Public are easy to understand. For protected:

Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.


The following parent class uses protected access control, to allow its child class overrideopenSpeaker() method:

class AudioPlayer {
   protected boolean openSpeaker(Speaker sp) {
      // implementation details

class StreamingAudioPlayer {
   boolean openSpeaker(Speaker sp) {
      // implementation details

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, thats why we used protected modifier.

The following rules for inherited methods are enforced:

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

  • Methods declared without access control (no modifier was used) can be declared more private in subclasses.

  • Methods declared private are not inherited at all, so there is no rule for them.


Java NIO Tutorial 1- Introduction

Java NIO Tutorial 1- Introduction Java NIO (New IO) is an alternative IO API for Java (from Java 1.4...

Groovy Tutorial for Java Developers – Part 1: The Basics

Groovy is an object-oriented programming language which could be described as Java paired with a dyn...

JAVA JDBC Tutorial(1)

JDBC is a platform-independent interface between relational databases and Java. In today’s Java worl...

java3d tutorial chapter 1

  • 2015-07-15 13:26
  • 227KB
  • 下载

Step-by-Step SqlAlchemy Tutorial(part 1 of 2)

一直觉得orm很高深,今天时间比较充分打算学习下 安装说一点,我的系统是win7 64位家庭普通版 用easy_install安装sqlalchemy出现问题,于是下载最新版0.7.6的zip压缩...

numpy module 1 -- Numpy tutorial

0. Numpy Numpy is a Python library that provides a multidimensional array object, various derived...