Methods & Classes
Methods & Classes
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
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.
Output (F6)
Java Programming Language
Constructor
Output (F6)
Java Programming Language
Constructor
Output (F6)
Java Programming Language
UML Diagram in Classes
♨ It is used sign (+) before public Attributes or methods and sign (-) before private
Attributes or methods .
♨ In Java, it is possible to inherit attributes and methods from one class to another.
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 (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.
♨ 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)