0% found this document useful (0 votes)
3 views4 pages

Java OOP Assignment: Polymorphism & Classes

best OOP with java

Uploaded by

Mark
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java OOP Assignment: Polymorphism & Classes

best OOP with java

Uploaded by

Mark
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NAME-RISHIRAJ PARMAR

Assignment 1

Object Oriented Programming using Java

Q No Question

1 Write a program to find the sum of all integers greater than 40 and less than 250
that are divisible by 5.

2 Create a Java program demonstrating class implementation. Define a class with


data members, such as Student Name, SAP Id, and Marks, along with methods
named setData(String name, int id, double marks) and getData() to manage
customer information. Invoke these methods within the main method to showcase
the functionality of the class.

3 Explain polymorphism in Java and provide a suitable program demonstrating


compile-time polymorphism.

ANSWER 1.

class abc

public static void main(String arg[]){

int sum=0;

for(int i=40;i<251;i++){

if(i%5==0 ){

[Link](i);

sum=sum+i;

}
[Link]("the sum of intergers from 40 to 250 that are divisible
by 5 \n" + sum);

ANSWER 2.

public class Student {

private String name;

private int id;

private double marks;

public void setData(String name, int id, double marks) {

[Link] = name;

[Link] = id;

[Link] = marks;

public String getData() {

return "Student Name: " + name + "\nSAP Id: " + id + "\nMarks: " +
marks;

public static void main(String[] args) {

Student student = new Student();


[Link]("John Doe", 123456789, 90.0);

[Link]([Link]());

ANSWER 3.

Compile Time Polymorphism in Java?

Early binding or static polymorphism are other names for compile time
polymorphism in Java. Compile time polymorphism gets its name from the fact
that the binding is done at the time of compilation. Connecting the function call
and the function body is referred to as binding. It is accomplished via
overloading operators and methods as well as by modifying the function’s
signature. The class’s reference variable handles the overloading of methods.
By adding additional features to an operator, operator overloading is
accomplished. The return value and its type, as well as the parameters and their
types, are referred to as the function’s signature. It outlines a function’s input
and output.

Example of Compile Time Polymorphism in Java:

class SimpleCalculator

int add(int a, int b)

return a+b;

}
int add(int a, int b, int c)

return a+b+c;

public class DemoCal

public static void main(String args[])

SimpleCalculator obj = new SimpleCalculator();

[Link]([Link](10, 20));

[Link]([Link](10, 20, 30));

Common questions

Powered by AI

Early binding, in the context of compile-time polymorphism, refers to resolving function calls to specific method implementations during compile time instead of at runtime. This leads to faster execution of the program since the method to be executed is pre-determined, thus eliminating the overhead of dynamic binding. Early binding is typically used in cases like method overloading within a class .

Data encapsulation in Java is the practice of restricting access to certain details of an object and only allowing access through specific methods. In the 'Student' class example, encapsulation is achieved by keeping data members 'name', 'id', and 'marks' private and accessing them through public methods 'setData' and 'getData'. This promotes data integrity and security as it hides the internal representation of the object from the outside .

Access modifiers in Java, such as private, public, and protected, control the visibility and accessibility of classes, methods, and variables. They are essential in class design to enforce encapsulation and data hiding. For instance, marking variables as private ensures they cannot be directly accessed or modified outside the class, preserving data integrity and encapsulation .

The function's signature, which includes the method name, parameter types, and the number of parameters, is crucial to achieving compile-time polymorphism. In Java, method overloading utilizes different function signatures to allow multiple methods with the same name to coexist in a class. The correct method is selected based on the signature during compilation, facilitating compile-time polymorphism .

A class in Java is implemented with both data members and methods. Data members represent the attributes of the class, while methods define the behavior. For example, in the 'Student' class, data members include 'name', 'id', and 'marks'. Methods like 'setData' and 'getData' allow manipulation and retrieval of these data members. In the main method, a 'Student' object is created, and these methods are invoked to set and get student information .

Method overloading and method overriding are two different aspects of polymorphism in Java. Overloading occurs within the same class and involves methods with the same name but different signatures, enabling compile-time polymorphism. Overriding, on the other hand, involves methods in a subclass having the same name and signature as a method in the superclass, enabling runtime polymorphism. While overloading is resolved during compile time, overriding is resolved at runtime .

Compile-time polymorphism in Java, also known as static polymorphism or early binding, is implemented through method overloading and operator overloading. This type of polymorphism occurs when the method call is resolved at compile time. The main advantage of compile-time polymorphism is improved performance because the method to be executed is known at compile time, eliminating overhead associated with deciding the method to execute at runtime .

Public methods in Java classes promote modularity by allowing interaction with an object's behavior while keeping its state limited to internal modifications. This design principle offers flexibility to change the underlying implementation without affecting external code. It helps in creating highly decoupled systems where components can be changed independently .

Java does not support operator overloading in the same way as C++. However, operator-like functionality can be achieved by designing class methods to handle operations with specific data, similar to overloading methods. While this can lead to more intuitive code, it might reduce code readability due to less explicit operation declarations, which is why Java discourages direct operator overloading .

In a Java program to calculate the sum of integers between 40 and 250 that are divisible by 5, a loop iterates through this range, checking the divisibility condition. If an integer is divisible by 5, it is added to a running total. The sum is then output. This approach effectively demonstrates handling iterations and conditional logic in Java .

You might also like