Related Articles

Related Articles

Brief Overview & Comparison of Object-Oriented Programming from C to Java
  • Last Updated : 24 Nov, 2020

In this article, you will get the ability to think how really OOP works in Java through C.

Through C, you will understand the concept of Polymorphism, Inheritance, Encapsulation, Class, Objects, etc. As you also know C language don’t support OOP, but we can understand the concept behind it by defining a fine structure as a Class and Creating its Identity as an Object.

In Parallel, through Java, we can understand how it really works with actual Class & Object.

From this Comparison, we get the ability to think of Logic behind Object-Oriented Programming.

Understanding the Concept behind the Comparison

Java



C

Class in java (example Customer) A Structure created in C (example customer)
Fields of the Class (example int id, String name) Variable present in Structure (example int id, char* name)
Constructor present in Class Function created and returning a pointer which is storing the data in heap memory (Example cust* createCustomer)
Method of the class (Example printCustomer) Function created (Example printCustomer)
Creation of Object form the Class in Java Create an instance of struct customer

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// Adding the necessary header files
#include <stdio.h>
#include <stdlib.h>
  
// customer structure
typedef struct customer {
    int id;
    char* name;
  
    // cust is an alias used for struct customer by using
    // typedef
} cust;
  
cust* createCustomer(int id, char* name)
{
    // Memory allocation in the Heap
    cust* this = (cust*)malloc(sizeof(cust));
  
    // Similiar to 'this' in Java
    this->id = id;
    this->name = name;
    return this;
}
void printCustomer(cust* c)
{
    printf("The Id is %d\n", c->id);
    printf("The name is %s\n", c->name);
}
  
int main()
{
    // Create an instance of struct customer Similar
    // to creation of Object form the Class in Java
    cust* c1;
  
    // Adding the Arguments in the function
    // is similar to adding arguments in methods of the Java
    // Class
    c1 = createCustomer(25, "Siddharth");
  
    // Calling the funtion printCustomer
    printCustomer(c1);
  
    // Free the allocated memory
    free(c1);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// import the required classes
  
import java.io.*;
  
class Cust {
    int id;
    String name;
  
    // constructor
    Cust(int id, String name)
    {
        this.id = id;
        this.name = name;
    }
    public void printCustomer()
    {
        System.out.println("The Id is " + id);
        System.out.println("The name is " + name);
    }
}
  
class GFG {
    public static void main(String[] args)
    {
        // Object declaration
        Cust c1;
  
        // object Initialisation
        c1 = new Cust(25, "Siddharth");
  
        // Calling the method of cust
        // class
        c1.printCustomer();
    }
}

chevron_right


Output

The Id is 25
The name is Siddharth

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :