CSE1007 Java Programming Lab Assesment-4 Shistata Subedi 19BCE2653
CSE1007 Java Programming Lab Assesment-4 Shistata Subedi 19BCE2653
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: