Java面试题每日10问(7)

news2024/11/20 4:48:57

Core Java - OOPs Concepts: final keyword Interview Questions

1. What is the final variable?

  • the final variable is used to restrict the user from updating it.
  • If we initialize the final variable, we can’t change its value.
  • The final variable - which is not assigned to any value can only be assigned through the class constructor.

The java final keyword can be used in many context.

  • variable
  • method
  • class

The java final keyword can’t

  • stop value change
  • stop method overridding
  • stop inheritance

在这里插入图片描述
code:

class Bike{  			
 final int speedlimit=90;//final variable  
 void run(){  
  speedlimit=400;  
 }  
 public static void main(String args[]){  
 Bike obj=new  Bike();  
 obj.run();  
 }  
}//end of class  
Output:Compile Time Error

2.What is the final method?

a method in java is defined a final method, we can’t override it

class Bike{  
  final void run(){System.out.println("running");}  
}  
     
class Honda extends Bike{  
   void run(){System.out.println("running safely with 100kmph");}  
     
   public static void main(String args[]){  
   Honda honda= new Honda();  
   honda.run();  
   }  
}  
Output:Compile Time Error

3.What is the final class?

if we make any class final, we can’t inherit it into any of the subclasses

final class Bike{}  
  
class Honda extends Bike{  
  void run(){System.out.println("running safely with 100kmph");}  
    
  public static void main(String args[]){  
  Honda honda= new Honda();  
  honda.run();  
  }  
}  
Output:Compile Time Error

4.What is blank or uninitialized final variable?

  • A final variable that is not initialized at the time of declaration is known as blank final variable.

  • create a variable is initialized at the time of creating object and once initialized may not be changed

  • It can be initialized only in constructor.

public class Student {
    int id;
    String name;
    final String PAN_CARD_NUMBER;//PAN_CARD_NUMBER is not  set value,so we must make constuctor and set PAN_CARD_NUMBER value


    public Student(String pan_card_number) {//make make constuctor and set PAN_CARD_NUMBER value
            PAN_CARD_NUMBER = pan_card_number;
    }

    public static void main(String[] args) {
        Student s = new Student("123456");
        System.out.println(s.PAN_CARD_NUMBER);

    }
}


123456

5. Can we initialize blank final variable?

Yes. if it is not static, we can initialize it in the constructor. If it is static blank final variable, it can be initialized only in the static block.

if it is not static, we can initialize it in the constructor.

public class Student {
    int id;
    String name;
    final String PAN_CARD_NUMBER;//PAN_CARD_NUMBER is not  set value


    public Student() {
            PAN_CARD_NUMBER = "78910"; //set value
    }

    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.PAN_CARD_NUMBER);

    }
}
78910

If it is static blank final variable, it can be initialized only in the static block.

public class Student {
    int id;
    String name;
    static final String PAN_CARD_NUMBER;
    static{
            PAN_CARD_NUMBER = "78910";
    }
    public static void main(String[] args) {
        System.out.println(Student.PAN_CARD_NUMBER);

    }
}

78910

6.What is final parameter?

If you declare any parameter as final, you cannot change the value of it.

class Bike{  
  int cube(final int n){  
   n=n+2;//can't be changed as n is final  
   n*n*n;  
  }  
  public static void main(String args[]){  
    Bike b=new Bike();  
    b.cube(5);  
 }  
}  
Output: Compile Time Error

7.Can we declare a constructor as final?

  • The constructor can never be declared as final because it is never inherited.
  • Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final.
  • if you try to do so, The compiler will throw an error.

8.Can we declare an interface as final?

  • No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition.
  • Therefore, there is no sense to make an interface final.
  • if you try to do so, the compiler will show an error.

9.What is the difference between the final method and abstract method?

The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition

Core Java - OOPs: Polymorphism Interview Questions

10.What is the difference between compile-time polymorphism and runtime polymorphism?

compile-time polymorphismRuntime polymorphism
In compile-time polymorphism, call to a method is resolved at compile-time.In runtime polymorphism, call to an overridden method is resolved at runtime.
It is also known as static binding, early binding, or overloading.It is also known as dynamic binding, late binding, overriding, or dynamic method dispatch.
Overloading is a way to achieve compile-time polymorphism in which, we can define multiple methods or constructors with different signatures.Overriding is a way to achieve runtime polymorphism in which, we can redefine some particular method or variable in the derived class. By using overriding, we can give some specific implementation to the base class properties in the derived class.
It provides fast execution because the type of an object is determined at compile-time.It provides slower execution as compare to compile-time because the type of an object is determined at run-time.
Compile-time polymorphism provides less flexibility because all the things are resolved at compile-time.Run-time polymorphism provides more flexibility because all the things are resolved at runtime.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/156882.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

论文阅读笔记《Multilevel Graph Matching Networks for Deep Graph Similarity Learning》

核心思想 本文提出一种多级图匹配网络(MGMN)用于图相似性的度量。常见的图相似性网络都是利用图神经网络或其他图嵌入技术将整幅图转化为特征向量,然后计算两个特征向量之间的相似程度。这种做法的缺点在于只关注了图一级的信息交互&#xff…

【随风丶逆风】2022年终总结

前言 又到了一年一度的年终总结了,回顾一年可谓一波三折,感慨良多,最有感触的大概就是疫情带来各种影响吧,经济下行、市场不景气、互联网寒冬。 回顾去年年终规划《【随风丶逆风】2021年终总结》,整体低于预期&#x…

ArcGIS基础实验操作100例--实验83查找点集中最近最远点

本实验专栏参考自汤国安教授《地理信息系统基础实验操作100例》一书 实验平台:ArcGIS 10.6 实验数据:请访问实验1(传送门) 高级编辑篇--实验83 查找点集中最近最远点 目录 一、实验背景 二、实验数据 三、实验步骤 &#xff0…

1.Linux库详解

Hello,小伙伴们,大家好!最近有小伙伴问我程序库相关的问题。程序库的存在很大程度上提高了程序的复用性、可维护性,但是程序库的应用往往对于初学者来说有些摸不清头脑,所以这一期本文从Linux的角度谈谈Linux下的程序库…

数字IC设计、验证、FPGA笔试必会 - Verilog经典习题 (二)异步复位的串联T触发器

数字IC设计、验证、FPGA笔试必会 - Verilog经典习题 (二)异步复位的串联T触发器 🔈声明: 😃博主主页:王_嘻嘻的CSDN博客 🧨未经作者允许,禁止转载 🔑系列专栏&#xff1a…

算法之拓扑关系

目录 前言: 算法解析 Kahn算法 DFS算法 总结: 参考资料 前言: 如何确定代码源文件的编译依赖关系? 我们知道,一个完整的项目往往会包含很多代码源文件。编译器在编译整个项目的时候,需要按照依赖关…

4-大规模城市场景建模与理解

方向:三维重建 题目:大规模城市场景建模与理解 作者:陈宝权 万国伟 山东大学 关键词:场景重建 场景理解 自动扫描 智能建模 来自:中国计算机学报通讯 12卷 8期 2016.08 期刊:https://github.com/Darr…

在智能家居音箱领域上的音频功放芯片IC

目前,音频功放芯片主要应用于手机、音响、车载、可穿戴设备、计算机设备、智能家居等领域。随着人机交互逐步落地,从应用广度上对音频功放芯片需求完全放开,截止2021年以“智能音箱”、“智能家居”为代表的音频智能终端也持续放量&#xff1…

Java开发学习(四十)----MyBatisPlus入门案例与简介

一、入门案例 MybatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提供效率。 SpringBoot它能快速构建Spring开发环境用以整合其他技术,使用起来是非常简单,对于MybatisPlus,我们也基于SpringBoot来构建…

掌握流量密码五要素,抓住底层逻辑,让你更容易获得流量

分享一篇关于流量的文章:流量是一切赚钱项目中最重要的一个要素没有流量,赚钱就是空谈。流量多就赚的多,流量少就赚的少,没有流量就没得赚。因为流量非常重要,所以要打造私域用户池,让流量变成留量。私域用…

微信小游戏开发学习记录2

接上一篇:微信小游戏开发学习记录_寂静流年韶华舞的博客-CSDN博客_微信小游戏开发学习 目录 一、UI系统 1、基础渲染组件-精灵组件 (1)操作: (2)Sprite 属性 (3)渲染模式 2、L…

Qt基于CTK Plugin Framework搭建插件框架--事件监听

文章目录一、前言二、框架事件三、插件事件四、服务事件五、添加事件监听一、前言 CTK一共有三种事件可以监听: 框架事件插件事件服务事件 但是这些事件只有在变化时才能监听到,如果已经变化过后,进入一个稳定的状态,这时才去监…

Android Studio实现一个新闻APP系统源码,仿网易,搜狐等新闻客户端,本科毕业设计必备项目

DavidTGNewsProject ##【Android】最新主流新闻app功能实现。仿网易,搜狐等新闻客户端 完整代码下载地址:Android Studio实现一个新闻APP系统源码 先给大家看一下效果图: 这个项目总体来说虽然不是特别难,但是确实非常常用的功能。是业余时间…

wordcloud | 词云 in python

wordcloud | 词云🤨wordcloud | 词云🫡词云是啥😶‍🌫️词云的历史🤔安装 wordcloud 包😎官方文档🤣一个最简单的例子👍运行结果😊感谢🤨wordcloud | 词云 赢…

Vue中v-for不要和v-if一起使用

在Vue2中v-for和v-if一起使用时会报错:The xxx variable inside v-for directive should be replaced with a computed property that returns filtered array instead. You should not mix v-for with v-if原因:Vue2中当 v-if 与 v-for 一起使用时&…

4张图搞懂Salesforce的认证体系(附新手考证攻略)

Salesforce认证计划概述最近这一两年,Salesforce的Trailhead和认证太热门了,小伙伴们前赴后继地刷Badge拿认证,可以考的认证也随着产品家族的增加而增加,从十几年前的几个认证,增长到现在的40多个认证。与其他应用平台…

2023年自学网络安全珍藏版路线,高效入门

前言 【一一帮助安全学习一一】 ①网络安全学习路线 ②20份渗透测试电子书 ③安全攻防357页笔记 ④50份安全攻防面试指南 ⑤安全红队渗透工具包 ⑥网络安全必备书籍 ⑦100个漏洞实战案例 ⑧安全大厂内部视频资源 ⑨历年CTF夺旗赛题解析 01 什么是网络安全 网络安全可以基于攻击…

Seq2Seq增加attention机制的原理说明

以中文翻译为英文为例讲解seq2seq的原理,以及增加attention机制之后的seq2seq优化版本。 文本参考: Pytorch实现Seq2Seq(Attention)字符级机器翻译_pytorch seq2seq_孤独腹地的博客-CSDN博客 https://github.com/datawhalechina/learn-nlp…

Dbeaver连接ES问题一站解决

前言 最近几天一直做ES的TPS测试,每次看数据ES的数据都在嫌麻烦(在postman指定索引通过url请求查看数据)。最后决定还是整整Dbeaver连接ES。 一、当前境况 1、ES版本比较老,还是6.4.2的 2、Dbeaver直接连接已经提示支持8.x版本 3…

【自学Python】Python格式化输出

Python格式化输出 Python格式化输出教程 在 Python 中,print() 函数用于打印相应的信息到终端控制台,同时我们还可以通过 print() 函数的 % 占位符,来对输出进行格式化,即按照我们指定的格式进行输出。 Python格式化输出占位符…