Dhaka International University
Dhaka International University
Lab Report
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;
}
void show() {
System.out.println(super.a);
}
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
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:
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:
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:
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;
package Acess;
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;
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.