0% found this document useful (0 votes)
8 views9 pages

Java Ass

The document is an assignment for a Java Programming course at Coimbatore Institute of Technology, detailing three questions on access modifiers, encapsulation, and inheritance. It includes code examples demonstrating the use of public, protected, default, and private access modifiers, as well as encapsulation through getter and setter methods. Additionally, it explains the rules of method overriding in Java with examples of a superclass and subclass relationship.

Uploaded by

preethisnj09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Java Ass

The document is an assignment for a Java Programming course at Coimbatore Institute of Technology, detailing three questions on access modifiers, encapsulation, and inheritance. It includes code examples demonstrating the use of public, protected, default, and private access modifiers, as well as encapsulation through getter and setter methods. Additionally, it explains the rules of method overriding in Java with examples of a superclass and subclass relationship.

Uploaded by

preethisnj09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COIMBATORE INSTITUTE OF TECHNOLOGY

(An Autonomous Institution Affiliated to Anna University)


COIMBATORE – 641014.

REGISTER NUMBER : 2303717672622045

NAME : PREETHI SENTHIL KUMAR

DEPARTMENT : MSc SOFTWARE SYSTEMS

BATCH : 2023 – 2028

COURSE CODE : 20MSS45

COURSE NAME :JAVA PROGRAMMING


ASSIGNMENT – 2

Question1: understanding access modifiers


Java program demonstrating the four access modifiers
(public, protected, default, and private) across two classes in
different packages.
Package 1: [Link].package1
package [Link].package1;
public class AccessModifiersDemo {
public int publicVar = 10;
protected int protectedVar = 20;
int defaultVar = 30;
private int privateVar = 40;
public void accessWithinSameClass() {
[Link]("Accessing within the same class:");
[Link]("Public: " + publicVar);
[Link]("Protected: " + protectedVar);
[Link]("Default: " + defaultVar);
[Link]("Private: " + privateVar);
}
}
Package 2: [Link].package2
package [Link].package2;
import [Link];
public class SubClass extends AccessModifiersDemo {
public void accessFromSubClass() {
[Link]("Accessing from subclass in a different
package:");
[Link]("Public: " + publicVar); // Accessible
[Link]("Protected: " + protectedVar); // Accessible
// [Link]("Default: " + defaultVar); // Not accessible
// [Link]("Private: " + privateVar); // Not accessible
}
}
public class NonSubClass {
public void accessFromNonSubClass() {
AccessModifiersDemo demo = new AccessModifiersDemo();
[Link]("Accessing from non-subclass in a different
package:");
[Link]("Public: " + [Link]); // Accessible
// [Link]("Protected: " + [Link]); // Not
accessible
// [Link]("Default: " + [Link]); // Not
accessible
// [Link]("Private: " + [Link]); // Not
accessible
}
}
Main Class to Test Access Modifiers
package [Link].package1;
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
AccessModifiersDemo demo = new AccessModifiersDemo();
[Link](); // Accessing within the same
class
SubClass subClass = new SubClass();
[Link](); // Accessing from subclass in a
different package
NonSubClass nonSubClass = new NonSubClass();
[Link](); // Accessing from non-
subclass in a different package
}
}

Question 2: Implementing Encapsulation


Java class Student that demonstrates encapsulation using
private fields and public getter/setter methods with validation.
public class Student {
private String name;
private int age;
private char grade;
public String getName() {
return name;
}

public void setName(String name) {


[Link] = name;

public int getAge() {


return age;
}
public void setAge(int age) {
if (age > 0) {
[Link] = age;
} else {
[Link]("Invalid age! Age must be greater than 0.");
}

public char getGrade() {


return grade;
}
public void setGrade(char grade) {
if (grade >= 'A' && grade <= 'F') {
[Link] = grade;
} else {
[Link]("Invalid grade! Grade must be between 'A'
and 'F'.");
}
}
}
Testing the Student Class
public class Main {
public static void main(String[] args) {
Student student = new Student()
// [Link] = "John"; // Error: name has private access
[Link]("John");
[Link](20);
[Link]('A');
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
[Link]("Grade: " + [Link]());
[Link](-5); // Invalid age
[Link]('G'); // Invalid grade
}
}
Question 3: Access Modifiers and Inheritance
Java program demonstrating access modifiers and inheritance
with a superclass Animal and a subclass Dog.
Package 1: [Link]
package [Link];

public class Animal {


private void privateMethod() {
[Link]("Private method in Animal");
}
protected void protectedMethod() {
[Link]("Protected method in Animal");
}
public void publicMethod() {
[Link]("Public method in Animal");
}
void defaultMethod() {
[Link]("Default method in Animal");
}
}
Package 2: [Link]
package [Link];
import [Link];
public class Dog extends Animal {
@Override
protected void protectedMethod() {
[Link]("Overridden protected method in Dog");
}
@Override
public void publicMethod() {
[Link]("Overridden public method in Dog");
}
// Cannot override private or default methods from superclass
// @Override
// private void privateMethod() { } // Error: Cannot override private
method
// @Override
// void defaultMethod() { } // Error: Cannot override default
method
}
Testing Overridden Methods
package [Link];
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Calls overridden method in Dog
[Link](); // Calls overridden method in Dog
// [Link](); // Error: privateMethod() is not
accessible
// [Link](); // Error: defaultMethod() is not
accessible
}
}

Explanation of Overriding Rules


1. Private Methods: Cannot be overridden because they are not
accessible outside the class.
2. Protected Methods: Can be overridden in a subclass, even in a
different package.
3. Public Methods: Can be overridden in a subclass, even in a
different package.
4. Default Methods: Cannot be overridden in a subclass in a
different package because they are package-private.

You might also like