Java Abstract class explanation

Source: Internet
Author: User

Objective

Let's take a look at the final keyword before we talk about abstract classes.

The final modifier can modify classes, properties, methods

When a class is decorated, it means that the class cannot be inherited by other features as normal classes

Modifying a property means that the property cannot be changed and must be assigned an initial value

Modifying a method means that the method cannot be overridden

What is an abstract class?

Why use abstract classes?

When we do not use the parent class when we write inheritance, we do not need to instantiate the object, we define the parent class as abstract class.

That is, the abstract class itself has no practical meaning, but when you write an inheritance or polymorphic program, you have to define the parent class all at this time, we define the parent class as abstract class, commonly known as abstract parent class

Abstract classes are characterized by abstract classes that cannot instantiate objects, but abstract classes can be inherited as well as other classes.

What is an abstract method?

Methods in abstract classes are generally inherited and polymorphic when abstract classes cannot create objects all the methods within the abstract class are not very useful, but it is not possible to define a method in an abstract class this time we define the method as an abstract method.

Three characteristics of an abstract method

1. There is no method body for abstract method

2. Abstract methods must exist in abstract classes

3. The abstract method in the parent class must be overridden in the subclass

Let's look at an example:

Abstract class Pet{public abstract void Eat ();} Class Dog extends Pet{public void eat () {System.out.println ("Dog eats Bones");}} Class Cat extends Pet{public void eat () {System.out.println ("Cat eats Fish");}} Class Nature{public void Law (Pet p) {p.eat ();}} Class  testab{public static void Main (string[] args) {Nature Nature = new Nature (); Nature.law (new Dog ()); Nature.law ( New Cat ());}}

Java Abstract class explanation

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.