Java object-oriented-abstract classes and interfaces
Use of the 1.Java final keyword
Package edu.tongji.fni;/*final keyword * 1, the final keyword in Java is called the end device, which means the final meaning * 2, Final can declare classes, methods, properties: * Classes that use Final declaration cannot be inherited * Methods that use the final declaration cannot be overridden * Use final declared variables to become constants, constants that cannot be modified */class people{ //The class preceded by final will error public void tell () {}}class Student extends People{public void Tell () {}}public class FinalDemo01 {public static void main (string[] args) { String Name= "Jikexueyuan";//Add final after the next sentence will be error name= "Www.jikexueyuan";}}
2.java Abstract class
Package edu.tongji.fni;/* Abstract class * 1, abstract class Concept: * The class that contains an abstract method is abstract class * 2, Abstract method: A method declared and not implemented, the abstract method must use the abstract keyword Declaration * 3, abstract class quilt class inheritance, Subclasses (if not abstract classes) must override all abstract methods in the class * 4, define the format: * Abstract class classname{* Property * Method * Abstraction Method *} * 5, abstract class cannot be directly instantiated */abstract class Abs{pri vate int age;public void Tell () {}//abstract method public abstract void Say ();p ublic abstract void print ();p ublic int getage () {return Age;} public void Setage (int.) {this.age = age;}} Class Absdemo extends Abs{public void Say () { //3.system.out.println (this.getage ());} public void print () { //3.}} public class AbsDemo01 {public static void main (string[] args) {//abs a=new Abs ();//5, abstract class cannot be directly instantiated Absdemo a=new Absdemo (); a . Setage (+); A.say (); A.print ();}}
3. Implementation of Java interface
Package edu.tongji.fni;/* Interface * 1, interface is the most important concept in Java, interface can be understood as a special class, which is all composed of global constants and public abstract methods of * 2, the format of the interface: * interface interfacename{* Global Variable * Abstract method *} * 3, the implementation of the interface must also be through the subclass, using the keyword implements, and the interface is multi-implementation of * 4, a subclass can inherit the abstract class and implementation interface * 5, an interface cannot inherit an abstract class, but can be To inherit multiple interfaces at the same time through the extends keyword, implement multiple inheritance of interfaces */interface inter1{public static final int age=100;//common global constants (constants to uppercase) public abstract void Tell ();//public abstract method}interface Inter2{public abstract void Say ();} Abstract class Abs1{public abstract void Print ();} Class A extends ABS1 implements inter1,inter2{//3, 4,//method to override interface @overridepublic void Tell () {}public void Say () {}public vo ID print () {}}interface Inter3 extends inter1,inter2{ //5, to compensate for the lack of single inheritance in Java}public class InterDemo01 {public static void Main (string[] args) {//inter i=new Inter ();//interface cannot be directly used a a=new a (); A.tell (); System.out.println (Inter1.age); A.say ();}}
Similarities and differences between interfaces and abstract classes
The core idea of Java object-oriented