OOP Assignment I
OOP Assignment I
Programming
Assignment 1: OOP
Subject: OOP
Class: BSCS
Assessment Type: Assignment Part I
Marks: 30
Assessment Weighting: 15%
Submission Method: Google Class Room
Date Set: 11-OCT-2022
Submission Date: 11:59pm, 23-OCT-2022
Provisional Feedback Release Date: 02:00pm, 25-OCT-2022
1. Overview:
Java is an object oriented language and some concepts may be new. Take breaks when
needed, and go over the examples as many times as needed.
1.1. Encapsulation
Encapsulation is one of the four fundamental OOP Concepts. The other three are
inheritance, polymorphism and abstraction. Encapsulation in Java is a mechanism of wrapping
the data (variables) and code acting on the data (methods) together as a single unit. In
encapsulation, the variables of a class will be hidden from other classes, and can be accessed
only through the methods of their current class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java –
• Declare the variables of a class as private.
• Provide public setter and getter methods to modify and view the variables values.
class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}
myCar.honk();
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
1.5. Polymorphism
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own
type and for the class Object.
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets
us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks. This allows us to perform a
single action in different ways. For example, think of a superclass called Animal that has a
method called animalSound().
Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own
implementation of an animal sound (the pig oinks, and the cat meows, etc.):
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
To access the interface methods, the interface must be "implemented" (kinda like
inherited) by another class with the implements keyword (instead of extends). The body of the
interface method is provided by the "implement" class:
1.10. Interface Program Example
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
}
}
2. Assignment
An overview of Java Programming concept is given in the previous sections. Understand
these few examples well and solve the following questions.
**Good Luck**