Reg No: 953325104044
[Link]: 1 Constructor and Static Members
Date:
Aim:
To write a Java program demonstrating the use of parameterized constructors, constructor overloading,
and static members (variables and methods) to manage shared data across objects.
Algorithm:
Step 1: Define a class Student with instance variables (rollNo, name) and a static variable (collegeName).
Step 2: Implement a parameterized constructor to initialize instance variables.
Step 3: Implement a static method changeCollege() to update the static variable collegeName.
Step 4: Define a display method to print student details along with collegeName.
Step 5: In the main method, instantiate Student objects, invoke instance methods, and call the static
method using the class name.
Program:
class Student{
String name;
Student(String s,int a){
name=s;
}
static String college = " Unnamalai Institute of Technology ";
void display ()
{
[Link]("Name :"+ name + " CollegeName:"+college);
}
static void Changecollege( String Newcollege){
college=Newcollege;
}
}
class Static{
public static void main(String args[]){
Student s1=new Student("santo");
Student s2=new Student("anto");
[Link]("--------Initial Student Details-----");
[Link]();
[Link]();
[Link]("Anna University");
[Link]("--------Updated Student Details-----");
[Link]();
[Link]();
}
}
Output:
uit@uit-HP-Elite-7100-Microtower-PC:~$ cd santhosh
uit@uit-HP-Elite-7100-Microtower-PC:~/santhosh$ javac [Link]
uit@uit-HP-Elite-7100-Microtower-PC:~/santhosh$ java Student
----Student deteils----
Rollno:101,Name:santo,college:Unnamalai Institute of Technology
Rollno:102,Name:anto,college:Unnamalai Institute of Technology
----updated Student details----
Rollno:101,Name:santo,college:Anna University
Rollno:102,Name:anto,college:Anna University
uit@uit-HP-Elite-7100-Microtower-PC:~/santhosh$
Result:
Thus, the java program Constructor and Static Members was executed Successfully.
Reg No: 953325104044
[Link]: 2 Inheritance
Date:
Aim:
To implement single and hierarchical inheritance in Java using base and derived classes, demonstrating
code reusability and method overriding.
Algorithm:
Step 1: Create a superclass Employee with fields id and name, and a method displayEmpDetails().
Step 2: Create a subclass Developer that extends Employee and adds a field programmingLanguage.
Step 3: Override the displayEmpDetails() method using super to call superclass properties.
Step 4: In the main method, instantiate Developer, assign values, and execute methods to verify
inheritance.
Program:
class Empdetails{
String name;
int empid;
double salary;
Empdetails(String n,int r,double s){
[Link]=n;
[Link]=r;
[Link]=s;
}
void display(){
[Link]("Employee Name:"+name);
[Link]("Employee id:"+empid);
[Link]("Employee Salary:"+salary);
}
}
class Empskills extends Empdetails{
String skills;
Empskills(String n,int r,double s,String skills)
{
super(n,r,s);
[Link]=skills;
}
void display(){
[Link]();
[Link]("Employee Skills:"+skills);
}
public static void main(String []args)
{
Empskills e1=new Empskills("Santhosh",240,75000,"FullStack Developer");
[Link]("--- Employee Profile ---");
[Link]();
}
}
Output:
C:\Users\santhosh\OneDrive\Desktop\javatest>javac [Link]
C:\Users\santhosh\OneDrive\Desktop\javatest>java Empskills
--- Employee Profile ---
Employee Name:Santhosh
Employee id:240
Employee Salary:75000.0
Employee Skills:FullStack Developer
C:\Users\santhosh\OneDrive\Desktop\javatest>
Result:
Thus, the java program Inheritance was executed Successfully.