Professional Documents
Culture Documents
Kru Week2 Report
Kru Week2 Report
Work done in last Week with description (Attach supporting Documents Like
Diagram, Data Dictionary, Screen Shot of each Work):
1. Understand and apply the four fundamental OOP concepts in Java.
2. Develop simple Java programs that demonstrate the use of classes, objects,
inheritance, polymorphism, and abstraction.
3. Learn about access modifiers and their role in encapsulation.
Date: Date:
Encapsulation and Classes:
What is Encapsulation?
Encapsulation is a programming technique that bundles data with the methods that operate on it. It
can also limit direct access to some data, such as an object's components. Encapsulation can be used
to hide the state or values of a structured data object inside a class. It can also prevent external code
from being concerned with the internal workings of an object.
Data hiding is when data is hidden so it will be safe from accidental manipulation. A programmer
uses hiding data to put it in a class and make it private so that it can't be accessed mistakenly by
functions outside the class.
Getters and setters are used to protect your data, particularly when creating classes. For each
instance variable, a getter method returns its value while a setter method sets or updates its
value. Given this, getters and setters are also known as accessors and mutators, respectively.
Inheritance:
Basics of Inheritance and it’s types?
Inheritance is a feature of object-oriented programming (OOP) that allows a class to inherit the
properties and methods of another class. This is useful for creating new classes that are similar to
existing classes, but with some modifications.
Single inheritance: A class can inherit from only one parent class.
Multilevel inheritance: A class can inherit from another class, which itself inherits from a
third class, and so on.
The ‘extends’ keyword in Java is used to create a subclass (child class) that inherits the properties and
methods of another class (parent class). This is known as inheritance. The subclass can then add its
own unique properties and methods.
Method overriding:
Method overriding in Java is a feature that allows a subclass to provide a specific implementation of
a method that is already provided by one of its super classes. This allows the subclass to inherit the
methods of the superclass and modify them as needed.
To override a method, the subclass must declare a method with the same name, return type, and
parameter list as the method in the superclass. The @Override annotation is used to indicate that
the method is overriding a method in the superclass.
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to
access the superclass constructor. The most common use of the super keyword is to eliminate the
confusion between super classes and subclasses that have methods with the same name.
Polymorphism:
Abstract class
Subclasses of an abstract class must provide implementations for all of the abstract methods
in the parent class.
Abstract classes are used to provide a common definition for a set of related classes.
Abstract method
Subclasses of an abstract class must provide implementations for all of the abstract methods
in the parent class.
Abstract methods are used to provide a common definition for a set of related methods.
An interface in Java is a blueprint of a class. It has static constants and abstract methods. The
interface doesn't contain any instance variables or method bodies; it only specifies the
methods that a class must implement.
Interfaces are used to achieve abstraction. For example, you can create an interface called
Drawable that specifies the methods that a class must implement to be drawn on the
screen. Any class that implements the Drawable interface must implement the draw()
method.
Interfaces are also used to achieve polymorphism. For example, you can create a List
interface that specifies the methods that a class must implement to be a list. Any class that
implements the List interface can be used as a list, regardless of its concrete type.
Java access modifiers control the accessibility of fields, methods, classes, and
interfaces. There are four access modifiers in Java:
Private: The most restrictive access modifier. Private members are only accessible within the
class in which they are declared.
Default: Also known as package-private. Default members are accessible within the same
package, but not from outside the package.
Protected: Protected members are accessible within the same package and by subclasses in
other packages.
Public: The least restrictive access modifier. Public members are accessible from anywhere.
Constructors in Java:
A constructor in Java is a special method that is used to initialize objects. It is invoked at the time of
object creation and is used to set the initial values of the object's fields.
Constructors are similar to methods in that they can have parameters and can perform any valid Java
code. However, there are some key differences between constructors and methods:
Constructors must have the same name as the class they are in.
Constructors do not have a return type.
Constructors are invoked automatically when an object is created, using
the new keyword.
The role of constructors in OOP, including parameterized constructors and constructor overloading: