Sie sind auf Seite 1von 15

Microlink Information Technology College

ማይክሮሊንክ የኢንፎርሜሽን ቴክኖሎጂ ኮሌጅ


Department of Computer Science

Lecture Handout
Course Name: Object Oriented Programming (OOP)
Course Code:
Chapter: Introduction To Object Oriented Programming (OOP)
Section:
Instructor: Surafel S
Mail address: jjet2116@gmail.com
Reference: An Introduction to Object-Oriented Programming with Java, Thomas Wu
This course is not a Java course, rather it is a course in Object Oriented Programming (OOP).
Java will be just a “medium of instruction”. What you learn here will be applicable to other
object-oriented languages (C++, C#, VB.NET, Python…
Course Delivery:
We will use google classroom to provide you the lecture course, using google classroom also
Assignments and quiz assessment also can be delivered. Lecture demo also will be shared
through your communication channels for more elaboration. Once the decision was made to
start a face to face, we will have a tutorial session before your final exam assessments. Small
programming assignments, individually or in group. will be given to enhance your
understanding of the lecture
Assignments and project
• The lectures will be much like: አበበ በሶ በላ, You will be told about and shown some object
oriented programming concepts.
• The assignments and lab sessions will be like: አበበ ምን በላ?,You will be required to
Course Note
practice what is discussed and shown in class
• The semester project will be like: አበበ የሚወደውን አይነት በሶ አበስብሱ, you will develop a
full-fledged software. You will learn lots of items that are not covered in class while
doing the project. You may need to refer additional materials. You can seek my
assistance at my office hours (yet to be posted)
Academic honesty
• All answers to assignments and exams should be your own work. Unless it is a group
assignment, you shall not share assignment solutions with your classmates. However,
you are encouraged to discuss general ideas and approaches.
• Your assignments will be run-through an advanced plagiarism detection software. All
students who have submitted identical code will get zero points (it does not matter who
copied from whom)
Developments in software technology continue to be dynamic. New tools and techniques are announced in quick
succession. This has forced the software engineers and industry to continuously look for new approaches to
software design and development, and they are becoming more and more critical in view of the increasing
complexity of software systems as well as the highly competitive nature of the industry. These rapid
advances appear to have created a situation of crisis within the industry. The following issued need to be
addressed to face the crisis:

• How to represent real-life entities of problems in system design?


• How to design system with open interfaces?
• How to ensure reusability and extensibility of modules?
• How to develop modules that are tolerant of any changes in future?
• How to improve software productivity and decrease software cost?
• How to improve the quality of software? • How to manage time schedules?

1.1 Principles of OOP


Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. In
other words, it is a programming language model organized around objects rather than "actions" and data rather
than logic.

Fig 1 Object Oriented Programming System

OOP simplifies the software development and maintenance by providing some concepts so that, it is necessary
to understand some of the concepts used extensively in object-oriented programming. These include: Objects,
Classes, Data abstraction and encapsulation, Inheritance, Polymorphism, Dynamic binding, Message passing,
Coupling, Cohesion, Association, Aggregation, Composition

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 2
Object
Object means a real word entity such as pen, chair, table etc. object, class, inheritance, polymorphism, abstraction,
and encapsulation.

Fig 2 Example of an object in the real-world entity

They may also represent user-defined data such as vectors, time and lists. Programming problem is
analyzed in term of objects and the nature of communication between them. Program objects should be
chosen such that they match closely with the real-world objects. Objects take up space in the memory and
have an associated address like a record in Pascal, or a structure in c.

When a program is executed, the objects interact by sending messages to one another. for example, if “customer”
and “account” are two objects in a program, then the customer object may send a message to the count object
requesting for the bank balance. Each object contain data, and code to manipulate data. Objects can interact
without having to know details of each other’s data or code. It is a sufficient to know the type of message accepted,
and the type of response returned by the objects. Although different author represent them differently fig shows
two notations that are popularly used in object-oriented analysis and design.

Fig 3: Representing an object

Class
We just mentioned that objects contain data, and code to manipulate that data. The entire set of data and code
of an object can be made a user-defined data type with the help of class. In fact, objects are variables
of the type class. Once a class has been defined, we can create any number of objects belonging to that
class. Each object is associated with the data of type class with which they are created. A class is thus a collection

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 3
of objects similar types. For examples, Mango, Apple and orange members of class fruit. Classes are
user-defined data types and behave like the built-in types of a programming language. The syntax used to create
an object is not different then the syntax used to create an integer object in C, C+, Java etc. If fruit has
been defining as a class, then the statement.

Fruit Mango.

Will create an object mango belonging to the class fruit.

We can also take a look the figure bellow to understand the concept of Class and Objects diagrammatically

Fig 4: Class Representation

Inheritance
Inheritance is the process by which objects of one class acquired the properties of objects of another classes. It
provides code reusability. It enables to achieve runtime polymorphism. In other words, inheritance is the concept
that when a class of objects is defined, any subclass that is defined can inherit the definitions of one or more
general classes.

Fig 5: Property inheritances

In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional
features to an existing class without modifying it. This is possible by deriving a new class from the existing one.

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 4
The new class will have the combined feature of both the classes. The real appeal and power of the inheritance
mechanism is that it allows the programmer to reuse a class i.e almost, but not exactly, what he wants, and to
tailor the class in such a way that it does not introduced any undesirable side-effects into the rest of classes.

Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take
more than one form. Polymorphism means one name, many forms. It manifests itself by having multiple methods
all with the same name, but slightly different functionality. There are two basic types of polymorphism.
Overriding and overloading.

Fig 6: Polymorphism Scenario

An operation may exhibit different behavior is different instances. The behavior depends upon the types
of data used in the operation. For example, consider the operation of addition. For two numbers, the
operation will generate a sum. If the operands are strings, then the operation would produce a third
string by concatenation. The process of making an operator to exhibit different behaviors in different instances is
known as operator overloading.

Fig. 6 illustrates that a single function name can be used to handle different number and different types of
argument. This is something similar to a particular word having several different meanings depending upon
the context. Using a single function name to perform different type of task is known as function overloading. In
summary an Overriding, also called run-time polymorphism. Overloading is a compile-time polymorphism.

Fig 7: Polymorphism

Polymorphism plays an important role in allowing objects having different internal structures to share
the same external interface. This means that a general class of operations may be accessed in the same

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 5
manner even though specific action associated with each operation may differ. Polymorphism is extensively
used in implementing inheritance.

Abstraction and Encapsulation


Data abstraction is a mechanism of hiding internal details and showing only functionality. Data abstraction is the
development of classes, objects, types in terms of their interfaces and functionality, instead of their
implementation details. Abstraction denotes a model, a view, or some other focused representation for an actual
item. In java, one uses abstract class and interface to achieve abstraction.

“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object
and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch

The wrapping up of data and function into a single unit (called class) is known as encapsulation. Data
and encapsulation are the most striking feature of a class. The data is not accessible to the outside world, and
only those functions which are wrapped in the class can access it. These functions provide the interface
between the object’s data and the program. This insulation of the data from direct access by the program is
called data hiding or information hiding.

Fig 8: Data abstraction and Encapsulation

Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic
binding means that the code associated with a given procedure call is not known until the time of the call at run
time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic
reference depends on the dynamic type of that reference.

Consider the procedure “draw” in fig. 6. by inheritance, every object will have this procedure. Its algorithm
is, however, unique to each object and so the draw procedure will be redefined in each class that defines
the object. At run-time, the code matching the object under current reference will be called.

Message Passing
An object-oriented program consists of a set of objects that communicate with each other. The process of
programming in an object-oriented language, involves the following basic steps:

1. Creating classes that define object and their behavior,


2. Creating objects from class definitions, and

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 6
3. Establishing communication among objects.
Objects communicate with one another by sending and receiving information much the same way as people
pass messages to one another. The concept of message passing makes it easier to talk about building
systems that directly model or simulate their real-world counterparts.

A Message for an object is a request for execution of a procedure, and therefore will invoke a function
(procedure) in the receiving object that generates the desired results. Message passing involves specifying
the name of object, the name of the function (message) and the information to be sent. Example:

Object has a life cycle. They can be created and destroyed. Communication with an object is feasible as
long as it is alive.

Coupling
Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware
of each other. If a class has the details information of another class, there is strong coupling. In Java, we use
private, protected, and public modifiers to display the visibility level of a class, method, and field. You can use
interfaces for the weaker coupling because there is no concrete implementation.

Cohesion
Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task
is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts. The
java.io package is a highly cohesive package because it has I/O related classes and interface. However, the
java.util package is a weakly cohesive package because it has unrelated classes and interfaces.

Association
Association represents the relationship between the objects. Here, one object can be associated with one object or
many objects. There can be four types of association between the objects:

• One to One
• One to Many
• Many to One, and
• Many to Many

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 7
Let's understand the relationship with real-time examples. For example, One country can have one prime minister
(one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime
minister (many to one), and many ministers can have many departments (many to many).

Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains
other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a
relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.

Composition
The composition is also a way to achieve Association. The composition represents the relationship where one
object contains other objects as a part of its state. There is a strong relationship between the containing object and
the dependent object. It is the state where containing objects do not have an independent existence. If you delete
the parent object, all the child objects will be deleted automatically.

1.2 Procedure (Structured) vs Object Oriented Programming


1.2.1 Procedure-Oriented Programming
In the procedure-oriented approach, the problem is viewed as the sequence of things to be done such as reading,
calculating and printing such as cobol, fortran and c. The primary focus is on functions. A typical structure
for procedural programming is shown in fig.1. The technique of hierarchical decomposition has been used to
specify the tasks to be completed for solving a problem.

Fig 9: Typical structure of procedural oriented programs

Procedure oriented programming basically consists of writing a list of instructions for the computer to follow and
organizing these instructions into groups known as functions. We normally use flowcharts to organize these
actions and represent the flow of control from one action to another.

In a multi-function program, many important data items are placed as global so that they may be accessed
by all the functions. Each function may have its own local data. Global data are more vulnerable to an
inadvertent change by a function. In a large program it is very difficult to identify what data is used by

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 8
which function. In case we need to revise an external data structure, we also need to revise all functions that
access the data. This provides an opportunity for bugs to creep in.

Another serious drawback with the procedural approach is that we do not model real world problems very
well. This is because functions are action-oriented and do not really corresponding to the element of the problem.
Some Characteristics exhibited by procedure-oriented programming are:

• Emphasis is on doing things (algorithms).


• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
1.2.2 Object Oriented Paradigm
The major motivating factor in the invention of object-oriented approach is to remove some of the flaws
encountered in the procedural approach. OOP treats data as a critical element in the program development
and does not allow it to flow freely around the system. It ties data more closely to the function that
operate on it and protects it from accidental modification from outside function. OOP allows decomposition
of a problem into a number of entities called objects and then builds data and function around these
objects. The organization of data and function in object-oriented programs is shown in fig.1.3. The data
of an object can be accessed only by the function associated with that object. However, function of one
object can access the function of other objects.

Fig 10: Organization of data and function in OOP

Some of the features of object-oriented programming are:

• Emphasis is on data rather than procedure.


• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in the data structure.
• Data is hidden and cannot be accessed by external function.

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 9
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
Object-oriented programming is the most recent concept among programming paradigms and still
means different things to different people.

1.2.3 Advantage of OOPs over Procedure-oriented programming language


1. OOPs 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.
2. OOPs provides data hiding, whereas, in a procedure-oriented programming language, global data
can be accessed from anywhere.

Data Representation in Procedure-Oriented Programming Data Representation in Object-Oriented Programming

3. OOPs provides the ability to simulate real-world event much more effectively. We can provide
the solution of real word problem if we are using the Object-Oriented Programming language.

1.3 Object Oriented Language


Object-oriented programming is not the right of any particular languages. Like structured programming, OOP
concepts can be implemented using languages such as C and Pascal. However, programming becomes clumsy
and may generate confusion when the programs grow large. A language that is specially id designed to support
the OOP concepts makes it easier to implement them. The languages should support several of the OOP
concepts to claim that they are object-oriented. Depending upon the features they support, they can be classified
into the following two categories:

1. Object-based programming is the style of programming that primarily supports


encapsulation and object identity. Major feature that are required for object-based
programming are:
a. Data encapsulation
b. Data hiding and access mechanisms
c. Automatic initialization and clear-up of objects
d. Operator overloading
Languages that support programming with objects are said to the objects-based programming
languages. They do not support inheritance and dynamic binding. Ada, JavaScript and VBScript
is a typical object-based programming language.
2. Object-oriented programming language incorporates all object-based programming features
along with two additional features, namely, inheritance and dynamic binding. Object-
oriented programming can therefore be characterized by the following statements: Object-
based features + inheritance + dynamic binding.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 10
1.4 Application of OOP
OOP has become one of the programming buzzwords today. There appears to be a great deal of excitement and
interest among software engineers in using OOP. Applications of OOP are beginning to gain importance in
many areas. The most popular application of object-oriented programming, up to now, has been in the
area of user interface design such as window. Hundreds of windowing systems have been developed,
using the OOP techniques.

Real-business system are often much more complex and contain many more objects with complicated
attributes and method. OOP is useful in these types of application because it can simplify a complex
problem. The promising areas of application of OOP include:

• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
The object-oriented paradigm sprang from the language, has matured into design, and has recently moved into
analysis. It is believed that the richness of OOP environment will enable the software industry to improve
not only the quality of software system but also its productivity. Object-oriented technology is certainly going
to change the way the software engineers think, analyze, design, and implement future system.

1.5 The Java Technology, Evolution and Programming Structure

1.5.1 Evolution of Java


The development of each programming language is based on a fact: there is a need to solve a problem that was
not resolved by previous programming languages. Early programmers had to choose different programming
languages, usually for various tasks, such as a specific language for a type of field. A certain language was
sufficient to solve the problems of its field but was not able to solve the problems of other fields. For example,
Fortran could have been used to write efficient programs for scientific problems, but it was not good for system
code. Similarly, Basic was easy to understand but was not robust to write big programs; While the assembly
language was powerful for writing efficient programs, but it was not easy to remember and execution.

Programming languages such as Cobol, Fortran do not have structural principles. They use the Goto statement to
control the flow of the program. Therefore, programs using this type of code are made up of many jumps and
conditional statements that make it difficult to understand.

Therefore, C was invented in 1970, to replace the assembly language and to create a structured, effective and
high-level language. The development of C was the result of the development process started with BCPL by
Dennis Ritchie. BCPL is an old language developed by Martin Richard. Ken Thompson developed a language
called B, which was influenced by BCPL.

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 11
C is a processor-oriented programming language; it is easy to execute and understand. C became quite famous at
that time because it was reliable, simple, and easy to use.

Though C was a quite efficient and successful programming language, the complexity of the program was seeking
more efficient language to solve problems. When we write a program in C, it has a limit, such as a maximum of
25000 lines of code, beyond which it cannot handle the complexity. But writing and managing large programs
was a demand at that time. So, a new concept came.

C++ came with object-oriented programming features. C++ is the extension of C language which has been used
extensively. It is a powerful modern language that includes the power and simplicity of C and the characteristics
of OOP. C++ provides more functional software benefits than C.

C ++ with OOP became quite famous but then a new problem arose, to control the software on different machines,
a separate compiler is required for that CPU. But building a C++ compiler was quite expensive. Therefore, an
efficient and easy solution was needed, and this requirement became the reason for the creation of Java, which is
a portable and platform-independent language.

1.5.2 History of Java Technology


The history of Java is very interesting. Java was originally designed for interactive television, but it was too
advanced technology for the digital cable television industry at the time. The history of Java starts with the Green
Team(James Gosling, Patrick Naughton, Chris Warth, Mike Sheridan, and Ed Frank). Java team members (also
known as Green Team), initiated this project to develop a language for digital devices such as set-top boxes,
televisions, etc. However, it was suited for internet programming. Later, Java technology was incorporated by
Netscape.

The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured,
High Performance, Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and Dynamic". Java was
developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team members
started the project in the early '90s. Currently, Java is used in internet programming, mobile devices, games, e-
business solutions, etc. There are given significant points that describe the history of Java.

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of sun engineers called Green Team.
Initially designed for small, embedded systems in electronic appliances like set-top boxes.
Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
After that, it was called Oak and was developed as a part of the Green project.
Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the
U.S.A., France, Germany, Romania, etc.
In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
Why had they chosen java name for Java language? The team gathered to choose a new name.
The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA", etc. They wanted
something that reflected the essence of the technology: revolutionary, dynamic, lively, cool,
unique, and easy to spell and fun to say.
According to James Gosling, "Java was one of the top choices along with Silk". Since Java was
so unique, most of the team members preferred Java than other names.

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 12
Java is an island of Indonesia where the first coffee was produced (called java coffee). It is a
kind of espresso bean. Java name was chosen by James Gosling while having coffee near his
office.
Notice that Java is just a name, not an acronym.
Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Corporation) and released in 1995.
In 1995, Time magazine called Java one of the Ten Best Products of 1995.
JDK (Java Development Kit) 1.0 released in(January 23, 1996). After the first release of Java,
there have been many additional features added to the language. Now Java is being used in
Windows applications, Web applications, enterprise applications, mobile applications, cards,
etc. Each new version adds the new features in Java.

1.5.3 Java Version History


Java SE Version Version Number Release Date
JDK 1.0 (Oak) 1.0 January 1996
JDK 1.1 1.1 February 1997
J2SE 1.2 (Playground) 1.2 December 1998
J2SE 1.3 (Kestrel) 1.3 May 2000
J2SE 1.4 (Merlin) 1.4 February 2002
J2SE 5.0 (Tiger) 1.5 September 2004
Java SE 6 (Mustang) 1.6 December 2006
Java SE 7 (Dolphin) 1.7 July 2011
Java SE 8 1.8 March 2014
Java SE 9 9 September, 21st 2017
Java SE 10 10 March, 20th 2018
Java SE 11 11 September, 25th 2018
Java SE 12 12 March, 19th 2019
Java SE 13 13 September, 17th 2019
Java SE 14 14 March, 17th 2020
Table 1: Java Detailed Version History

From the table above we can see that the naming and the version number have been changing over times:

Versions 1.0 and 1.1 are named as JDK (Java Development Kit).
From versions 1.2 to 1.4, the platform is named as J2SE (Java 2 Standard Edition).
From versions 1.5, Sun introduces internal and external versions. Internal version is continuous from
previous ones (1.5 after 1.4), but the external version has a big jump (5.0 for 1.5). This could make
confusion for someone, so keep in mind that version 1.5 and version 5.0 are just two different version
names for only one thing.
From Java 6, the version name is Java SE X.
Major versions were released after every 2 years, however the Java SE 7 took 5 years to be available after its
predecessor Java SE 6, and 3 years for Java SE 8 to be available to public afterward.

1.5.4 Java Programming Structure


It is necessary to know the exact structure of the Java program, and this lesson contains a detailed description of
it. This lesson is essential for you before proceeding to learn more advanced lessons of Java programming. Here,

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 13
in this chapter, you will study the structure of the Java program. Such as how to create a simple Java program and
what its different sections mean.

1.5.4.1 Basic Java Program Structure


A Java program involves the following sections:

Section Description
Documentation You can write a comment in this section. Comments are beneficial for the programmer
Section because they help them understand the code. These are optional, but we suggest you use
them because they are useful to understand the operation of the program, so you must
write comments within the program.
Package You can create a package with any name. A package is a group of classes that are defined
statement by a name. That is, if you want to declare many classes within one element, then you can
declare it within a package. It is an optional part of the program, i.e., if you do not want
to declare any package, then there will be no problem with it, and you will not get any
errors. Here, the package is a keyword that tells the compiler that package has been
created.

It is declared as:

package package_name;
Import This line indicates that if you want to use a class of another package, then you can do this
statements by importing it directly into your program.
Example:
import calc.add;
Interfaces are like a class that includes a group of method declarations. It's an optional
Interface
section and can be used when programmers want to implement multiple inheritances
statement
within a program.
A Java program may contain several class definitions. Classes are the main and essential
Class Definition
elements of any Java program.
Every Java stand-alone program requires the main method as the starting point of the
Main Method program. This is an essential part of a Java program. There may be many classes in a Java
Class program, and only one class defines the main method. Methods contain data type
declaration and executable statements.
Table 2: Java Programming Structure

Here is an example of the Hello Java program to understand the class structure and features. There are a few lines
in the program, and the primary task of the program is to print Hello Java text on the screen.

1.5.4.2 A Simple Java Program to Print “Hello Java”


A simple Java Program to Print “Hello Java” Script

//Name of this file will be "Hello.java"

public class Hello


{
/* Author: www.w3schools.in
Date: 2018-04-28
Description:

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 14
Writes the words "Hello Java" on the screen */
public static void main(String[] args)
{
System.out.println("Hello Java.");
}
}
Program Output

Hello Java,

public class Hello • This creates a class called Hello.


• All class names must start with a capital letter.
• The public word means that it is accessible from any other classes.

/* Comments */ The compiler ignores comment block. Comment can be used anywhere in the
program to add info about the program or code block, which will be helpful for
developers to understand the existing code in the future easily.
Braces Two curly brackets {...} are used to group all the commands, so it is known that the
commands belong to that class or method.
public static void • When the main method is declared public, it means that it can also be used
main by code outside of its class, due to which the main method is declared public.
• The word static used when we want to access a method without creating its
object, as we call the main method, before creating any class objects.
• The word void indicates that a method does not return a value. main() is
declared as void because it does not return a value.
• main is a method; this is a starting point of a Java program.

You will notice that the main method code has been moved to some spaces left. It is
called indentation which used to make a program easier to read and understand.
String[] args It is an array where each element of it is a string, which has been named as "args". If
your Java program is run through the console, you can pass the input parameter, and
main() method takes it as input.
System.out.println(); This statement is used to print text on the screen as output, where the system is a
predefined class, and out is an object of the PrintWriter class defined in the system.
The method println prints the text on the screen with a new line. You can also use
print() method instead of println() method. All Java statement ends with a semicolon.
Table 3: Description of the Program structure

Here are the most important points to note about the Java programs:

You have to keep in mind that, Java code is case sensitive.


To write a Java program, you must have to define class first.
The name of the class in Java (which holds the main method) is the name of the Java program, and the
same name will be given in the filename. As mentioned above in the sample program; The name of the
class is "Hello" in which the main method is, then this file will be named "Hello.Java".

OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 15

Das könnte Ihnen auch gefallen