Java 8-5 Abstraction

Source: Internet
Author: User

1, abstract class overview:
Animals should not be defined as specific things, and animals in the eating, sleeping and so should not be specific.
We call a feature that is not a specific function, but a class that, if it has abstract functionality, must be an abstract class.

Features of abstract classes:
A: Abstract classes and abstract methods must be decorated with the abstract keyword, which cannot have a body
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's not specific.
Abstract classes have a construction method, but they cannot be instantiated.
What is the function of the construction method?
Initialization for subclass access to the parent class data
D: Abstract sub-class
A: If you do not want to override the abstract method, the subclass is an abstract class.
B: Rewrite all the abstract methods, this time the subclass is a concrete class.

The instantiation of an abstract class is actually achieved by a specific subclass. is a polymorphic way.
Animal a = new Cat ();

1 //Abstract class Animal//declarative format for abstract classes2 Abstract classAnimal {3 //Abstract Methods4 //Public abstract void Eat () {}//empty method body, this will be an error. Abstract methods cannot have principals5  Public Abstract voideat ();6 7  PublicAnimal () {}8 }9 Ten //The first type: Subclasses are abstract classes One Abstract classDogextendsAnimal {} A  - //The second type: Subclasses are concrete classes, overriding abstract methods - classCatextendsAnimal { the  Public voideat () { -System.out.println ("Cat Eats fish"); - } - } +  - classAbstractdemo { +  Public Static voidMain (string[] args) { A //Creating Objects at //animal is abstract; unable to instantiate - //Animal a = new Animal (); - //by polymorphic means -Animal A =NewCat (); - a.eat (); - } in}

2, the characteristics of the abstract class Members:
Member variable: can be either a variable or a constant.
Construction method: there is.
Initializes the child class to access the parent class data.
Member methods: can be either abstract or non-abstract.

Member method attributes for abstract classes:
A: Abstract method: Forcing a subclass to do something. The sub-class does not have to error.
B: Non-abstract method: Subclass inherited things, improve code reusability.

1 Abstract classAnimal {2  Public intnum = 10;3  Public Final intnum2 = 20;4 5  PublicAnimal () {}6 7  PublicAnimal (String name,intAge ) {}8 9  Public Abstract voidShow ();Ten  One  Public voidmethod () { ASystem.out.println ("Method"); - } - } the  - classDogextendsAnimal { -  Public voidShow () { -System.out.println ("Show Dog"); + } - } +  A classAbstractDemo2 { at  Public Static voidMain (string[] args) { - //Creating Objects -Animal A =NewDog (); -A.num = 100; - System.out.println (a.num); - //a.num2 = $; in System.out.println (a.num2); -System.out.println ("--------------"); to a.show (); + A.method (); - } the}

3. Interview questions:
If there is no abstract method for a class, can it be defined as an abstract class? If so, what's the point?
A: Yes.
B: Don't let the object be created. This object can only be used by accessing its subclasses

What keywords does abstract not coexist with?
Private: conflict, private proprietary is inherited, cannot be overridden, and abstract inheritance requirements are overridden, so conflicts
Final: conflict, final variable is the final variable, cannot be rewritten, ibid.
Static: Meaningless, abstract method is no method body, and static static is accessed by the class name method body, but. To access a method without a method body, meaningless.

1 Abstract classFu {2 //Public abstract void Show ();3 //illegal modifier combinations: abstract and private4 //private abstract void Show ();5 6 //illegal modifier combination7 //final abstract void show (); 8 9 //illegal modifier combinationTen Static Abstract voidShow (); One  A  Public Static voidmethod () { -System.out.println ("Method"); - } the } -  - classZiextendsFu { -  Public voidShow () {} + } -  + classAbstractDemo3 { A  Public Static voidMain (string[] args) { at Fu.method (); - } -}

Java 8-5 Abstraction

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.