java子类用父类成员函数_Java的父类和子类中是否可以具有相同的数据成员?

news/2024/7/3 12:23:23 标签: java, 多态, c++, 编程语言, 接口

java子类用父类成员函数

The question is that "Can we have same data members in parent/base and child/derived classes in Java"?

问题是“在Java的父类/基类和子类/派生类中可以有相同的数据成员”吗?

  • Yes, it is possible to have same data member in Parent and Child classes.

    是的,在Parent和Child类中可以有相同的数据成员

  • Now, we will see the capability or strength of the Parent and Child class. The Parent class reference can hold its own object and Child class object as well and The Child class reference hold its own object only.

    现在,我们将看到父子类的能力或实力。 父类引用可以保留自己的对象,子类对象也可以保留,而子类引用只能保留自己的对象。

  • The Parent class object can access their data only that's means this class doesn't have permission to access Child class data.

    父类对象只能访问其数据,这意味着该类无权访问子类数据。

  • With the help of example we will see what will happen if we contain same data member in both the class [Parent and Child].

    借助示例,我们将看到如果在[Parent和Child]类中包含相同的数据成员,将会发生什么情况。

Example:

例:

class Base {
    //  Declare and Instantiate data member  
    String str = "We are in Parent class";
}

class Derived extends Base {
    //  Declare and Instantiate data member  
    String str = "We are in Child class";
}

class Main {
    public static void main(String[] args) {
        // Creating a Base class object
        Base base = new Base();
        // Invoked Base class data member with Base class object.
        System.out.println("Base class reference can access only base class member :" + " " + base.str);

        // Creating a Derived class object
        Derived derived = new Derived();

        // Invoked Derived class data member with Derived class object.
        System.out.println("Derived class reference can access both Base and Derived class member : " + " " + derived.str);

        // Here Base class reference holding an object of
        // Derived class and it is perfectly valid
        base = new Derived();

        // Base class reference can access only Base class 
        // data member whether it is holding an object is of 
        // Base class or Derived class.
        System.out.println("Base class reference holding an object of Derived class but it can access only Base class data member :" + " " + base.str);

        // we don't need to include the below code ….
        /*
        // Here Derived class reference holding an object of
        // Base class and it is perfectly invalid
        derived = new Base();

        // If Derived class reference hold an object of Base 
        // class then we will get compile time error
        System.out.println("We will get compile time error");
        */
    }
}

Output

输出量

E:\Programs>javac Main.java

E:\Programs>java Main
Base class reference can access only base class member : We are in Parent class
Derived class reference can access both Base and Derived class member:  We are in Child class
Base class reference holding an object of Derived class but it can access only Base class data member: We are in Parent class

In the above program we have four cases:

在以上程序中,我们有四种情况:

  1. If Base class reference holds an object of Base class then it is perfectly valid and we can access only Base class data member.

    如果基类引用包含基类的对象,则它是完全有效的,并且我们只能访问基类数据成员。

    Base base = new Base();
    
  2. If Derived class reference holds an object of Derived class then it is perfectly valid and we can access both Base and Derived class data member.

    如果“派生类”引用持有“派生类”的对象,则它是完全有效的,我们可以访问基类和派生类数据成员。

    Derived derived = new Derived();
    
  3. If Base class reference holds an object of Derived class then it is also perfectly valid but it can access only Base class data member.

    如果基类引用包含派生类的对象,则它也是完全有效的,但它只能访问基类数据成员。

    Base base = new Derived();
    
  4. If Derived class reference holds an object of Base class then it is perfectly invalid and in that case we will get compile time error.

    如果派生类引用持有基类的对象,则它完全无效,在这种情况下,我们将获得编译时错误。

    Derived derived = new Derived();
    

翻译自: https://www.includehelp.com/java/is-it-possible-to-have-same-data-members-in-parent-and-child-classes-in-java.aspx

java子类用父类成员函数


http://www.niftyadmin.cn/n/1253734.html

相关文章

[杂谈]杂谈章1 问几个JAVA问题

1.面向对象、面向过程 区别 2.Java 如何实现的平台无关 和C/C不同的是,Java语言提供的编译器不针对特定的操作系统和CPU芯片进行编程,而是针对Java虚拟机把Java源程序编译成称为字节码的“中间代码”,然后Java虚拟机负责将字节码…

字符串逗号分隔成字符串数组_PHP | 从数组创建逗号分隔的字符串,而无需使用库函数

字符串逗号分隔成字符串数组Given an array and we have to create a comma delimited string from the array without using library function. 给定一个数组,我们必须使用该数组创建一个逗号分隔的字符串,而无需使用库函数。 Example: 例:…

如何使用HTML中CSS在不使用项目符号的情况下创建无序列表?

Trivia: 琐事: We all want our elements inside the web page to be in a particular order as it helps in displaying the web page in a sorted and well-arranged manner. The convention is to choose from various types of lists. There are majorly three…

在JavaScript中以Hours24:Minutes:Seconds格式获取当前UTC时间

在JavaScript中获取当前UTC时间 (Getting current UTC time in JavaScript) To get the current UTC time in JavaScript, we need to use three library functions of Date class, 要使用JavaScript获取当前UTC时间 ,我们需要使用Date类的三个库函数, …

洛谷P3865 ST表

传送门啦 思路&#xff1a; $ f[i][j] $ 表示从 $ i $ 开始&#xff0c;包含 $ 1<<j $ 个元素的区间的区间最大值&#xff1b; 转移方程: $ f[i][j]max_(f[i][j-1],f[i(1<<j-1)][j-1] $ ; 查询 $ (l,r) $ : $ plog_2(r-l1) $ ; $ max(l,r)max(f[l][p],f[r-(1<&l…

关于专线映射问题

最近布置高管局的oa系统&#xff0c;然后碰到了专线ip如何映射的问题&#xff0c;今天给大家分享一下。 关于映射&#xff0c;首先要解决的问题是在局域网中的访问&#xff0c;如何在局域网中访问你的项目呢&#xff1f;我这边用的是PHPstudy 1.打开vhost 2.将你项目的ServerNa…

c语言 递归拆分数字_C程序使用递归在数字数组中找到最大的数字

c语言 递归拆分数字Problem statement: Write a C Program to find the Biggest Number in an Array of integers (can be negative too) using Recursion. 问题陈述&#xff1a;编写一个C程序&#xff0c;以使用Recursion在整数数组中查找最大数(也可以为负数) 。 Algorithm:…

零基础英文盲打的建议

巧妇难为无米之炊之——工具篇 其实个人觉得金山打字通就不错了&#xff0c;对于喜欢二指禅的同学来说&#xff0c;强烈建议从英文开始&#xff0c;尤其是从它的新手入门开始&#xff0c;一步步掌握基本的指法&#xff0c;以及熟悉基本的按键的位置&#xff0c;然后就可以开始自…