assignment2
assignment2
assignment2
3. Class name: The name should begin with an initial letter (capitalized
by convention).
4. Superclass(if any): The name of the class’s parent (superclass), if
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
public static void main(String args[])
{
// creating an object of
// Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
properties of an object.//variables
2.Behavior : It is represented by the methods of an object. It also
import java.io.*;
class Demo {
int x = 10;
int display()
{
System.out.println("x = " + x);
return 0;
}
}
class Main {
public static void main(String[] args)
{
Demo D1 = new Demo(); // point 1
System.out.println(D1); // point 2
System.out.println(D1.display()); // point
3
}
}
1. Modifier: It defines the access type of the method i.e. from where
it can be accessed in your application. In Java, there 4 types of access
specifiers.
public: It is accessible in all classes in your application.
in its subclasses.
private: It is accessible only within the class in which it is defined.
accessible within the same class and package within which its class is
defined.
Note: It is Optional in syntax.
2. The return type: The data type of the value returned by the
method or void if does not return a value. It is Mandatory in syntax.
3. Method Name: the rules for field names apply to method names
as well, but the convention is a little different. It is Mandatory in
syntax.
4. Parameter list: Comma-separated list of the input parameters is
defined, preceded by their data type, within the enclosed parenthesis.
If there are no parameters, you must use empty parentheses (). It
is Optional in syntax.
5. Exception list: The exceptions you expect by the method can
throw; you can specify these exception(s). It is Optional in syntax.
6. Method body: it is enclosed between braces. The code you need
to be executed to perform your intended operations. It is Optional in
syntax.
class Addition {
// Method
// To add two numbers
public int addTwoInt(int a, int b)
{
// Class 2
// Helper class
class GFG {
class Geek {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
System.out.println();
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
6. What is constructor overloading?
Explain it with appropriate example,
// Fields Declared
String name;
int age;
// Constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
}
// main function
public static void main(String[] args)
{
// Objects Declared
Person first = new Person("ABC", 18);
Person second = new Person("XYZ", 22);
first.printDetails();
second.printDetails();
first.change_name("PQR");
System.out.println("Name has been changed to: "
+ first.get_name());
}
}
access modifiers.
Classes, methods, or data members that are declared as public
Syntax of Varargs
Internally, the Varargs method is implemented by using the single
dimensions arrays concept. Hence, in the Varargs method, we can
differentiate arguments by using Index. A variable-length argument is
specified by three periods or dots(…).
For Example,
public static void fun(int ... a)
{
// method body
}
This syntax tells the compiler that fun( ) can be called with zero or
more arguments. As a result, here, a is implicitly declared as an array
of type int[].
class Test1 {
// A method that takes variable
// number of integer arguments.
static void fun(int... a)
{
System.out.println("Number of arguments: "
+ a.length);
// Driver code
public static void main(String args[])
{
// Calling the varargs method with
// different number of parameters
// one parameter
fun(100);
// four parameters
fun(1, 2, 3, 4);
// no parameter
fun();
}
}