The document provides an overview of methods and constructors in Java, detailing their definitions, types, and key components such as method signatures, return types, and access specifiers. It also explains method overloading and constructor overloading, illustrating these concepts with code examples. Additionally, it discusses the absence of a copy constructor in Java and alternative methods for copying object values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views17 pages
Constructors
The document provides an overview of methods and constructors in Java, detailing their definitions, types, and key components such as method signatures, return types, and access specifiers. It also explains method overloading and constructor overloading, illustrating these concepts with code examples. Additionally, it discusses the absence of a copy constructor in Java and alternative methods for copying object values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17
Method in Java
• A method is a block of code or collection of
statements or a set of code grouped together to perform a certain task or operation. • It is used to achieve the reusability of code. • Types of Method – Predefined Method – User-defined Method Method Declaration Method Signature • Every method has a method signature. • It is a part of the method declaration. • It includes the method name and parameter list. Return Type • Return type is a data type that the method returns. • It may have a primitive data type, object, collection, void, etc. • If the method does not return anything, we use void keyword. Access Specifier • Access specifier or modifier is the access type of the method. • It specifies the visibility of the method. • Java provides four types of access specifier: • Public: The method is accessible by all classes when we use public specifier in our application. • Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined. • Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package. • Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible only from the same package only. Method Name: • It is a unique name that is used to define the name of a method. • It must be corresponding to the functionality of the method. Parameter List: • It is the list of parameters separated by a comma and enclosed in the pair of parentheses. • It contains the data type and variable name. • If the method has no parameter, left the parentheses blank. Method Body: • It is a part of the method declaration. • It contains all the actions to be performed. Constructors In Java, a constructor is a block of codes similar to the method.
It is called when an instance of the class is created.
At the time of calling constructor, memory for the object is
allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least
one constructor is called. It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default. Rules for creating Java constructor – Constructor name must be the same as its class name – A Constructor must have no explicit return type not even void • Because it returns instance of a class – A Java constructor cannot be abstract, static, final, and synchronized Types of Java constructors • Default constructor (no-arg constructor) • Parameterized constructor Method Overloading • Different ways to overload the method – By changing number of arguments – By changing the data type class Adder{ static int add(int a,int b){return a+b;} static int add(int a,int b,int c){return a+b+c;} } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }} class Adder{ static int add(int a, int b){return a+b;} static double add(double a, double b) {return a+b;} } class TestOverloading2{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(12.3,12.6)); }} Constructor Overloading in Java • In Java, a constructor is just like a method but without return type. • It can also be overloaded like Java methods. • Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. • They are arranged in a way that each constructor performs a different task. • They are differentiated by the compiler by the number of parameters in the list and their types. //Java program to overload constructors class Student5{ int id; String name; int age; //creating two arg constructor Student5(int i,String n){ id = i; name = n; } //creating three arg constructor Student5(int i,String n,int a){ id = i; name = n; age=a; } void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } } Java Copy Constructor • There is no copy constructor in Java. • However, we can copy the values from one object to another like copy constructor in C+ +. • There are many ways to copy the values of one object into another in Java. They are: • By constructor • By assigning the values of one object into another • By clone() method of Object class //Java program to initialize the values from one object to another object. class Student{ int id; String name; //constructor to initialize integer and string Student(int i,String n){ id = i; name = n; } //constructor to initialize another object Student(Student s){ id = s.id; name =s.name; } void display() { System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan"); Student s2 = new Student(s1); s1.display(); s2.display(); } }