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 Tutorial中文版 开篇综述】 Oracle(甲骨文)公司Java最新官方教程【译文】

此篇为我博客【Java Tutorial】系列开篇综述。JavaTutorial是Oracle公司的官方教学教程,内容从认识Java到如何开发Java有着非常系统的体系,而且叙述清楚明了,非常适合Ja...
  • RuingMan
  • RuingMan
  • 2016年02月04日 17:28
  • 4819

【Java Tutorial】: Getting Started(开始入门)【官方原版教程】

入门: 这个页面内容提供了你需要了解并开始使用Java编程语言的一切。
  • RuingMan
  • RuingMan
  • 2016年02月05日 17:58
  • 1724

【Java Tutorial】: The Java Technology Phenomenon(Java技术现象)【官方原版教程】

  • RuingMan
  • RuingMan
  • 2016年02月04日 17:07
  • 908

The Java EE 6 Tutorial 中文版 (翻译) 第32章 JPA简介 (一) 实体 第六部分 持久化 第六部分讲述了Java持久化API (JPA),本部分主要包括下述各章: ■     ...
  • wea56567
  • wea56567
  • 2013年06月20日 11:35
  • 1579

Tensorflow - Tutorial (1) : 线性回归

Tensorflow-Tutorial (1) : 线性回归
  • u010089444
  • u010089444
  • 2016年09月09日 11:53
  • 1605

Coursera Deep Learning 第四课 卷积神经网络 第二周 编程练习 Keras - Tutorial - Happy House v2

Coursera Deep Learning 第四课 卷积神经网络 第二周 编程练习 Keras - Tutorial - Happy House v2...
  • justry24
  • justry24
  • 2017年11月17日 09:17
  • 1202

JAVA EE 7 SDK Tutorial分析

随着Java平台企业版(Java EE),Java企业应用程序的开发从未如此简单或更快。在Java EE 7平台的目的是向开发人员提供了一套强大的API,同时缩短开发时间,降低了应用的复杂性,并提高应...
  • pangjiuzala
  • pangjiuzala
  • 2016年01月02日 13:59
  • 2845

Java Tutorial 1

 Java is a OOP languageobject is a self-contained entity which has its own private collection of pro...
  • ivanna
  • ivanna
  • 2009年11月03日 12:11
  • 217

JAVA Array Tutorial(1)

An array is a named list of data items that all have the same type. You declare an array variable in...
  • zmycoco2
  • zmycoco2
  • 2013年06月23日 13:09
  • 1070

JAVA JDBC Tutorial(1)

JDBC is a platform-independent interface between relational databases and Java. In today’s Java worl...
  • zmycoco2
  • zmycoco2
  • 2013年06月25日 10:49
  • 1147
您举报文章:Java tutorial 1