0% found this document useful (0 votes)
8 views11 pages

Understanding Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses classes and objects to represent real-world entities, offering advantages like improved code structure, maintainability, and reusability. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which facilitate efficient software design and development. The document also covers class creation, object instantiation, and the use of interfaces, along with homework assignments to reinforce understanding.

Uploaded by

mrayamread
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)
8 views11 pages

Understanding Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses classes and objects to represent real-world entities, offering advantages like improved code structure, maintainability, and reusability. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which facilitate efficient software design and development. The document also covers class creation, object instantiation, and the use of interfaces, along with homework assignments to reinforce understanding.

Uploaded by

mrayamread
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

Object Oriented Programming (OOP) Represent the real world

OOP is a method of programming based on a hierarchy of classes, and well-defined and


cooperating objects. It has several advantages over procedural programming:
§ OOP is faster and easier to execute
§ OOP provides a clear structure for the programs
§ OOP makes the code easier to maintain, modify and debug
§ OOP makes it possible to create full reusable applications with less code and shorter
development time.

Classes and objects are the two main aspects of object-oriented programming.

A class is a structure that defines the data and the methods to work on that data. It can contain
any of the following variable types.
§ Local variables: Variables defined inside methods, constructors or blocks are called
local variables. The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
§ Instance variables: Instance variables are variables within a class but outside any
method. These variables are instantiated when the class is loaded. Instance variables
can be accessed from inside any method, constructor, or blocks of that class.
§ Class variables: Class variables are variables declared within a class, outside any
method, with the static keyword.

Creating an Object:
A class provides the blueprints for objects. So basically, an object is created from a class. In
Java, the keyword new is used to create new objects. There are three steps when creating an
object from a class:
§ Declaration: A variable declaration with a variable name with an object type.
§ Instantiation: The 'new' keyword is used to create the object.
§ Initialization: The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.

Example,
UML Class Diagram

In the UML, each class is modelled in a class diagram as a rectangle with three compartments.
The top compartment contains the name of the class centred in boldface type. The middle
compartment contains the class’s attributes, which correspond to instance variables. The
bottom compartment contains the class’s operations, which correspond to methods in Java.
Constructors
It is like a method but is used only at the time an object is created to initialize the object’s data.
§ Constructor name == the class name
§ No return type – never returns anything
§ All classes need at least one constructor. If you don’t write one, Java compiler will
not create a default constructor for that class
Classname()
{

Static
• Applies to variables and methods
• Means
– Is defined for the class declaration,
– Is not unique for each instance

Note

• Each class declaration that begins with the access modifier public.
• classes, which specify the types of objects, are reference types.
• Every class declaration contains keyword class followed immediately by the class’s name.
• A method declaration that begins with keyword public indicates that the method can be
called by other classes declared outside the class declaration.
• By convention, method names begin with a lowercase first letter and all subsequent words
in the name begin with a capital first letter.
• Empty parentheses following a method name indicate that the method does not require any
parameters to perform its task.
• Every method’s body is delimited by left and right braces ({ and }).
• The method’s body contains statements that perform the method’s task. After the
statements execute, the method has completed its task.
• When you attempt to execute a class, Java looks for the class’s main method to begin
execution.
• Typically, you cannot call a method of another class until you create an object of that class.

Homework
Q> Using programming show the effect of the keyword this.
Q> Using programming show the effect of the method overloading.
Q> Convert an object into an array of object.
Encapsulation
Is a mechanism of wrapping attributes and methods into objects—an object’s attributes and
methods are intimately related. Objects may communicate with one another, but they’re
normally not allowed to know how other objects are implemented. This information hiding is
important to good software engineering.

In encapsulation, variables of class will be hidden from other classes, and can be only accessed
through the methods of their current class. The declaring of instance variables with access
modifier private is known as data hiding or information hiding.

Example,

Why we need access control?

1) Protect private information


2) Clarify how others should use your class
3) Keep implementation separate from interface
Homework

Design the following application:

Inheritance
is a form of software reuse in which a new class is created by absorbing an existing class’s
members and embellishing them with new or modified capabilities. With inheritance, you
can save time.

When creating a class, rather than declaring completely new members, you can designate
that the new class should inherit the members of an existing class. The existing class is called
the superclass, and the new class is the subclass. Each subclass can become a superclass for
future subclasses. For example, A university community has thousands of members, including
employees, students, and alumni. Employees are either faculty or staff members. Faculty
members are either administrators or teachers. students can be graduate or undergraduate
students.
Note

§ Inheritance reduces program development time.


§ The direct superclass of a subclass (specified by the keyword extends in the first line
of a class declaration) is the superclass from which the subclass inherits.
§ An indirect superclass of a subclass is two or more levels up the class hierarchy from
that subclass.
§ Java does not support multiple inheritance.
§ The annotation @Override indicates that a method should override a superclass
method.

Example,

Homework

Using programming show the effect of the keyword final usage for instance variable.
Polymorphism (“Poly” means many, and “Morphs” means forms)
Polymorphism is the ability of an object to take on different forms. In Java, polymorphism
refers to the ability of a class to provide different implementations of a method, depending on
the type of object that is passed to the method.

§ Polymorphism in Java allows us to perform the same action in many ways.


§ With polymorphism, we can design and implement systems that are easily extensible.
§ You can perform Polymorphism in Java via two different methods: Method
Overloading and Method Overriding.

Abstract Classes and Methods


Data abstraction is the process of hiding certain details and showing only essential information
to the user. Abstraction can be achieved with either abstract classes or interfaces

§ Abstract classes cannot be used to instantiate objects because they’re incomplete.


§ The primary purpose of an abstract class is to provide an appropriate superclass from
which other classes can inherit and thus share a common design.

The abstract keyword is a non-access modifier, used for classes and methods:

§ Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).

§ Abstract method: can only be used in an abstract class, and it does not have a body.
The body is provided by the subclass (inherited from).

Example,
Homework
Q> Using programming show the effect of using final with Methods and Classes.
Nested Classes
The Java programming language allows you to define a class within another class. Such a class is
called a nested class and is illustrated here:

class OuterClass {
...
class NestedClass {
...
}
}

Nested classes are divided into two categories: non-static and static. Non-static nested classes are
called inner classes. Nested classes that are declared static are called static nested classes.

Why Use Nested Classes?


• It is a way of logically grouping classes that are only used in one place.
• It increases encapsulation.
• It can lead to more readable and maintainable code.

Note:
• A nested class is a member of its outer class. Non-static nested classes (inner classes) have
access to other members of the outer class, even if they are declared private.
• Static nested classes do not have access to other members of the enclosing class. As a
member of the OuterClass, a nested class can be declared private, public, protected,
or package private.

Example,
Interface
Software objects also communicate via interfaces. A Java interface describes a set of methods that
can be called on an object to tell it, for example, to perform some tasks or return some piece of
information. An interface declaration begins with the keyword interface and contains only
constants and abstract methods.

All methods declared in an interface are implicitly public abstract methods, and all fields are
implicitly public, static and final.

To use an interface, a concrete class must specify that it implements the interface and must
declare each method in the interface with the signature specified in the interface declaration.

Example,
interface Bicycle {

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);


}

class ACMEBicycle implements Bicycle {

int cadence = 0;
int speed = 0;
int gear = 1;

void changeCadence(int newValue) {


cadence = newValue;
}

void changeGear(int newValue) {


gear = newValue;
}

void speedUp(int increment) {


speed = speed + increment;
}

void applyBrakes(int decrement) {


speed = speed - decrement;
}

void printStates() {
[Link]("cadence:" + cadence + " speed:" + speed
+ " gear:" + gear);
}
}

Homework>>
• Why would we use an interface?
• Explain with example: An interface can extend multiple interfaces.
A class can implement multiple interfaces.

You might also like