
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How does default virtual behavior differ in C++ and Java?
In C++, the class member methods are non-virtual by default. This means, they can be made virtual by specifying it.
On the other hand, in Java, methods are virtual by default, and can be made non-virtual with the help of the ‘final’ keyword.
Example
class base_class{ public void display_msg(){ System.out.println("The display_msg method of base class class"); } } class derived_class extends base_class{ public void display_msg(){ System.out.println("The display_msg of derived class called"); } } public class Main{ public static void main(String[] args){ base_class my_instance = new base_class();; my_instance.display_msg(); } }
Output
The display_msg method of base class class
A class named ‘base_class’ is created, that has a function ‘display_msg’. This function just displays a relevant message. Another function named ‘derived_class’ is inherited from the ‘base_class’. This class also has the ‘display_msg’ that displays the relevant message. Another class named Main contains the main function, where an instance of base_class is created. The ‘display_msg’ is called with this instance and the output is displayed on the screen.
- Related Articles
- Default virtual behavior in C++ vs Java
- Default arguments and virtual function in C++
- How does “void *” differ in C and C++?
- How to stop dragend's default behavior in drag and drop?
- How does Attitude Affect Organizational Behavior?
- Why does Java strictly specify the range and behavior of its primitive types?
- How to configure default Mouse Double-Click behavior in a Tkinter text widget?
- How to disable default behavior of when you press enter with JavaScript?
- What is Kestrel and how does it differ from IIS? (ASP.NET)
- How does Capital Gearing differ from Income Gearing?
- How does a Planet differ from a Star?
- How does a proton differ from an electron?
- How does an electron differ from a neutron?
- How does Implicit coercion differ from Explicit coercion in JavaScript?
- How Does Off-Chain & On-Chain Crypto Transactions Differ?

Advertisements