UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Can structs have methods?

Asked by Last Modified  

3 Answers

Learn Java

Follow 2
Answer

Please enter your answer

Wroking in IT industry from last 15 years and and trained more than 5000+ Students. Conact ME

Yes, structs in languages like Go and C++ can have methods, but not in C.
Comments

My teaching experience 12 years

A struct can also have static methods.
Comments

An aspiring IT professional and final-year BTech student at Vellore Institute of Technology

Yes, structs can have methods in C++, C#, and even C (with function pointers), but their behavior differs: C (No Direct Methods) C structs cannot have methods, but function pointers can be used as a workaround. c #include <stdio.h> typedef struct { int x, y; void (*print)(int, int);...
read more

Yes, structs can have methods in C++, C#, and even C (with function pointers), but their behavior differs:

C (No Direct Methods)

  • C structs cannot have methods, but function pointers can be used as a workaround.
c
 
#include <stdio.h> typedef struct { int x, y; void (*print)(int, int); } Point; void printPoint(int x, int y) { printf("Point: (%d, %d)\n", x, y); } int main() { Point p = {10, 20, printPoint}; p.print(p.x, p.y); return 0; }

C++ (Structs Can Have Methods)

  • C++ treats structs like classes (default access is public), allowing methods, constructors, destructors, and operator overloading.
cpp
 
#include <iostream> using namespace std; struct Point { int x, y; void print() { cout << "Point: (" << x << ", " << y << ")" << endl; } }; int main() { Point p = {10, 20}; p.print(); return 0; }

C# (Structs Can Have Methods)

  • C# structs support methods, properties, and constructors (except parameterless constructors). They cannot inherit from classes but can implement interfaces.
csharp
 
using System; struct Point { public int X, Y; public Point(int x, int y) { X = x; Y = y; } public void Print() { Console.WriteLine($"Point: ({X}, {Y})"); } } class Program { static void Main() { Point p = new Point(10, 20); p.Print(); } }

Summary

  • C: No direct methods, but function pointers can be used.
  • C++: Structs behave like lightweight classes and support methods.
  • C#: Structs can have methods, properties, and constructors, but no inheritance from classes.

Each language uses structs differently based on its design principles.

read less
Comments

View 1 more Answers

Related Questions

Base obj=new Child(); What are the advantage and real life application of above code?
Have you ever observed while communicating with databases? You can find such type of example while writing code from communicating with DB. Connection connectionObject = DriverManager.getConnection(); or Statement...
Vijay
What are the booming technologies in US from Job point of view
All the terminologies are good. First of all decide your self you want go as functional consultant or technical. If you have java background Learn Hadoop. Billing rate is very high.
Chandra
Is it really needed to do Java training before graduation, to get a job?
Yes, It's must to get better opportunities and to standard alone in IT field. Not only core Java but also learn Spring,Hinernat ,Struts. ANGULARJS add an advantage to get job easily.
Udaybhan
What is the general syllabus covered in core Java training from any institution?
Welcome Course Overview Review of Java Fundamentals The Java Environment Data Types The String Class The StringBuffer Class Arrays Passing Data Types to a Method Constructors &...
Shiv Kohli
0 0
5

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Why We Should Go With Java?
Java is widely used in the Desktop application, web application, mobile development, games etc. Java is an object-oriented programming language. Its follow all the oops concept Like Object, Class,...
B

Bhupendra Patidar

0 0
0

Importance of Constructor's Visibility.
While developing program or implementing Singleton pattern we have learnt to mention constructor as private and known reason is -> to blocked its direct call from outside of class. But did we think...
M

Comparable vs Comparator
java.lang.Comparable java.util.Comparator For comparing some other object with its own object. Ex. I am comparing myself to some other employee. Method signature is: int compareTo (T object). For...

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

Difference Between Checked And Un-Checked Exception
1. Exception is an abnormal condition in the program, if raises program terminates abnormally. 2. Exception always occurs at runtime only, there is no chance of exception at compile time. 3. Syntactical...

Recommended Articles

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more