Dark Horse programmer----Java Basics---Abstract classes and interfaces, with related questions

Source: Internet
Author: User
Tags define abstract modifiers

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

One: abstract class (Master)

(1) To extract a number of common things into a class, this is the practice of inheritance.
But there are many things that are common, in some cases, method declarations, but method bodies.
That is, the method declaration is the same, but each specific object is not the same when the concrete implementation.
Therefore, when we define these common methods, we cannot give the concrete method body.
A method that does not have a concrete method body is an abstract method.
In a class, if there is an abstract method, the class must be defined as an abstract class.
(2) Characteristics of abstract classes
A: Abstract classes and abstract methods must be modified with the keyword abstract
Abstract class animal{} This is the declarative format
public abstract void Eat () This is the format of the abstract method
public abstract void Eat () {} This is an empty method problem, not an abstract method

B: Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be defined as abstract classes
C: Abstract class cannot be instantiated
Because it is not specific, the abstract class has a constructor method, but it cannot be instantiated? So what is the function of the construction method?
The constructor of an abstract class is used to access the initialization of the parent class data.
D: Subclass of Abstract class
A: is an abstract class. If you do not want to override the abstract method, define the class as an abstract class.
B: is a specific class. This class must override all abstract methods in the abstract class.


Add: The instantiation of an abstract class is actually achieved by a specific subclass.  This is the polymorphic way. Animal a=new Cat ();
(3) Characteristics of abstract class Members:
A: Member variables
Can have variables, and can have constants
B: Construction method
There is a constructor method for subclasses to access the initialization of the parent class data.
C: Member Method
There must be an abstract method that can have non-abstract

Member method attributes for abstract classes
A: An abstract method is a method that enforces the need for subclasses of an abstract parent class.
B: Non-abstract methods, subclasses inherit things, in order to improve the reusability of the code.
(4) Practice of abstract classes
A: Cat and dog case exercise
B: Teacher Case Exercises
C: Student Case Exercise
D: Employee Case Exercise
(5) Some small problems of abstract class (interview questions)
A: Abstract class has a construction method, can not be instantiated, then what is the use of the construction method?
Initialization for subclass access to the parent class data
B: What is the use of a class if it has no abstract method but is defined for abstract classes?
To keep the object from being created
C:abstract cannot coexist with which keywords
A:final Conflict (Constants cannot be overridden)
B:private Conflict (abstract method is to allow subclasses to rewrite, private cannot be inherited, so cannot be overridden)
C:static no meaning


Two:: Interface (Master)
(1) Looking back at cat and dog cases, they only provide some basic functions.
For example: Cat drill fire ring, dog high jump and other functions, not the animals themselves have,
Is trained in the back of the training, this extra function, Java provides an interface representation.
(2) Features of the interface:
A: interface with keyword interface decoration
Format: Interface interface Name {}
B: Class implementation interface with implements modification
Class name implements interface name {}
C: interface cannot be instantiated. So how do you instantiate it? Instantiated in a polymorphic manner.
D: Implementation Class (subclass) of the interface
A: Can be an abstract class. (But it doesn't make much sense)
B: It can be a concrete class, and this class must override all the abstract methods in the interface. (Recommended scheme)


This shows: the specific class of polymorphic (almost no) abstract class polymorphic (Common) interface polymorphism (most commonly used)
(3) member features of the interface:
A: Member variables
Interface to constants default to public static final decoration, regardless of whether you write this way, the system will default such modifiers
So the member variables of an interface can only be constants
Default modifier: public static final
B: Construction method
Interface has no constructor method. Because the interface is mainly extended functionality, there is no specific presence.
C: Member Method
Default modifier for interface member methods: Public abstract. The system defaults to these modifiers, whether or not you write them all.
So the member method of the interface can only be abstract

(4) Classes and classes, classes and interfaces, interfaces and interfaces
A: Classes and classes
Inheritance relationship, can only be single-inheritance, multi-layered inheritance such as: Class a{} class B extends a{} class C extends b{}
B: Classes and Interfaces
The implementation of the relationship, can be implemented alone, or more.
You can also implement multiple interfaces while inheriting a class
such as: Class a{} interface father{} interface mother{}
Class B extends A implements fathrer,mother{}
C: Interface and interface
Inheritance, can be single-inheritance, or multiple inheritance
Example: Interface father{} interface mother{} interface Son extends father,mother{}
(5) The difference between abstract classes and interfaces
A: Member Differences
Abstract class:
Member variable: can be a variable, or it can be a constant
Construction method: Available
Member method: Can be an abstract method or a non-abstract
Interface:
Member variable: can only be a constant
Construction Method: None
Member method: can only be an abstract method
B: Relationship differences:
Classes and classes: Inheritance relationships, can only be single-inheritance,
Classes and interfaces: Implement relationships, can be implemented single, or multiple implementations
Interfaces and interfaces: Inheritance relationships, can be single-inheritance, or multiple inheritance
C: Different design concepts
Abstract class: The inheritance embodies the relationship of is a, and the generic function is defined in the abstract class. For example, cats are animals, they have the function of animals.
Interface: The implementation embodies the relationship of like a, the interface is defined by the extension function. such as computer USB interface, is to achieve the expansion function.
(6) Practice:
A: Cat and Dog case, join the high jump function
Code:
Define the high jump interface (as the animal height is expanded) interface Jump{public abstract void jumps ();} Define abstract classes, because animals have commonalities, define commonalities in abstract class animal{//define member variables private String name;private int age;//constructor method public Animal () {} Public Animal (String Name,int age) {this.name=name;this.age=age;} Public String GetName () {return name;} public int getage () {return age;} Define abstract methods, because different animals eat things differently. public abstract void Eat ();//define common methods public void sleep () {System.out.println ("Animals are going to sleep");}} Define CAT class inherit animal class cat extends animal{//construction method public Cat () {}public cat (String Name,int age) {super (name,age);} Overrides the abstract method of the parent class. public void Eat () {System.out.println ("Cat eats Fish");}} Define dog class inherit animal class dog extends animal{//construction method Public Dog () {}public Dog (String name,int age) {super (name,age);} Overrides the abstract method of the parent class. public void Eat () {System.out.println ("Dog eats Bones");}} Defines the cat that will jump high class Jumpcat extends cat implements Jump{public Jumpcat () {}public jumpcat (String name,int age) {Super ( Name,age);} public void jumps () {System.out.println ("High jump cat will high Jump");}} Defines the dog that will jump high to achieve the height of class Jumpdog extends dog implements Jump{public Jumpdog ({}public Jumpdog (String name,int age) {super (name,age);} public void jumps () {System.out.println ("High jump dog will high jump");}} Class Test{public static void Main (String[]args) {//Creates a cat object that will jump. Jumpcat c=new jumpcat ("Doraemon", 2); System.out.println (C.getname () + "----" +c.getage ()); C.eat (); C.sleep (); C.jump ();}}



B: Teacher and student case, add smoking function
A, analysis: from concrete to abstract
Teacher: Name, age, eat, sleep.
Student: Name, age, meal, sleep.
Due to the commonality function, we present a parent class, human.


Human:
Member variables: name, age
Abstract method: Eat ();
Member Method: Sleeping () {}
Since the extra features of smoking are not available to every student or teacher, we define the expansion function as an interface for smoking.

Smoking interface
Some teachers smoke: realize the Smoking interface.
Some students smoke: to achieve the smoking interface.
B, implementation: From abstraction to specific code.
C, use: specific.

Dark Horse programmer----Java Basics---Abstract classes and interfaces, with related questions

Related Article

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.