Assignment 5: Method Calling, private
Goal:
Learning Method Calling, Accessing static, non-static variables from other classes, private modifier.
Steps: Create a class Called School.
Have non-static variables as below.
int mark;
private int salary;
Have static variable as below.
static String school_name = "St. Antony's Primary School";
Define non-static methods mentioned below
void conduct_exams()
- Have a print statement inside this method
void publish_results(int mark)
- Have a print statement inside this method to print mark Now, create another class called Teacher Create main method inside Teacher class Create an instance(object) for School class [Object name -> teacher] Using 'teacher' object, call conduct_exams() method Using 'teacher' object, call publish_results() method and pass 75 as argument here. Print school_name Try to access private variable salary in Teacher class and note down the error message.
package B14;
public class School {
int mark;
private int salary = 20000;
static String school_name = "st antonys primary school";
public static void main(String[] args) {
// TODO Auto-generated method stub
}
void condunt_exam() {
System.out.println("conduct exam");
}
void publish_result(int mark) {
System.out.println("exam result" + mark);
}
public void salary() {
System.out.println("salary" + salary);
}
}
package B14;
public class Teacher extends School{
public static void main(String[] args)
{
School teacher = new School();
teacher.condunt_exam();
teacher.publish_result(75);
teacher.salary();
System.out.println("school name"+ school_name);
}
}
If we declare salary method as public we can get output,if private method means we can access within the class only ,we cannot access from other class.
Output:
conduct exam
exam result75
salary20000
school namest antonys primary school
Assignment 6:
GOAL: Learning private, default and public Access Modifiers, Creating Package and understanding its usage, Calling Methods with/without arguments.
- Check if you can create private class
- Check if you can create private main method
- Check if you can create Method local variable as private.
Task:
- Create a package called bank.chennai.
- Create a public class called 'SBI'.
- Have default non-static variables String empName, empId.
- Have default static variable branch_name = 'chennai'
- Create two default, non-static methods get_loan(int amount), create_account() with void as return datatype.
- Now, in the same package(bank.creditcard), create one more default class called Account_Holder.
- Have main method in this class.
- Try to access all static, non-static variables and non-static methods in SBI class.
- Create another package called bank.madurai.
- In this package, create default class called Account_Holder_Madurai.
- Have main method in this class.
- Try to access all static, non-static variables and non-static methods in SBI class.
- Note down the Errors and rectify those errors and make sure this class gives output without any error.
package Bank.chennai;
public class SBI {
public String empName = "name";
public int empid = 32;
public static String branchName = "chennai";
public void get_loan(int amount) {
System.out.println("amount" + amount);
}
public void create_account() {
System.out.println("create");
}
public static void branch() {
System.out.println("leave" + branchName);
}
}
package Bank.chennai;
public class Account_Holder extends SBI{
public static void main(String[] args) {
// TODO Auto-generated method stub
Account_Holder account=new Account_Holder();
account.get_loan(10000);
account.create_account();
Account_Holder.branch();
System.out.println( "empname" + account.empName);
System.out.println("empid" + account.empid);
System.out.println("branchName" + branchName );
}
}
Output:
amount10000
create account
branch namechennai
empnameneel
empid32
branchNamechennai
Import Statement in Java
Import statement in Java is helpful to take a class or all classes visible for a program specified under a package, with the help of a single statement. It is pretty beneficial as the programmer do not require to write the entire class definition. Hence, it improves the readability of the program.
Syntax 1:
import package1[.package2].(*);
Syntax 2:
import package1[.package2].(myClass);
Reference:https://www.geeksforgeeks.org/import-statement-in-java/
imported from package Bank.Chennai
package Bank.madurai;
import Bank.chennai.SBI;
public class Account_holder_Madurai{
public static void main(String[] args) {
SBI acc = new SBI();
int val = 34000;
acc.get_loan(val);
acc.create_account();
SBI.branch();
System.out.println(acc.empName);
System.out.println(acc.empid);
System.out.println(SBI.branchName);
}
}
Output:
amount34000
create account
branch namechennai
neel
32
chennai
Top comments (0)