Day 09Java Basic Learning Notes

Source: Internet
Author: User

Abstract abstract classes and abstract methods must be decorated with the abstract keyword
    • Abstract class Format

      abstract class 类名{}
    • Abstract method definition, plus the abstract keyword before returning a value, or modifier
    • Method has no method body, that is: no {}

      abstract public void eat();public abstract void eat();
    • Abstract classes do not necessarily have abstract methods, classes with abstract methods must be abstract classes

    • Abstract classes can have member variables and non-abstract member methods entirely
Subclass of abstract class--using extends
    • is either an abstract class or an abstract method that does not fully implement its parent class
    • Either rewrite all the abstract methods in the abstract class, which is a complete implementation class, so it can be used to instantiate an object.
Illegal combination of abstract class, method definition
    • The private abstract method relies on subclasses to implement, but the proprietary method cannot be accessed outside the class and cannot be implemented
    • The final abstractfinal modification method cannot be overridden, and the abstract method is a subclass implementation, a contradiction
    • Static abstract abstract methods in this class there is no method body, the use of the class name is called when the method is called no method body, no effect
Characteristics of abstract class members
    1. Member variable: can be either a variable or a constant
    2. Construction method: There, initialization for subclass access to abstract class data
    3. Member methods: Either abstract or non-abstract
Attributes of abstract class member methods
    • The abstract method enforces what subclasses do
    • The non-abstract method subclass inherits things and improves the reusability of the Code
Abstract class Practice case: Animal--Cat dog
    Specific things: Cat, dog commonness: Name, age, eat have commonalities, so you can extract the parent class, but because the subclass of the method "eat" There are different implementations, so, the method "eat" is defined as an abstract method, so the parent class is also an abstract class, the implementation of the parent class will need to implement their own unique "eat" method, only    is a class that can be instantiated, or it is an abstract class.           Define abstract Animal class public abstract class Animaldemo {//Private member variable private String name;        private int age; public abstract void Eat ();        Abstract method//Common Get/set method public String GetName () {return name;        } public void SetName (String name) {this.name=name;        } public int Getage () {return age;        } public void Setage (int age) {this.age=age; }//Null parameter construction method public Animaldemo () {}//Parameter construction method public Animaldemo (String Name,int age) {T            His.age=age;        This.name=name;        }}//Inherit the parent class and implement the parent class abstract method public class Dogdemo extends Animaldemo {//NULL parameter construction method public Dogdemo () {} Two parameters are constructed by public Dogdemo (String Name,int Age) {//Call the parent class with a parameter constructor method, and pass a parameter super (nAme,age);        }//overriding parent class abstract method public void Eat () {System.out.println ("Dog eat"); }}//Inherit the parent class and implement the parent class abstract method public class Catdemo extends Animaldemo {public Catdemo () {} public Catdemo (String name, int age)        {super (name,age);        } public void Eat () {System.out.println ("cat eat"); }} Test class public class Demotest {public static void main (string[] args) {Animaldemo ad = new do            Gdemo ("Tom", 2);//Use polymorphic parent reference to refer to subclass Object Ad.eat ();            System.out.println ("Name:" +ad.getname () + "" + "Age:" +ad.getage ());            Animaldemo aadd = new Catdemo ("Jack", 3);            Aadd.eat ();        System.out.println ("Name:" +aadd.getname () + "" + "Age:" +aadd.getage ()); }    }
The introduction of interface interface abstract class is a template extracted from multiple classes, if the abstraction is more thorough, you can extract a more special "abstract class"-interface, the interface can not contain ordinary methods in the interface all methods are abstract methods, where the member variables are all constants. Interface Features
    • The interface is represented by the keyword interface

      格式:interface 接口名 {} //不能使用class关键字类实现结构用 implements关键字
    • Format: Class name implements interface name {}

      接口没有构造方法,即:接口不能实例化
    • According to the polymorphic way, there is a specific subclass instantiation, in fact, this is a polymorphic, interface polymorphism
Sub-class of the interface
    • Can be an abstract class, with the Implements keyword
    • Or an interface that uses the extends keyword
    • is an implementation class that implements all the abstract methods in the interface, using the Implements keyword
Summary

Only interfaces and interfaces are used between the extends keyword, the abstract class and the interface, and the Implements keyword that is used between the implementation class and the interface.

Features of Interface members
    • Member variables

    • can only be constants

    • The default modifier public, static, final, either of which can be omitted, or the province may be slightly

    • Construction method

No, because the interface is primarily extended, and no specific presence

    • Member Methods
    • Can only be an abstract method
    • Default modifier public, abstract, can omit one or omit all
Class-To-Class, class-to-interface, interface-to-interface relationships
    • General class and Ordinary class--extends

      Inheritance can be single-inheritance, but multiple-tier inheritance

    • General class and abstract class--extends

      Inheritance, can be single-inheritance, but multi-tier inheritance

    • Common classes and interfaces, abstract classes and Interfaces--implements

      Implementation of the relationship, can be single implementation, can also be implemented, but also in the inheritance of a class at the same time want to multiple interfaces, it should be noted that extends to write in front, implements write in the back

    • Interface and Interface--extends

      Inheritance, can be single-inheritance, or multiple inheritance

Keywords that are used when defining the difference between an abstract class and an interface
    • Abstract class Abstraction
    • Interface interface
Member Differences
    • Abstract classes: Variable constants can have, abstract methods, non-abstract methods can have, or can have a construction method
    • Interfaces: only constants; abstract methods; no construction method
Relationship Differences
    • Classes and classes: integration, single inheritance, multi-tier inheritance
    • Common classes and interfaces: Implementing single implementations or multiple implementations implements
    • Abstract classes and interfaces: implementation, single implementation, multi-implementation abstract Class A implements Ia,ib
    • Common classes and Abstract classes: inheritance, and implementation of all abstract methods extends
    • Interfaces and interfaces: inheritance, single inheritance, multiple inheritance extends
In the interface of the various relationships, only the interface and interface is an inheritance relationship, the rest and the interface are all implemented relationship interface exercises
    /* Cat dog case, add extra function Analysis of high jump: from concrete to abstract Cat: name, age to eat, sleeping dog: Name, age to eat, sleep because of the commonality function, so, we extract a parent class: Animals: Name, age to eat                ();            Sleeping () {} Cat: Inherit from Animal dog: The Extra function inherited from animal high jump is a new extension function, so we want to define an interface interface: High jump part cat: Realizing high jump        Some dogs: realization of high jump; From abstraction to concrete use: Use specific classes *///define high Jump interface public interface jumps {    public abstract void jump ();        } public class Dog extends Animal {public void Eat () {System.out.println ("Dog chew Bones");        Public Dog () {} public dog (String Name,int age) {super (name,age);        }} public class Cat extends Animal {public void Eat () {System.out.println ("cat Catching Mouse");        Public cat () {} public cat (String Name,int age) {super (name,age); }} public class JumpdoG extends Dog implements jump{public Jumpdog () {} public Jumpdog (String Name,int age) {Super (name        , age);        } public void jumps () {System.out.println ("Dogs can Jump"); }} public class Jumpcat extends Cat implements jump {public jumpcat () {} public jumpcat (String name,i        NT age) {super (name,age);        } public void jumps () {System.out.println ("cat can Jump"); }}//test class public class Jumptest {public static void main (string[] args) {Animal a = new Cat (            "Tom", 2);            System.out.println ("Name:" +a.getname () + "" + "Age:" +a.getage ());            A.eat ();            A.sleep ();            System.out.println ("----------------");            Jumpcat JC = new Jumpcat ("Garfield", 2);            System.out.println ("Name:" +jc.getname () + "" + "Age:" +jc.getage ());            Jc.eat ();            Jc.jump ();            Jc.sleep ();           System.out.println ("----------------"); System.out.println ("Name:" +a.getname () + "" + "Age:" +a.getage ());            A= new Dog ("Jerry", 2);            A.eat ();            A.sleep ();            System.out.println ("----------------");            Jumpdog JG = new Jumpdog ("Mechanical Dog", 2);            System.out.println ("Name:" +jg.getname () + "" + "Age:" +jg.getage ());            Jg.eat ();            Jg.jump ();        Jg.sleep (); }    }

Day 09Java Basic Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.