Three Java object-oriented features, interfaces and abstract summary

Three Java object-oriented features, interfaces and abstract summary

First, the three characteristics of object-oriented

Object-oriented language: is a kind of an object in the form of language as a basic program structure. It is to use language to describe the problem in an object in the form of build up of. It has encapsulation, inheritance and polymorphism these three major characteristics; object is achieved by instantiating the class's implementation; belongs to the Java language is an object-oriented;

Encapsulation:

Serve to protect the package of the class; major classes certain information hide (Private), to prevent direct calls or other types of modifications, resulting in destruction of the state class information; class inside the hidden information setter generally by custom / getter member method to modify and access;

Encapsulation: changing access through private, protected, public modifier, in order to achieve the effect of encapsulation;

  1. private (private) limited access to the current class;
  2. protected (protected) itself is limited to one packet classes and subclasses below accessible;
  3. public (the public) can access any package;

If you did not write modifier, unified default public access modifier;

public class TestOne{
    private String name; //private修饰符定义改属性
    public void setName(name){//通过setName修改name属性
        this.name=name;
    }
    public String showName(){//通过showName访问name属性
        return this.name;
    }
}

Inherited:

Inheritance: implementation inheritance between classes and extends through keyword;

Inheritance mainly to scalability / reuse program that is Java, a common part of the code will be pulled into a parent and then the other subclasses can inherit the parent, then the child would have a parent class features (attributes) and behavior (methods); but do not abuse inheritance, it will improve the coupling of the code is not conducive to code maintenance; so inheritance is double-edged sword; remember that a good project code feature is high cohesion low coupling;

First, the basic inheritance

//定义类AA
public class ClassAA {
    private String name;    //姓名
    private String wType; //职业类型
    public ClassAA(String name,String wType){
        this.name=name;
        this.wType=wType;
    }
    public void showInfo(){
        System.out.println(this.name+"是一名"+this.wType);
    }
}
//定义类A并extends关键字继承ClassAA
public class ClassA extends ClassAA {
    public ClassA(String name,String wType){
        super(name,wType);
    }
    public static void main(String[] args){
        ClassA a_obj=new ClassA("张三","前端开发程序猿");
        a_obj.showInfo();
    }
}

Second, the inheritance structure display and a variety of implementations

Figure I shows the structure of FIG inheritance; inheritance is generally one-way, multiplicity;

  1. Unidirectional: there is a subclass of a parent class, a subclass of the flow direction is from the parent class; after all, his father only son;
  2. Multiplicity: spectrum is similar to family, inheritance can have a parent, a parent (the parent), ... (parent) and other structures;

Figure 2 shows the inheritance of more implementations, while repeating a unidirectional FIG, multiplicity; (Figure 2 from novice tutorial)

This novice tutorial from FIG.

Summary: No multiple inheritance in Java concepts, Java belongs to single inheritance, which is a subclass can have only one parent; but different subclasses can inherit the same parent class. That is, there can be between the brothers and the like.

Polymorphism:

Polymorphism is built on top of inheritance, which means that a parent class method can conduct a variety of different methods or performance derived; in saying: dragon nine sons, nine different child. There is the concept of inheritance and polymorphism; meet polymorphism must meet the "inheritance", "rewrite", "parent child class object reference to" three conditions are indispensable oh; inheritance is not long-winded.

  1. Rewriting: refers to override the superclass method, a method to meet the own characteristics. For example, another phone resolution unanimously, provided inconsistent screen size, screen size information is also required to return, then we need to override the parent class method. (Note: rewrite generally directed to methods, properties will override error); code as follows:
//Phone只设置屏幕分辨率信息
//@Override装饰器表示重写
pulic class Phone{
    private String sInfo; //屏幕信息
    public void showSInfo(){ //显示屏幕信息
        System.out.print("1900x1080分辨率显示屏");
    }
}
//例如:PhoneA屏幕大小时5.0英寸
public class PhoneA extends Phone {
	@Override
    public void showSInfo(){
        String sszie="5.0"; //设置手机屏幕大小
        System.out.print("手机A屏幕大小:"+ssize+";1900x1080分辨率显示屏");
    }
}
//例如:PhoneB屏幕大小时6.0英寸
public class PhoneB extends Phone {
	@Override
    public void showSInfo(){
        String sszie="5.0"; //设置手机屏幕大小
        System.out.print("手机B屏幕大小:"+ssize+";1900x1080分辨率显示屏");
    }
}
  1. Parent class reference subclass object: a "transformation up" manner using the parent class reference type subclass instantiated object; this reference can only call the parent class itself methods and properties, or may call the child class object the method of rewriting the parent class;
//定义父类Parent
public class Parent {
	public void showName() {
		System.out.println("我是父类Parent");
	}
}
//定义子类Child
public class Child {
	public void showInfo() {
		System.out.println("信息查询");
	}
	@Override
	public void showName() {
		System.out.println("数据改变了");
	}
	public static void main(String[] args) {
	    //childObj属于父类Parent类型,所以只能调用父类本身属性和方法
	    //或者调用子类重写的父类方法
	    //不能直接调用当前子类的属性和方法
		Parent childObj=new Child(); //向上转型
		//Child newObj=(Child) childObj; //强制向下转型
		childObj.showName();
		childObj.showInfo(); //导致报错,不能正常编译
	}
}

Second, the difference between abstract and interface

The basic concept of abstract classes

With declared abstract class key or an abstract method can be abstract class (abstract method is not a method thereof). An abstract class is mainly inherited by other classes, some people may doubt that an ordinary class inherits like, why should an abstract class. Abstract class key is "abstract" in object-oriented languages, all of the object is an instance of the class to achieve, but if there is not a specific category of information, it should be abstract it out (abstract class), the class will can not be instantiated, the general abstract class as the base class can not be instantiated (first generation); abstract class which can contain member properties and methods members, what is not inconsistent with other classes; code as follows:

//接口类Person
abstract class Person{
    private String name; //成员属性name
    abstract void showName(); //抽象方法——无方法体
    public void setName(String name){ //成员方法
        this.name=name;
    }
    public String getName(){
        return this.name;
    }
}
//普通类Student继承接口类Person
class Student extends Person{
   @Override
	void showName() {
		// TODO Auto-generated method stub
		System.out.println(this.getName());
	}
	public static void main(String[] args) {
		Two two=new Two();
		two.setName("我是学生");
		two.showName();
	}
}

Abstract class Feature Summary

  1. Abstract class declaration using the keyword, and abstract method comprising (no method body);
  2. An abstract class can have member properties and member methods;
  3. An abstract class can not be instantiated, typically as the base class;
  4. Abstract class inheritance generally used, is not particularly abstract class;
  5. Ordinary class implements the abstract class inherited by the extends keyword, only single inheritance;
  6. Subclass inherits the abstract class must implement the abstract methods;

The basic concept of interface

Use keywords interface modified class is called interface classes, worded as follows:

[public] interface InterFaceName{} //public可以不用写,因为接口默认是public

Then the mouth can contain only class interface method (interface method is no method body), can not have the other members of attributes and member methods, access modifiers attention to the interface class and interface methods can only be public. In order to achieve Java interface is a no multiple inheritance embarrassment, while abstract and general category of behavioral constraints. code show as below:

//接口
interface UserInterface{ //接口类
    void show(); //接口方法showName——无方法体
	default public void setName() { //在java8及以上版本,可以通过default关键字在接口类中实现成员方法
		System.out.println("接口类中default关键字成员方法执行");
	};
}
//普通类UserInfo实现接口类UserInterface
class UserInfo implements UserInterface {
	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.println("重写接口类中接口方法show执行");
	}
	public static void main(String[] args) {
		UserInfo two=new UserInfo();
		two.setName();
		two.show();
	}
}

Interface Class Feature Summary

  1. Interface type declared by the keyword, and comprising an interface method (Method no body);
  2. Interface classes can not have member properties and member methods, but by default keyword can have a member method (limited java8 and above);
  3. General behavior of the interface as normal constraints and abstract class;
  4. By ordinary class implements the interface class implements (achieve) can be separated by commas, multi-interface, multiple inheritance can not solve java embarrassment;

Guess you like

Origin blog.csdn.net/u012475786/article/details/89219894