Java basics 15: inheritance of Java object-oriented

Source: Internet
Author: User

Java basics 15: inheritance of Java object-oriented

Today, let's take a look at the inheritance of Java's three object-oriented features.

1. Java inheritance

Inheritance is a cornerstone of java object-oriented programming technology because it allows the creation of hierarchical classes. Inheritance can be understood as an object from another

The process of retrieving attributes of an object.

If Class A is the parent class of Class B, and Class B is the parent class of class C, we also say that class C is A subclass of Class A, and class C is inherited from Class. In Java

A single inheritance means that a subclass can only have one parent class.

The two most commonly used keywords in inheritance are extends and implements. The use of these two keywords determines whether an object and another object are

IS-A (is a) relationship. By using these two keywords, we can implement an object to obtain the attributes of another object.

All Java classes are inherited from the java. lang. Object class, so the Object is the ancestor class of all classes, and all classes except the Object must

There must be a parent class.

Through the extends keyword, you can declare that a class inherits from another class. The general form is as follows:

Instance

A. java source file code:

Public class A {private int I; protected int j; public void func () {// Implementation Details }}
B. java source file code:
Public class B extends A {// Implementation Details}

The preceding code snippets show that B is inherited by A and B is A subclass of. A is A subclass of the Object. It can be declared without display. As a child

Class, B's instance has all the member variables of A, but has no access permission to the private member variable B, which ensures the encapsulation of.

2) IS-A relationship

IS-A means that one object is a classification of another object. The following describes how to use the keyword extends to implement inheritance.

Instance:

Animal. java source file code:

 

Public class Animal {public String name; public int age; public void eat () {System. out. println ("animals can eat ");}}
Mammal. java source file code:
public class Mammal extends Animal{}
Reptile. java source file code:
public class Reptile extends Animal{}
Dog. java source file code:
public class Dog extends Mammal{}

The source code file based on the above example is in the same folder. This folder is called a package in the future. The following statements are correct:

1) The Animal class is the parent class of the Mammal class.

2) The Animal class is the parent class of the Reptile class.

3) the Mammal class and Reptile class are subclasses of the Animal class.

4) The Dog class is both a subclass of the Mammal class and a subclass of the Animal class.

Analyze the IS-A relationships in the preceding example as follows:

Mammal IS-A Animal

The Reptile IS-A Animal

Dog IS-A (Mammal)

So: Dog IS-A Animal

By using the keyword extends, subclass can inherit all the methods and attributes of the parent class, but cannot use private (private) methods and attributes.

We can determine the Mammal IS-A Animal by using the instanceof operator.

Instance:

Test. java source file code:

public class Test{      public static void main(String args[]){          Animal a = new Animal();          Mammal m = new Mammal();          Dog d = new Dog();          System.out.println(m instanceof Animal);          System.out.println(d instanceof Mammal);          System.out.println(d instanceof Animal);      }}

The above example compilation and running results are as follows:

After introducing the extends keywords, let's take a look at how implements keywords are used to represent IS-A relationships. Implements keyword usage

In the case of class inheritance interface, the keyword extends cannot be used in this case.

Instance:

Animal. java source file code:

public interface Animal{}

Mammal. java source file code:

public class Mammal implements Animal{}

Dog. java source file code:

public class Dog extends Mammal{}
Three instanceof keywords

You can use the instanceof operator to check whether the Mammal and dog objects are an instance of the Animal class.

Continue with the instance of the above interface:

Test. java source file code:

public class Test{public static void main(String args[]){           Mammal m = new Mammal();           Dog d = new Dog();           System.out.println(m instanceof Animal);           System.out.println(d instanceof Mammal);           System.out.println(d instanceof Animal);}}

The above example compilation and running results are as follows:

4 HAS-A relationships

The HAS-A represents the subordination between the class and its members. This helps code reuse and reduce code errors.

Instance:

Vehicle. java source file code:

public class Vehicle{}
Speed. java source file code:
public class Speed{}

Van. java source file code:

public class Van extends Vehicle{private Speed sp;}

The Van class and the Speed class are HAS-A relationships (Van has a Speed), so you don't have to paste all the code of the Speed class into the Van class, and

The Speed class can also be reused in multiple applications. In the object-oriented feature, you do not have to worry about how the class is implemented internally. Van class will implement fine

This section is hidden from users. Therefore, users only need to know how to call the Van class to complete a function, rather than whether the Van class is implemented or called by themselves.

Other classes.

5. Java inheritance features

Java only supports single inheritance. That is to say, a class cannot inherit multiple classes. The following method is invalid:

public class extends Animal, Mammal{}

Java only supports single inheritance (inheriting basic classes and abstract classes), but we can use interfaces (implementing multiple inheritance interfaces). The script structure is as follows:

public class Apple extends Fruit implements Fruit1, Fruit2{}

Generally, we use the extends keyword to inherit basic classes and abstract classes, and use the implements keyword to inherit interface classes.

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.