0% found this document useful (0 votes)
2 views

Methods & Classes

The document provides an overview of Java programming concepts, focusing on static methods, object-oriented programming (OOP) principles, and class structures. Key OOP concepts such as encapsulation, abstraction, inheritance, and polymorphism are explained, along with examples of Java programs for various tasks. Additionally, it covers UML diagrams for class design and constructors in Java.

Uploaded by

kahan565p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Methods & Classes

The document provides an overview of Java programming concepts, focusing on static methods, object-oriented programming (OOP) principles, and class structures. Key OOP concepts such as encapsulation, abstraction, inheritance, and polymorphism are explained, along with examples of Java programs for various tasks. Additionally, it covers UML diagrams for class design and constructors in Java.

Uploaded by

kahan565p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Second Stage

Assist. Lec. Hussein Mazin


Java Programming Language
Static Methods in Java
Java Programming Language
Static Methods in Java (void and return type function)

Output (F6)
Java Programming Language
Write a Java program using methods to calculate the area of rectangle?

Output (F6)
Java Programming Language
Write a Java program using methods to calculate the factorial of an input number?

Output (F6)
Java Programming Language
Write a Java program using methods to find the maximum of two entered numbers?

Output (F6)
Java Programming Language
Object-Oriented Programming (OOP)
♨ Object-oriented programming (OOP) is defined as a programming style (and not a
specific language) built on the concept of objects, i.e., a set of data contained in fields that
has unique attributes, and behavior or code, specify procedures (methods).

♨ This type of language treats a program as a group of objects composed of data and
program elements, known as attributes and methods.

♨ Objects can be reused within a program or in other programs. This makes it a popular
language type for complex programs, as code is easier to reuse and scale.
Java Programming Language
The Main Concepts of OOP
♨ Encapsulation : This principle states that all important information is contained within the object, and
only customized information is selected. Other objects do not have access to this class
or the authority to make changes. They are only able to call a list of public functions
or methods.

♨ Abstraction : Objects only reveal internal mechanisms that are relevant for the use of other objects,
hiding any unnecessary implementation code. This concept can help developers more
easily make additional changes or additions over time.

♨ Inheritance : Classes can reuse code from other classes. Relationships and subclasses between objects
can be assigned. This property of OOP reduces development time and ensures a higher
level of accuracy.

♨ Polymorphism : Objects are designed to share behaviors and they can take on more than one form.
The program will determine which meaning or usage is necessary for each execution
of that object from a parent class, reducing the need to duplicate code.
Java Programming Language
The Structure of Object-Oriented Programming (OOP)
♨ Classes are user-defined data types that act as the blueprint for individual objects,
attributes and methods.

♨ Objects are instances of a class created with specifically defined data. When class is
defined initially, the description is the only object that is defined.

♨ Methods are functions that are defined inside a class that describe the behaviors of an
object. the method contained in an object are called instance methods. Programmers use
methods for reusability or keeping functionality encapsulated inside one object at a
time.

♨ Attributes are defined in the class template and represent the state of an object. Class
attributes belong to the class itself.
Java Programming Language
The Diagram of Class Content
Java Programming Language
The Type Methods in Classes

♨ print() : It is used to print variables or messages within a class.

♨ setdata() : It is used to give values to variables in the class.

♨ calc() : It is used to calculate the output from the input data.

♨ getvalue() : It is used to return a value from a specific variable within a class.

♨ others() : Create any function according to the programmer.


Java Programming Language
Java Programming Language
Classes in java

Output (F6)
Write a java program have class called player to print the details of player Ronaldo ?
The details is name , country , age and power

Output (F6)
Write a java program have class called Employee has methods (print , empsalary) the details
The details is id ,name , salary ,bonus , totalpay
empsalary() : totalpay= salary + bonus

Output (F6)
write a program java to Design a class named Triangle . The class contains:
• Tow integer data fields named high and width
• A method named setdata() that returns the area of this rectangle.
• A method named getArea() that returns the area of this rectangle.
• A method named print() that returns a message description for the area of rectangle.

Output (F6)
Java Programming Language
Constructor

♨ A constructor has the same name as its class and is syntactically similar to a method,
but constructors have no explicit return type.

♨ Constructor use to give initial values to the instance variables defined by the class.

♨ There are two types of constructors:

❖ constructor (Non arguments)


❖ constructor (arguments)
Java Programming Language
Constructor

Output (F6)
Java Programming Language
Constructor

Output (F6)
Java Programming Language
Constructor
Output (F6)
Java Programming Language
UML Diagram in Classes

♨ A the main building block of object-oriented modeling.

♨ It is used for general conceptual modeling of the structure of the application.

♨ It is used sign (+) before public Attributes or methods and sign (-) before private
Attributes or methods .

♨ The diagram contains:


❖ Class name : (Student)
❖ Attributes : (id , name )
❖ Methods() : (Student() , setname(name) , print() )
Java Programming Language
UML Diagram in Classes
Java Programming Language
Design a UML diagram to describe class called Employee has:
three instance variables: name(string), age(int) and salary(double).
four instance methods:
1. printEmployee() to Print the Employee details.
2. empSalary(double) to assign the salary to the variable salary.
3. empAge(int) to assign the age of the Employee to the variable age.
4. empName(String) to assign the name of the Employee to the variable name.
Java Programming Language
Design a UML diagram to describe class called Employee has five variables :
Full name (String) , age(int) , private address(String) , years of service(int) ,
private monthly salary (double)
And three methods
printEmployee() to Print the Employee details
getAddresss() to return the value of address
setMonthlySalary() to assign the salary of employee
Java Programming Language
Inheritance

♨ In Java, it is possible to inherit attributes and methods from one class to another.

♨ the formula of inheritance is : Subclass extends Superclass

♨ Subclass (child) - the class that inherits from another class


Superclass (parent) - the class being inherited from
Java Programming Language
Types of inheritance in java

class superclass class superclass{ class superclass{


{ ……} ……}

} class subClass1 extends superClass{ class subClass1 extends superClass{


class subClass extends superClass ……} ……}
{ class subClass2 extends superClass{
……} class subClass2 extends subClass1{
} class subClass3 extends superClass{ ……}
……}
Java Programming Language
Write a Java program to create class Student has(id,name,stage) and inheritance to class
Info to print data?

Output (F6)
Java Programming Language
Overriding
♨If subclass has the same method (same name and same parameter)
as declared in the parent class, it is known as method overriding

output : In class A output : In class A


In class B
Java Programming Language
Write a Java program using methods to find the maximum of two entered numbers?

Output (F6)
Java Programming Language
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.

♨ By providing only setter or getter method, you can make the class read only or write
only.

Read value Write value


Java Programming Language
Encapsulation
Output (F6)
Java Programming Language
Encapsulation
Output (F6)
Java Programming Language
UML Diagram with Inheritance Classes
Java Programming Language
UML Diagram with Inheritance Classes
Write a Java program according to the diagram below
Java Programming Language
Java Programming Language
Polymorphism

♨ Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So, polymorphism means many forms..

♨ In computer science the term polymorphism means “a method the same as another in
name but with different behavior”.
Java Programming Language
Polymorphism
Output (F6)
Java Programming Language
Polymorphism

Output (F6)

You might also like