The Dark Horse programmer ———— Abstract class in Java

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Abstract class embodies the Java object-oriented features, for each thing through five aspects: attributes, methods, constructors, code blocks, internal classes, to re-deconstruct and then assemble, and then classify similar things as a class, this is the object-oriented thinking. In Java it is often said that all things are objects, then it is obvious that we can further take the method of the object as a study, that is, to further extract its function, the generation of the originator, through the subclass inherits the parent to invoke similar functions. Because an abstract-modified class cannot create an object, the abstract-decorated method is meaningless, so the subclass must override the abstraction of the parent class.

Abstract class animal{abstract void Sing (); abstract void Show (); Class Cat extends Animal{void sing () {System.out.println ("Meow meow Sing");} void Show () {}}class Dog extends Animal{void sing () {System.out.println ("barking singing"); void Show () {}}public class a6_58{public static void Main (string[] args) {Animal t=new Cat (); t.sing ();}}

  

For abstract, when a class is decorated, this class is an abstract class, and the member method in the class is divided into three cases:1. Can be all abstract methods, this class cannot create objects, subclasses need to replicate all the abstract methods in the parent class to invoke their methods by creating subclass objects

Abstract class cx{            abstract  void  function1 () {//Abstraction Method 1                    System.out.println ("I am abstract Method 1");             }             Abstract  void   function2 () {//Abstraction Method 2                     System.out.println ("I am Abstract Method 2");}         }       


Class Zi extends cx{
void Function1 () {
System.out.println ("Abstract Method 1 after Replication");
}
void Function2 () {
System.out.println ("Abstract Method 2 after Replication");
}
}


Class a{
public static void Main (string[] args) {
Zi aa =new zi;
Aa.function1;//will invoke the post-replication method
aa.function2;//will call the review method

}
}

2. Can be all non-abstract methods, this class is still unable to create objects, this is very rare, usually to not want to create objects such as the abstract decoration

Abstract class cx{           void Aa () {                   System.out.println ("I am the Normal method");       }          void  BB () {                   System.out.println ("I am also common method");}      }

  

3. There can be abstract methods or non-abstract methods that cannot create objects, and subclasses still need to review all abstractions to create objects. When the adornment method is, as long as there is an abstract method in the class, the class must be defined as abstract, because of this, when the subclass inherits the parent class, if you want to create the object, you must erase all of the abstract methods.

Abstract class cx{           void Aa () {                   System.out.println ("I am the Normal method");       }         abstract void  bb1 () {                   System.out.println ("I am abstract Method 1");         abstract void Bb2 () {                    System.out.println ("I am Abstract Method 2");}        } Class  zi{          void Bb1 () {                    System.out.println ("Replication abstract Method 1");}        }

This is the case where all abstract methods in the abstract class are not replicated, so he is still an abstraction, unable to create objects, and cannot invoke methods.

The Dark Horse programmer ———— Abstract class in Java

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.