0% found this document useful (0 votes)
15 views17 pages

DANISH

The document contains code examples demonstrating the use of inner classes in Java. There are 3 activities: 1. The first activity shows an example of an inner class defined within an outer class. An object of the inner class is instantiated and its method is accessed from within a method of the outer class. 2. The second activity demonstrates how to access private members of an outer class using an inner class. 3. The third activity shows an example of a method-local inner class, where the inner class is defined within a method of the outer class. An object of the method-local inner class is instantiated and its method accessed within the method.

Uploaded by

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

DANISH

The document contains code examples demonstrating the use of inner classes in Java. There are 3 activities: 1. The first activity shows an example of an inner class defined within an outer class. An object of the inner class is instantiated and its method is accessed from within a method of the outer class. 2. The second activity demonstrates how to access private members of an outer class using an inner class. 3. The third activity shows an example of a method-local inner class, where the inner class is defined within a method of the outer class. An object of the method-local inner class is instantiated and its method accessed within the method.

Uploaded by

E 4 Eagle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

NAME: DANIS EJAZ

REG NO. : FA21-BSE-074


SUBJECT: OBJECT ORIENTED PROGRAMMING
DATE: 19-DEC-2022
MA’AM: SAMIA RIAZ
(ASSIGNMENT NO. 9)
Activity 1:
class Outer_Demo{
int num;
//inner class
private class Inner_Demo{
public void print(){

System.out.println("This is an inner class");


}
}
//Accessing he inner class from the method within
void display_Inner(){

Inner_Demo inner = new Inner_Demo();


inner.print();

}
}
public class My_class{
public static void main(String args[]){
//Instantiating the outer class
Outer_Demo outer = new Outer_Demo();

//Accessing the display_Inner() method.


outer.display_Inner();

Output:

This is an inner class.


Activity 2:
Access the private members of a class using inner class.
Code:
class Outer_Demo {
//private variable of the outer class
private int num= 175;
//inner class
public class Inner_Demo{
public int getNum(){
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}

public class My_class2{


public static void main(String args[]){
//Instantiating the outer class
Outer_Demo outer=new Outer_Demo();
//Instantiating the inner class
Outer_Demo.Inner_Demo inner=outer.new Inner_Demo();
System.out.println(inner.getNum());
}
}

Output:

The value of num in the class Test is: 175


Activity 3:
Use a method-local inner class.

Code:
public class Outerclass{

//instance method of the outer class


void my_Method(){
int num = 23;

//method-local inner class


class MethodInner_Demo{
public void print(){
System.out.println("This is method inner class "+num);
}
}//end of inner class
//Accessing the inner class
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}

public static void main(String args[]){


Outerclass outer = new Outerclass();
outer.my_Method();
}
}

Output:

This is method inner class 23


Implement Vehicle as outer and owner as the inner class, the
vehicle class contains vehicle name, engine cc, model as data
members. The inner class data members are owners name, CNIC
number and phone contact of the owner.
Write down proper setters/ getters and constructors for both the classes.

1: Override the method of a class using anonymous inner class.

package assignmentinnerclasses;
abstract class vahicle_Owner{
abstract void info();

}
class vahicle{
String Name;
String Enginecc;
String Model;
vahicle(){
System.out.println("THE INFORMATION OF VAHICLE");
}
vahicle(String n,String e,String m){
this.Name=n;
this.Enginecc=e;
this.Model=m;
}

public String getName() {


return Name;
}

public String getEnginecc() {


return Enginecc;
}
public String getModel() {

return Model;
}

public void setName(String Name) {


this.Name = Name;
}

public void setEnginecc(String Enginecc) {


this.Enginecc = Enginecc;
}

public void setModel(String Model) {


this.Model = Model;
}
private class Owner{
private String Name;
private String CNIC;
private String PHNO;
Owner(){
System.out.println("THE INFORMATION OF OWNER");
}

Owner(String Name, String CNIC, String PHNO) {


this.Name = Name;
this.CNIC = CNIC;
this.PHNO = PHNO;
}

public String getName() {


return Name;
}
public String getCNIC() {
return CNIC;
}

public String getPHNO() {


return PHNO;
}

public void setName(String Name) {


this.Name = Name;
}

public void setCNIC(String CNIC) {


this.CNIC = CNIC;
}

public void setPHNO(String PHNO) {


this.PHNO = PHNO;
}

}
public class Assignmentinnerclasses {
public static void main(String[] args) {
// TODO code application logic here
vahicle_Owner vo=new vahicle_Owner(){
@Override
void info() {
throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
};

}
2: Pass an anonymous inner class as a method argument.
package assignmentinnerclasses;
import java.util.Scanner;
class vahicle{
String Name;
String Enginecc;
String Model;
vahicle(){
System.out.println("THE INFORMATION OF VAHICLE");
}
vahicle(String n,String e,String m){
this.Name=n;
this.Enginecc=e;
this.Model=m;
}

public String getName() {


return Name;
}

public String getEnginecc() {


return Enginecc;
}

public String getModel() {


return Model;
}

public void setName(String Name) {


this.Name = Name;
}

public void setEnginecc(String Enginecc) {


this.Enginecc = Enginecc;
}

public void setModel(String Model) {


this.Model = Model;
}
void information(String PName, String PHNO, String CNIC){
class Owner{
private String PName;
private String CNIC;
private String PHNO;
Owner(){
System.out.println("THE INFORMATION OF OWNER");
}

Owner(String Name, String CNIC, String PHNO) {


this.PName = Name;
this.CNIC = CNIC;
this.PHNO = PHNO;
}

public String getName() {


return Name;
}

public String getCNIC() {


return CNIC;
}

public String getPHNO() {


return PHNO;
}

public void setName(String Name) {


this.PName = Name;
}

public void setCNIC(String CNIC) {


this.CNIC = CNIC;
}

public void setPHNO(String PHNO) {


this.PHNO = PHNO;
}
}
Owner OW=new Owner();
OW.getName();
OW.getPHNO();
OW.getCNIC();
OW.setName(PName);
OW.setPHNO(PHNO);
OW.setCNIC(CNIC);
}

}
public class Assignmentinnerclasses {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
vahicle v=new vahicle();
System.out.println("ENTER NAME OF THE OWNER");
String PName = s.nextLine();
System.out.println("ENTER CNIC OF THE OWNER");
String CNIC =s.nextLine();
System.out.println("ENTER PHONE NUMBER OF THE OWNER");
String PHNO =s.nextLine();
v.information(PName, PHNO, CNIC);

}
3: Implement the inner class as static first and then as non static nested class.
package assignmentinnerclasses;
import java.util.Scanner;
class vahicle{
String Name;
String Enginecc;
String Model;
vahicle(){
System.out.println("THE INFORMATION OF VAHICLE");
}
vahicle(String n,String e,String m){
this.Name=n;
this.Enginecc=e;
this.Model=m;
}

public String getName() {


return Name;
}

public String getEnginecc() {


return Enginecc;
}

public String getModel() {


return Model;
}

public void setName(String Name) {


this.Name = Name;
}

public void setEnginecc(String Enginecc) {


this.Enginecc = Enginecc;
}

public void setModel(String Model) {


this.Model = Model;
}
static class Owner{
private static String PName;
private static String CNIC;
private static String PHNO;
Owner(){
System.out.println("THE INFORMATION OF OWNER");
}

Owner(String Name, String CNIC, String PHNO) {


this.PName = Name;
this.CNIC = CNIC;
this.PHNO = PHNO;
}

public static String getName() {


return PName;
}

public static String getCNIC() {


return CNIC;
}

public static String getPHNO() {


return PHNO;
}

public void setName(String Name) {


this.PName = Name;
}

public void setCNIC(String CNIC) {


this.CNIC = CNIC;
}

public void setPHNO(String PHNO) {


this.PHNO = PHNO;
}
public static void Display(){
System.out.println("THE NAME OF THE OWNER IS:"+PName);
System.out.println("THE CNIC OF THE OWNER IS:"+CNIC);
System.out.println("THE PHONE NUMBER OF THE OWNER IS:"+PHNO);

}
}

}
public class Assignmentinnerclasses {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
vahicle v=new vahicle();
vahicle.Owner o=new vahicle.Owner();
o.Display();
}

}
By using non-static nested inner classes

package assignmentinnerclasses;
import java.util.Scanner;
class vahicle{
String Name;
String Enginecc;
String Model;
vahicle(){
System.out.println("THE INFORMATION OF VAHICLE");
}
vahicle(String n,String e,String m){
this.Name=n;
this.Enginecc=e;
this.Model=m;
}

public String getName() {


return Name;
}

public String getEnginecc() {


return Enginecc;
}
public String getModel() {
return Model;
}

public void setName(String Name) {


this.Name = Name;
}

public void setEnginecc(String Enginecc) {


this.Enginecc = Enginecc;
}

public void setModel(String Model) {


this.Model = Model;
}
class Owner{
private String PName;
private String CNIC;
private String PHNO;
Owner(){
System.out.println("THE INFORMATION OF OWNER");
}

Owner(String Name, String CNIC, String PHNO) {


this.PName = Name;
this.CNIC = CNIC;
this.PHNO = PHNO;
}

public String getName() {


return PName;
}

public String getCNIC() {


return CNIC;
}

public String getPHNO() {


return PHNO;
}

public void setName(String Name) {


this.PName = Name;
}

public void setCNIC(String CNIC) {


this.CNIC = CNIC;
}

public void setPHNO(String PHNO) {


this.PHNO = PHNO;
}
public void Display(String PName,String CNIC, String PHNO){
System.out.println("THE NAME OF THE OWNER IS:"+PName);
System.out.println("THE CNIC OF THE OWNER IS:"+CNIC);
System.out.println("THE PHONE NUMBER OF THE OWNER IS:"+PHNO);

}
}

}
public class Assignmentinnerclasses {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
vahicle v=new vahicle();
vahicle.Owner o=vahicle.new Owner();
System.out.println("ENTER NAME OF THE OWNER");
String PName = s.nextLine();
System.out.println("ENTER CNIC OF THE OWNER");
String CNIC =s.nextLine();
System.out.println("ENTER PHONE NUMBER OF THE OWNER");
String PHNO =s.nextLine();
o.Display(PName, CNIC, PHNO);
}

You might also like