0% found this document useful (0 votes)
6 views7 pages

Dhaka International University

Uploaded by

jannatjahan0195
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)
6 views7 pages

Dhaka International University

Uploaded by

jannatjahan0195
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
You are on page 1/ 7

Dhaka International University

Lab Report

Department: Computer Science and Engineering.

Course Title: Object-Oriented Programming Languages


Lab. Course No: 0613-202

Lab Report No: 04

Submitted By: Submitted To:


Name: Jannat Jahan Holy Name: Prof. Md. Abdul Based
Roll: 21
Batch: 94
Reg no.: CS-D-94-23-125524
Title: “Understanding Access Modifiers, Abstraction, and Interfaces in Java”
Introduction:
This week in our Java course, we discussed some important concepts that help us write better and
more secure code. We learned about the super keyword, which works with classes, variables, methods,
and constructors. We also explored access modifiers, which control how variables and methods are
used in different parts of our program. Then, we looked at abstraction, which helps us hide complex
details and show only the important parts using the abstract keyword. Finally, we discussed interfaces,
which define rules that other classes must follow. Understanding these concepts will help us write
cleaner Java code.

Super Keyword:
In Java, the super keyword is used to refer to the immediate parent class. We use it to access the
properties and methods of the parent class, even if they are overridden or hidden by subclasses.

Code Implementation:
public class B {
int a = 10;
}

public class C extends B {


int a = 15;

void show() {
System.out.println(super.a);
}

public static void main(String[] args) {


C ob = new C();
ob.show();
}
}

Output:

Access Modifiers:
Access modifiers are like rules that decide who can see and use the parts of your code, like variables
and methods. In Java, there are different types of access modifiers. Let's start with Default Access
Modifier

1. Default Access Modifier:


When we don’t specify any access modifier, it’s considered the default one. This means that the
member (like a variable or method) is only accessible within the same class and package. If we try to
use it outside the class or in a different package, it won’t work.

Code Implementation:
class A {
int a = 9;
int sum() {
int b = a + a;
return b;
}
}
class B extends A {
public static void main(String[] args) {
A ob = new A();
System.out.println(ob.sum());
}
}

Output:

2. Private Access Modifier:


When we use the private modifier, it means that the variable or method can be used only within the
same class. No other class, even if it is a subclass, can access or modify that private member.

Code Implementation:

package Acess;
public class AA {
private void display() {
System.out.println("DIU");
}
public static void main(String[] args) {
AA ob = new AA();
ob.display();
}
}
Output:

3. Public Access Modifier:


Public modifier means that the variable or method can be used
anywhere in the program. It can be accessed from any class,
whether in the same package or a different package.
Code Implementation:

package Acess;

public class AA {
public void display() {
System.out.println("Batch D-94");
}
}

package TEst;
import Acess.AA;

public class C {
public static void main(String[] args) {
AA ob = new AA();
ob.display();
}
}

Output:

4. Protected Access Modifier:


The protected modifier allows variables or methods to be used
in the same package and in subclasses, even if the subclass
is in a different package.

Code Implementation:

package Acess;
public class AA {
protected void display() {
System.out.println("CSE");
}
}
package TEst;
import Acess.AA;
public class C extends AA {
public static void main(String[] args) {
C ob = new C();
ob.display();
}

Output:
Abstraction:
Abstraction in Java helps us hide the complex details of how things work
and show only the important features. This makes our code easier to
understand and use. We use the abstract keyword to create abstract
classes and abstract methods.

Code Implementation:

package Acess;

public abstract class AA {


abstract void display();
}

package Acess;

public class B extends AA {


@Override
void display() {
System.out.println("D-94");
}

public static void main(String[] args) {


AA ob = new B();
ob.display();
}
}

Output:

Interface:
An interface in Java is like a contract. It tells classes what methods they must have, but it doesn’t
provide the actual code for those methods. All methods in an
interface are abstract (without any body), and you can also have
constants in it.
To use an interface, a class must implement it using the implements keyword. When a class implements
an interface, it promises to provide the code for all the methods in the interface.

Code Implementation:

package Acess;

public interface I1 {
void m();
}

package Acess;

public interface I2 {
void m();
}

package Acess;

public class A implements I1, I2 {


public void m() {
System.out.println("CSE");
}

public static void main(String[] args) {


A ob = new A();
ob.m();
}
}

Output:

Conclusion:
In this lesson, we learned about key Java concepts that
make our code cleaner, more organized, and secure. We explored access modifiers, which control
how we access different parts of our classes, and how the super keyword helps with inheritance. We
also saw how abstraction lets us hide complex details and focus on the important parts, and how
interfaces allow us to define a contract for classes to follow. Mastering these concepts will help us
write more efficient and flexible code as we continue learning Java.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy