0% found this document useful (0 votes)
227 views5 pages

Object Oriented Programming Lab Report

This document contains a Java code sample that demonstrates class member access controls. It defines a Protection class with fields of different access modifiers (private, no modifier, protected, public). It then defines subclasses and classes in the same/different packages that demonstrate which members can be accessed under each scenario. The output shows the values that can be accessed from each class, following the rules of Java's access modifiers.

Uploaded by

Joy Pal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
227 views5 pages

Object Oriented Programming Lab Report

This document contains a Java code sample that demonstrates class member access controls. It defines a Protection class with fields of different access modifiers (private, no modifier, protected, public). It then defines subclasses and classes in the same/different packages that demonstrate which members can be accessed under each scenario. The output shows the values that can be accessed from each class, following the rules of Java's access modifiers.

Uploaded by

Joy Pal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)

Faculty of Sciences and Engineering

Semester: (Spring, Year:2021), B.Sc. in CSE (Day)

Course Title: Object Oriented programing.

Course Code: CSE 201 Section: 203 DA

Name: Md. Johirul Islam

ID: 203002006

Type : Assignment

Submission Date : 09/12/2021.

Course Teacher’s Name : Ayesha Khatun


TABLE : Class Member Access
Implementation

Private No Modifier Protected public

Same class Yes Yes Yes Yes

Same No Yes Yes Yes


package
subclass
Same No Yes Yes Yes
package
non-subclass

Different No No Yes Yes


package
subclass
Different No No No Yes
package
non-subclass

Java code:

This is file Protection.java:

package p1;

public class Protection {

int n = 1;

private int n_pri = 2;

protected int n_pro = 3;

public int n_pub = 4;


public Protection() {

System.out.println("base constructor");

System.out.println("n = " + n);

System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);

This is file Derived.java:

package p1;

class Derived extends Protection {

Derived() {

System.out.println("derived constructor");

System.out.println("n = " + n);

// class only

// System.out.println("n_pri = "4 + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);

This is file SamePackage.java:

package p1;

class SamePackage {

SamePackage() {

Protection p = new Protection();


System.out.println("same package constructor");

System.out.println("n = " + p.n);

// class only

// System.out.println("n_pri = " + p.n_pri);

System.out.println("n_pro = " + p.n_pro);

System.out.println("n_pub = " + p.n_pub);

This is file Protection2.java:

package p2;

class Protection2 extends p1.Protection {

Protection2() {

System.out.println("derived other package constructor");

// class or package only

// System.out.println("n = " + n);

// class only

// System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);

This is file OtherPackage.java:

package p2;

class OtherPackage {

OtherPackage() {
p1.Protection p = new p1.Protection();

System.out.println("other package constructor");

// class or package only

// System.out.println("n = " + p.n);

// class only

// System.out.println("n_pri = " + p.n_pri);

// class, subclass or package only

// System.out.println("n_pro = " + p.n_pro);

System.out.println("n_pub = " + p.n_pub);

Output:
file Protection.java : 1 2 3 4

file Derived.java: 1 3 4

file Same Package: 1 3 4

file Protection2: 3 4

file Other Package: 4

You might also like