:::: MENU ::::

Saturday, October 9, 2021

This article is all about a comparison between the two most popular programming languages C++ and Java. Most of the time, beginners get confused on important topics of these languages such as Static, final keywords, etc.

Table 1. Very basic differences between C++ and Java.

C++

Java

1.  C++ is a platform-dependent

a.  Java is a platform-independent

2.  C++ is mainly used for system programming

b.  Mainly used for application programming in Windows, Web-based, mobile applications.

3.  It supports the goto statement

 Multiple inheritances, operator overloading, etc.

c.   Doesn’t support multiple inheritances, goto statement, operator overloading, etc.

 

But the interface helps to achieve multiple inheritance features.

4.   It also supports pointers externally, and can access a particular memory location using pointers.

d.  Pointers are handled internally, which makes the program less complex.

5.  It’s a compiled language. Its compiler is g++.

e.  It’s a compiler and interpreted both.

Source code ->(compiler) byte code -> (interpreter) machine code

6.   Support call by value and call by reference

f.    Only call by value. No call by reference.

7.   It doesn’t have built-in support for threads but rather relies on third-party libraries.

g.  It has built-in thread support.

8.  Its overriding is performed using virtual keyword. So that we can decide whether or not to override a function

h.  No virtual keyword. We can override methods by default. So all non-static methods are virtual by default.

9.  It always creates a new inheritance tree

i.     Uses a single inheritance tree. Because all classes are the child of an object class.

10.              Doesn’t support documentation comments.

j.     Supports documentation comments.

 

Note: Java doesn’t support default argument in function and doesn’t support header files.

 

 

Four Basic OOP concepts

        Encapsulation:

  •  Place all data objects and methods/functionalities in one location, e.g. class is an example of encapsulation.

        Inheritance:

  •  It is the process of code reusability and it implements the “IS A” relationship between parent and child classes. The child or subclass inherits non-private data/methods from the parent class based on access specifiers.

 

 

Access specifier:

C++:       

Private, e.g. class B: private A {}

All non-private members become private members of Class B.

Protected, e.g. class B: protected A{}

All non-private members become protected members of class B.

Public, e.g. class B: public A{}

All public members become public and protected members become protected.

 

Java: 

Java allowed four access modifier such as Private, default, Protected, and Public. We don't have permission to use private, protected, and static with top-level/OuterClass. But InnerClass has the freedom to use any access modifier. InnerClass can be Two types:

1. non-static

        It can access data-instance and method of outClass without creating outerClass's reference object.

2. static

        It has permission to access only and only static instances/methods of OuterClass. But it can be created except for creating OuterClass.

 

Within class

Within package

Outside package by subclass

Outside package

Private

Yes

No

No

No

Default

Yes

Yes

No

No

Protected

Yes

Yes

Yes

No

Public

Yes

Yes

Yes

Yes

 

                So, the subclass inherits:

1. Public members

2. Protected members in/out package

3. Default members within the package

 

        Abstraction:

        It is a process of hiding the implementation details, but showing only functionalities to the user.

        It is implemented by

§  Abstract class

§  Interface

Polymorphism:

 

 

Uses of Static keyword

o   This point is only applicable to C++, not Java. a static local variable can declare in local scope like functions block.

o   In the class definition, the static keyword can be used for declaring static class data variables. (both)

o   Declaring static function within the class definition (both) or in the global scope (C++).

 

Characteristics of static data variables:

ü It is initialized only once from the beginning of the program and gets allocated in memory for the life-time of the program.

ü It can be declared in class definition and can access before creating any class instance. But there is some difference in C++ and Java

 

 

C++

Java

It must be initialized in outside of class definition scope. e.g.

Class_name::TYPE variable_name = value;

No need to initialized in outside of scope.

Accessible in class definition as well as outside using class_name::static_variable_name

Accessible in class definition as well as outside using class_name.static_variable_name

Support static variable in local scope and exists until program-end, but only accessible in that local scope.

Doesn’t support local static variables.

 

Characteristics of static functions:

ü This function only allows static data and function members to use.

 

 

Uses of Final keyword

 

C++ 11:

1. Make prevention to be overridden of Bases’ virtual function in derived class

2. Make a class non-inheritable.

Note: final is not a keyword. It just has special meaning in the above context.

Java:

1. Make prevention to be overridden of Base class function in the derived class

2. Make the class non-inheritable

3. Ensuring assignment of the variable will be only once.

e.g. final int a =10;

Or

final int a; a=10;

(But remember that initialization of a final variable is only possible in the class constructor or static block)

Note: final is a keyword in java.

 

 

 

 


Last modified: 26.08.2022

1 comment:

  1. Base obj= new Derived();
    Derived obj2= (Derived)obj;

    this usual polymorphism case, but sometimes it comes as unusual case.

    so, polymorphism and inheritance could have included.

    ReplyDelete