0% found this document useful (0 votes)
68 views

CSE1007 Java Programming Lab Assesment-4 Shistata Subedi 19BCE2653

This document contains code for two Java programming lab assessment questions: 1. The first question involves creating an abstract class with an abstract and non-abstract method, inheriting from it with a subclass that implements the abstract method, and calling methods on both. 2. The second question demonstrates nested classes by creating a static nested class within an outer class, declaring static and non-static variables, and calling a method on the nested class from the main method.

Uploaded by

Shistata Subedi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

CSE1007 Java Programming Lab Assesment-4 Shistata Subedi 19BCE2653

This document contains code for two Java programming lab assessment questions: 1. The first question involves creating an abstract class with an abstract and non-abstract method, inheriting from it with a subclass that implements the abstract method, and calling methods on both. 2. The second question demonstrates nested classes by creating a static nested class within an outer class, declaring static and non-static variables, and calling a method on the nested class from the main method.

Uploaded by

Shistata Subedi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE1007

Java Programming LAB ASSESMENT-4


SHISTATA SUBEDI 19BCE2653

Q.1 Write a Java program to implement an abstract class.

ALGORITHM:
1. Create an abstract class food.
2. Create abstract method favourite() and non-abstract method eat () in the abstract class.
3. Declare a static method healthy().
4. Declare a public function eat().
5. Inherited a subclass pizza from the superclass food.
6. Create a Subclass pizza that provides the implementation for the abstract method favourite().
7. In the main function, create an instance object of pizza class.
8. Use object food to call method healthy() and p1 for the pizza class to call method favourite()
and method eat()
CODE:
abstract class food{ //created abstract class food
abstract void favourite(); //The class contains an abstract method favourite()and non-abstract method
eat()
public static void healthy() {
System.out.println("Yes, I am healthy and fit.");
}
public void eat() {
System.out.println("I love food.");
}
}
class pizza extends food { //We have inherited a subclass pizza from the superclass food
public void favourite() { //Subclass pizza provides the implementation for the abstract method
favourite()

System.out.println("Pizza is everything");
}
}
class Eat {
public static void main(String[] args) { //create an object of pizza class
pizza p1 = new pizza();
food.healthy();
p1.favourite(); //Used object p1 for the pizza class to call method favourite() and method eat()
p1.eat();
}
} OUTPUT:
Q.2 Write a Java Program to demonstrate nested classes.
ALGORITHM:
1. Create a class named OuterClass.
2. Declare an integer type static, non-static and a private static variable.
3. Create a static nested class.
4. Declare a display function and print the variables.
5. Create a main function declare an object and call the static nested method.
6. Declare an instance object for the display function.

CODE:
class OuterClass
{
static int a = 25; //static member
int b = 35; //non-static member
private static int c = 40;
static class StaticNestedClass //static nested class
{
void display() {
System.out.println("The value of a is = " + a); //can access static member of Outerclass
System.out.println("The value of c is = " + c); //can access display private static member of Outerclass
//Can't compile for b as static nested class cannot access non static member directly
}
}
}
public class maths {//Main
public static void main(String[] args)
{
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
OUTPUT:

You might also like