Java tutorial 1
2015-07-08 18:17
519 查看
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.
example:
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.
example:
The following parent class uses protected access control, to allow its child class overrideopenSpeaker() method:
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.
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.
example:
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.
example:
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.lang.UnsatisfiedLinkError: Native method not found: com.baidu.platform.
- Java面向对象的思想
- Java集合的小抄 Java初学者必备
- javamelody
- java笔记(3)
- Java基础-----反射
- Ubuntu上安装与配置JDK1.8
- 深入理解Java的接口和抽象类
- 《编程导论(Java)·6.2.1 命名空间》
- 使用Spring处理自定义注解
- JAVA 计算地球上任意两点(经纬度)距离
- 10005---Java事务(七) - 分布式事务 - spring + JTA + jotm
- java读取txt中用空格隔开的数字
- Java高级个人笔记(判断List、Map、Set是否为空及效率比较)
- Java高级个人笔记(RandomStringUtils工具类)
- Java基础之Java异常处理机制
- Struts2标签之select的使用方法
- 如何在Java客户端调用RESTful服务
- Spring学习笔记——Spring的自动装配
- Java内存泄露原因详解