Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Program1.Demonstrate Method Overloading by changing no.

of arguments

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));  
}}  

Output:
22
33

Program2.Demonstrate Method Overloading by changing data type of


arguments

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));  
}}  

Output:
22

24.9
Program3. Demonstrate Parameterized Constructor Overloading

public class Student {  
//instance variables of the class  
int id;  
String name;  
  
Student(){  
System.out.println("this a default constructor");  
}  
  
Student(int i, String n){  
id = i;  
name = n;  
}  
  
public static void main(String[] args) {  
//object creation  
Student s = new Student();  
System.out.println("\nDefault Constructor values: \n");  
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);  
  
System.out.println("\nParameterized Constructor values: \n");  
Student student = new Student(10, "David");  
System.out.println("Student Id : "+student.id + "\nStudent Name : 
"+student.name);  
}  
}  

Output:
this a default constructor

Default Constructor values:

Student Id : 0
Student Name : null

Parameterized Constructor values:

Student Id : 10

Student Name : David

Java Inner Classes (Nested Classes)

Nested class is a class that is declared inside the class or interface. We use inner
classes to logically group classes and interfaces in one place to be more readable and
maintainable.

Additionally, it can access all the members of the outer class, including private data
members and methods.

Syntax of Inner class

class Java_Outer_class {  
 //code  
class Java_Inner_class {  
//code  
 }  
}  

Advantage of Java inner classes

1. Nested classes represent a particular type of relationship that is it can


access all the members (data members and methods) of the outer
class, including private.

2. Nested classes are used to develop more readable and maintainable


code because it logically group classes and interfaces in one place only.

3. Code Optimization: It requires less code to write.

Program4. Demonstrate Inner Class

class TestMemberOuter1{  
 private int data=30;  
 class Inner{  
  void msg(){System.out.println("data is "+data);}  
 }  
 public static void main(String args[]){  
  TestMemberOuter1 obj=new TestMemberOuter1();  
  TestMemberOuter1.Inner in=obj.new Inner();  
  in.msg();  
 }  
}  

Output:

data is 30

You might also like