Sahil
Sahil
Constructor Overloading :
The constructor overloading can be defined as the concept of having more than one
•
constructor with different parameters so that every constructor can perform a different
task.
• Java Constructor overloading is a technique in which a class can have any number of
constructors that differ in parameter list.
Example :
class Main {
private String name;
// constructor
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}
Method Overloading :
• In Java, two or more methods may have the same name if they differ in parameters
(different number of parameters, different types of parameters, or both). These methods
are called overloaded methods and this feature is called method overloading.
• Method overloading is one of the ways through which java supports
polymorphism. Polymorphism is a concept of object oriented programming that deal
with multiple forms.
Example :
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
Java Programming Lab
import java.io.*;
import java.lang.*;
import java.util.Scanner;
class ConstOver
int x=0,y=0,z=0;
String str="";
ConstOver()
ConstOver(int a,int b)
a=in.nextInt();
b=in.nextInt();
Java Programming Lab
x=a;
y=b;
int a1=in.nextInt();
int b1=in.nextInt();
int c1=in.nextInt();
x=a1;
y=b1;
z=c1;
void display()
void display(int a)
System.out.println("Enter a number:");
int a1=in.nextInt();
x=a1;
void display(String s)
{
Java Programming Lab
System.out.println("Enter a string:");
String s1=in.nextLine();
c2.display();
c1.display(s1);
c3.display(a);
OUTPUT :
Documents/2semjava$ javac ConstOver.java
Documents/2semjava$ java ConstOver
This is empty constructor
Syntax :
class OuterClass {
...
class NestedClass {
...
}
}
Access Protection :
The protected access modifier is accessible within package and outside the
package but through inheritance only. The protected access modifier can be applied on the
data member, method and constructor. It can’t be applied on the class. It provides more
accessibility than the default modifer.
SOURCE CODE
import java.io.*;
import java.lang.*;
class InnerClass
{
int x=0,y=0,z=0;
String str="";
void impi()
{
in.call();
in.display();
System.out.println("The inner method is called\n");
}
class Inner
{
int a,b,c;
protected void call()
{
DataInputStream in=new DataInputStream(System.in);
try{
System.out.println("Enter the value of a:\n");
a=Integer.parseInt(in.readLine());
System.out.println("Enter the value of b:\n");
b=Integer.parseInt(in.readLine());
catch(Exception e)
{}
c=a*b-50;
}
void display()
{
System.out.println("The solution is "+c);
}
}
public static void main(String args[])
{
InnerClass inc=new InnerClass();
inc.impi();
}
Output:
Documents/2semjava$ java InnerClass
String in Java :
Strings in Java are Objects that are backed internally by a char array. Since
arrays are immutable(cannot grow), Strings are immutable as well. Whenever a
change to a String is made, an entirely new String is created.
Syntax :
<String_Type> <string_variable> = "<sequence_of_string>";
Example:
String str = "hey";
SOURCE CODE :
import java.io.*;
class StringHandler
{
s2=s2+s1.charAt(i);
}
System.out.println("\nThe reversed String is:\t"+s2);
s3=s2.toUpperCase();
System.out.println("\nThe uppercase of reverse string is:\t"+s3);
System.out.println("\nEnter a new string:\t");
System.out.println("\n===========================\
t");s4=br.readLine();
Output:
=====================================
pinto