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

Developer and Computer Science graduate from Vellore Institute of Technology, Chennai, India

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

Can we print any string in Java without a semicolon?
Yes We can print a string with out semicolon in if statement. public class Print_Without_Semicolon { public static void main(String args) { int i = 0; if(System.out.printf("print...
Sridevi
0 0
6
What is the difference between checked and unchecked exceptions?
The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime
Wren

Hi every one i have done my I com so plzz suggest me it course. I want do carrer in it field

First of all you should take the degree course with full concept because when you have chosen this subject then you will haveto try for best performance in it
Aquib
0 0
9
How does Java implement polymorphism?
Java implements polymorphism in two ways: 1. Static or compile-time polymorphism is done by method overloading (more then one methods sharing the same name but different number/types/order of parameters) 2....
Sajjan
0 0
6
What is the best book to learn data structures using Java?
There are several good books available for learning data structures using Java, depending on your level of expertise and experience. Some of the best books to learn data structures using Java are: 1....
Shubham
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

Android : Support multiple themes in an android application
If you are developing a theme based application to enhance user experience, then following steps needs to follow.We are taking example of an android application having 2 themes white and black.Step 1:Define...

SWITCH STATEMENT IN JAVA
switch statement - condition statement - only once the checking takes place so we recommend use break statement. switch(condition/variable/choice){case 1: //statements break;case 2: // statements break;case...

Java :: Inner Class Concepts
Java:: Inner Class Concepts--------------------------------- Inner class concepts introduced in Java v1.1 - as a part of AWT Event Handling improvements.When to use Inner class: - Without existing One...
M

Marimuthu P.

2 0
0

Features Of Java
There is given many features of Java. They are also known as java buzzwords. The Java Features given below are simple and easy to understand. Simple. Object-Oriented. Portable. Platform independent. Secured. Robust. Architecture...
V

Features Of Java
There is given many features of java. They are also known as java buzzwords. The Java Features given below are simple and easy to understand. i. Simple ii. Object-Oriented iii. Portable iv. Platform...

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