Overloading in JAVA

Method Overloading and Constructor Overloading

/* 
Create Class Student with attributes like rollno and name, where You have to implement the concept of Method Overloading and Constructor Overloading.

Constructor Overloading and Static member concept :
Create Constructor in such a way where if User provide (rollno, name) then students object should be created with same. If user provide only name then next possible rollno should be assigned to student. 

Method Overloading :
Create method exam where marks of 3 subject will be supplied. If student get <50 marks in any of the subject then result should be declared as a Fail with no of ATKT in a exam.
If students appeared in exam of only one or two subject then using appropriate method should be called with different no of parameters 
*/


class Student
{
                int rollno;
                String name;
                static int noofstud;

// Constructor Overloading
                Student()
                {
                }

                Student(int roll,String sname)
                {
                                this.rollno=roll;
                                this.name=sname;
                                noofstud=roll+1;
                }
                Student(String sname)
                {
                                this.rollno=noofstud;
                                this.name=sname;
                }

//Method Overloading
                void exam(int sub1,int sub2,int sub3)
                {
                                int fail=0;
                                if(sub1<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub2<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub3<50)
                                {
                                                fail=fail+1;
                                }

                                if(fail==0)
                                {
                                                System.out.println("Congratulations "+ this.name + " You have successfully clear the Exam.....!");
                                }
                                else
                                {
                                                System.out.println("Sorry "+ this.name + " You are failed in "+fail+" Subject");
                                }
                }

                void exam(int sub1,int sub2)
                {
                                exam(sub1,sub2,0);
                }

                void exam(int sub1)
                {
                                exam(sub1,0,0);
                }

                public static void main(String args[])
                {
                                Student s1=new Student(1,"Ajay");
                                Student s2=new Student("Mitesh");
                                Student s3=new Student("Mitesh");
                                                               
                                s1.exam(50,60,12);
                                s2.exam(60,70);
                                s3.exam(30);
                }

}

Java Exception Handling

/ * 

Create a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “AgeNotWithinRangeException”. If name contains numbers or special symbols raise exception “NameNotValidException”. Define the two exception classes.

*/

import java.lang.Exception;
class AgeNotWithinRange extends Exception{
String msg; AgeNotWithinRange(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class NameNotValidException extends Exception{
String msg; NameNotValidException(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class Student{

 
int rollno;
int age;
String name;
String course;

Student(int r,String n, int a, String c) throws AgeNotWithinRange,NameNotValidException{ 
if(a < 15 || a > 21){  
throw new AgeNotWithinRange(String.valueOf(a)); 
for(int i = 0;i < n.length();i++){  
if(!(Character.isLetter(n.charAt(i)))){      
throw new NameNotValidException(String.valueOf(i));  
this.rollno = r; 
this.name = n; 
this.age = a;
this.course = c; 
System.out.println("Object Created successfully.");
}

 public static void main(String args[]){

 
try{   
Student s1 = new Student(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]),args[3]);
          }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are UG.");
}

 
try{   
Student s2 = new Student(Integer.parseInt(args[4]),args[5],Integer.parseInt(args[6]),args[7]);
 }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are PG.");
} 
}
}

Developed at : MCA - 3 (A), Department of MCA, B H Gardi College, Rajkot

Example of Multiple Inheritance in JAVA

abstract class A
{
            {
            System.out.println("Abstract Class Object is Created");
            }
           
abstract void display();
}

interface T
{
int i=10;
void print();
}

class B extends A implements T
{

void display()
            {
            System.out.println("Display from Derived Class");
            }

public void print()
            {
            System.out.println("Interface Implementation");
            }

public static void main(String s[])
            {
            B b=new B();
            b.display();
            b.print();
            System.out.println(T.i);
            }

}

Shadow Variable Concept

class A
{
int k; 
            {
            System.out.println("Super Class Object is Created");
            }

            A(int j)
            {
            this.k=j;
            System.out.println("Super Class Object is Created with Value K= "+k);
            }
}

class B extends A
{
int k; //shadow variable

            {
            System.out.println("Derived Class Object is Created");
            }
           
            B(int i,int j)
            {
            super(i);
            this.k=j;
            System.out.println("Base Value = "+ super.k+" Derived Value = "+k);
            }
public static void main(String a[])
            {
            System.out.println("Test - 1 : Constructor");
            B b2=new B(20,30);
            }
}

OUTPUT:
E:\java\2015\Lecture\Lecture-2\Shadow Variable>javac A.java

E:\java\2015\Lecture\Lecture-2\Shadow Variable>java B
Test - 1 : Constructor
Super Class Object is Created
Super Class Object is Created with Value K= 20
Derived Class Object is Created

Base Value = 20 Derived Value = 30

Single Inheritance and Super Keyword

class A
{
int k;
            A()
            {
            System.out.println("Super Class Object is Created");
            }
            A(int j)
            {
            this.k=j;
            System.out.println("Super Class Object is Created with Value K= "+this.k);
            }
}

class B extends A
{
int val;
            B()
            {
            System.out.println("Derived Class Object is Created");
            }
            B(int i)
            {
            this.val=i;
            System.out.println("Derived Class Object is Created with Value = "+this.val);
            }
            B(int i,int j)
            {
            super(i);
            this.val=j;
            System.out.println("Derived Class Object is Created with Value = "+this.val);
            }
public static void main(String a[])
            {
            System.out.println("Test - 1 : Default Constructor");
            B b=new B();
            System.out.println();
            System.out.println("Test - 2 : Parameterized - Default Constructor");
            B b1=new B(10);
            System.out.println();
            System.out.println("Test - 3 : Parameterized - Parameterized Constructor");
            B b2=new B(20,30);
            }
}

OUTPUT:
E:\java\2015\Lecture\Lecture-2>javac A.java

E:\java\2015\Lecture\Lecture-2>java B
Test - 1 : Default Constructor
Super Class Object is Created
Derived Class Object is Created

Test - 2 : Parameterized - Default Constructor
Super Class Object is Created
Derived Class Object is Created with Value = 10

Test - 3 : Parameterized - Parameterized Constructor
Super Class Object is Created with Value K= 20

Derived Class Object is Created with Value = 30