Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 1 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter 1

Basic Java OOP Concepts

Learning Objectives
After the completion of the chapter, students will be able to:
explain what is object-oriented programming and its basic concepts;
discuss the advantage of OOP in developing a program; and
declare identifiers such as class, package, variable, constant, and method
following the Java naming convention.

OOPs (Object-Oriented Programming System)

Simula is considered as the first object-oriented programming language. Programming


paradigm where everything is represented as an object is known as a truly object-oriented
programming language.

Smalltalk is considered the first truly object-oriented programming language. The


popular object-oriented languages are Java, C#, PHP, Python, and C++.

Object means a real-world entity such as a pen, chair, table, computer, and watch. Object-
oriented programming is a methodology or paradigm to design a program using classes
and objects. It simplifies software development and maintenance.

Any entity that has state and behavior is known as an object. For example, a chair, pen,
table, keyboard, and bike. It can be physical or logical. An object can be defined as an
instance of a class. An object contains an address and takes up some space in memory.
Objects can communicate without knowing the details of each other's data or code. The
only necessary thing is the type of message accepted and the type of response returned
by the objects.

Example A dog is an object because it has state like color, name, and breed, as well as
behaviors like wagging the tail, barking, and eating.
A collection of objects is called a class. It is a logical entity. A class can also be defined as
a blueprint from which you can create an individual object. Class doesn’t consume any
space.

When one object acquires all the properties and behaviors of a parent object, it is known
as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

If one task is performed in different ways, it is known as polymorphism. In Java, we use


method overloading and method overriding to achieve polymorphism. An example can
be to speak something; for example, a cat speaks meow while a dog barks woof.

Hiding internal details and showing functionality is known as abstraction. For example,
we do not know the internal processing that occurs during a phone call. In Java, we use
abstract class and interface to achieve abstraction.

Binding (or wrapping) code and data together into a single unit is known as
encapsulation. A Java class is an example of encapsulation.

Advantage of OOP over Procedure-Oriented Programming


Languages
OOP makes development and maintenance easier, whereas, in a procedure-
oriented programming language, it is not easy to manage if code grows as project size
increases.

OOP provides data hiding, whereas, in a procedure-oriented programming language,


global data can be accessed from anywhere.

OOP provides the ability to simulate real-world event much more effectively.

Object-Oriented Programming Language and Object-Based


Programming Language

Object-based programming language follows all the features of OOPs except inheritance.
JavaScript and VBScript are examples of object-based programming languages.

Java Naming Conventions


Java naming convention is a rule to follow as you decide what to name your identifiers
such as class, package, variable, constant, and method but it is not required that you
follow them. These conventions are suggested by several Java communities such as
Sun Microsystems and Netscape.

All the classes, interfaces, packages, methods and fields of Java programming language
are given according to the Java naming convention. If you fail to follow these conventions,
it may generate confusion or erroneous code.

By using standard Java naming conventions, you make your code easier to read for
yourself and other programmers. Readability of Java program is very important. It
indicates that less time is spent to figure out what the code does. The following are the
key rules that must be followed by every identifier:

The name must not contain any white spaces.


The name should not start with special characters like & (ampersand), $ (dollar), or _
(underscore).
Class and interface should start with uppercase letter.
Class should be noun (e.g. Color, Button, System, Thread)
Interface should be adjective (e.g. Runnable, Remote, ActionListener)
Use appropriate words for class and interface. Avoid acronyms.
Method should start with lowercase letter.
Method should be verb (e.g. print(), println())
If the name of the method contains multiple words, start with a lowercase letter
followed by an uppercase letter (e.g. actionPerformed())
A variable should start with a lowercase letter (e.g. id, name)
A variable should not start with special characters like & (ampersand), $ (dollar), or _
(underscore).
If the name of the variable contains multiple words, start with a lowercase letter
followed by an uppercase letter (e.g. firstName, lastName)
Avoid using one-character variables (e.g. x, y, z).
A package should be a lowercase letter (e.g. java, lang)
A constant should be in uppercase letters (e.g. RED, YELLOW)
If the name of the constant contains multiple words, it should be separated by
underscore (_) (e.g. MAX_PRIORITY)
Java follows camel-case syntax for naming the class, interface, method, and variable.
If the name is combined with two words, the second word will always start with
uppercase letter (e.g. actionPerformed(), firstName, ActionEvent, ActionListener).

You might also like