This document discusses object-oriented programming concepts including encapsulation, inheritance, polymorphism, and dynamic method binding. It explains how encapsulation groups data and operations together and hides implementation details. Inheritance allows classes to inherit attributes and behaviors from base classes. Polymorphism allows subclasses to override methods from base classes, and dynamic method binding determines which implementation to call at runtime based on the object's actual type. The document also covers constructors, references vs values, multiple inheritance, and abstract classes.
Synapse India Reviews About:
Functional/procedural programming:
program is a list of instructions to the computer
Object-oriented programming
program is composed of a collection objects that communicate with each other
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
More Related Content
Similar to 10 - Encapsulation(object oriented programming)- java . ppt
Synapse India Reviews About:
Functional/procedural programming:
program is a list of instructions to the computer
Object-oriented programming
program is composed of a collection objects that communicate with each other
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Embracing GenAI - A Strategic ImperativePeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
Francesca Gottschalk - How can education support child empowerment.pptxEduSkills OECD
Francesca Gottschalk from the OECD’s Centre for Educational Research and Innovation presents at the Ask an Expert Webinar: How can education support child empowerment?
3. 3
Encapsulation
• Encapsulation
– Encapsulation allows the programmer to group data and the
subroutines that operate on them together in one place, and
to hide irrelevant details from the user.
• Information Hiding
– Making objects and algorithms invisible to portions of the
system that do not need them.
4. 4
Modules
• If a module M exports a type T, the rest of the program can only
pass T to subroutines exported from M.
– T is said to be an opaque type.
var Database : module
exports (tuple with (:=, name))
…
type tuple = record
var name : packed array 1..80 of char
…
end tuple
…
• What can the code outside the Database module do?
5. 5
Module Changing
• Body is Changed
• Private Part of Header is Changed
• Public Part of Header is Changed
6. 6
Classes can limit visibility
• Private
• Protected
• Public
• Package (in some languages, e.g. Java)
7. 7
Derived class can restrict visibility
• Private
– Protected and public members of base class are private in derived
class.
• Protected
– Protected and public members of base class are protected in derived
class.
• Public
– Protected and public members of base class are protected and public
in derived class.
• Private members of base class aren’t visible in derived class.
9. 9
Four Important Issues
• Choosing a Constructor
• References and Values
• Execution Order
• Garbage Collection
– We’ve seen that already
10. 10
Choosing a Constructor
• Object-Oriented Languages allow classes to have zero,
one or more different constructors.
• Two ways to distinguish between constructors
– Different Names
– Different Number and Types of Arguements
11. 11
Constructors
• Eiffel code:
• class COMPLEX
creation
new_cartesian, new_polar
…
new_cartesian(x_val, y_va; : REAL) is
…
new_polar(rho, theta : REAL) is
…
• class mydata {
public:
mydata(string data);
mydata(int data);
mydata();
12. 12
References and Values
• C++ vs. Java
– Java uses reference, C++ you can specify
• Reference
– Every object is created explicitly so it is easy to make sure
the correct constructor is called.
– More elegant, but requires allocation from heap and extra
indirections on every access of the object.
• Value
– More efficient but harder to control initialization
13. 13
Execution Order
• If class B is derived from class A, A constructor is
called before B constructor
– To get arguments to the A constructor, you must use an
intializer list
class foo : bar {
...
}
foo::foo (foo_params) : bar(bar_params) {
…
– The part after the colon is a call to bar’s constructor
14. 14
Destructors and Garbage Collection
• When an object is destroyed, the destructor is called
for the derived class first, then the destructors of the
base classes are called.
– Reverse order of derivation
• Destructors purpose is to return allocated space back to
the heap
• Many languages provide automatic garbage collection
– Java, Smalltalk, Eiffel, etc.
15. 15
Java’s finalize() method
• In Java, you can override the finalize() method
• This allows code to be executed when the object is
about to be deleted
– But you shouldn’t extend the object’s lifetime by doing this
– As the finalize() method is only called once per object
17. 17
Polymorphism
• A derived class (D) has all the members of its base
class (C)
– Class D can be used anytime class C is expected.
– If class D does not hide any publicly visible members of C
then D is a subtype of C.
• If class D is used in place of class C, this is a form of
polymorphism.
18. 18
Polymorphism Example
class person { …
class student : public person { …
class professor : public person { …
student s;
professor p;
…
person *x = &s;
person *y = &p;
19. 19
Dynamic vs. Static binding
• Static method binding uses the type of the reference:
s.print_mailing_label();
p.print_mailing_label();
• Dynamic method binding uses the class of the object
that is referred/pointed to:
x->print_mailing_label();
y->print_mailing_label();
20. 20
Which one does Java use?
public class Foo {
public String toString() {
return "Foo's toString()";
}
public static void main (String args[]) {
Object bar = new Foo();
System.out.println (bar);
}
}
• Java uses dynamic binding
21. 21
Dynamic method binding
• Dynamic method binding: calls to virtual methods are
dispatched to the appropriate implementation at run time based
on the class of the object
– Simula: virtual methods listed at beginning of class
declaration
CLASS Person;
VIRTUAL: PROCEDURE PrintMailingLabel;
BEGIN
…
END Person;
22. 22
Dynamic method binding
– C++: keyword “virtual” prefixes function declaration
class person {
public:
virtual void print_mailing_label ();
…
}
• This requires keeping a virtual method table
along with each object
– More on this in a bit…
23. 23
Abstract Methods
• Bodyless virtual methods
In C++: called pure virtual method, created by following a
procedure declaration with an assignment to zero.
class person {
…
public:
virtual void print_mailing_label() = 0;
24. 24
Abstract Classes
• Class that contains one or more abstract methods
– Java: called an interface (which has only abstract methods)
• Generally not possible to declare object of an abstract
class b/c it would be missing at least one member
– But you can do so in C++
• Serves as a base for concrete classes.
– Concrete class must provide a definition for every abstract
method it inherits
• Application to dynamic method binding: allows code
that calls methods of objects of a base class, assuming
that the concrete methods will be invoked at run time.
25. 25
Member Lookup: vtable
• In dynamic binding each object is represented with a
record whose first field contains the address of a
virtual method table (vtable) for that object’s class
• Our objects are being more complicated for the
compiler to manage
– Virtual method tables
– Reference counts
– Etc…
29. 29
Multiple Inheritance
• Derived class with two or more base classes
• E.g. - Student class
• C++:
class student : public person, public gp_list_node
{ … }
30. 30
Multiple Inheritance
• Supported in C++, Eiffel, CLOS
• Single Inheritance only in Simula, Smalltalk, Modula-
3, Ada 95 & Oberon
• Java provides limited support – more on this later
31. 31
Why use MI?
• Involves a number of tradeoffs
– Complexity vs. Simplicity
– Efficiency vs. Scalability
• How do you decide?
– Does it satisfy the “is a” relationship?
– Is object creation speed a constraint?
35. 35
Efficiency (or lack thereof)
• May have to determine view dynamically
• Results in less time-efficient code
• An example implementation may have:
– 3 extra cycles, 1 extra memory access over single inheritance
– 5 extra cycles, 3 extra memory accesses over static methods
36. 36
Semantic Ambiguities
• What if two base classes have implementations
of a shared method?
– Won’t work in Eiffel or C++
– In other languages, you must call methods explicitly,
i.e. class::method()
37. 37
Semantic Ambiguities
• What if the relationship below occurs?
• This is repeated multiple inheritance
– As one of the ancestors is repeated in the parent class of one
of the descendents
gp_list_node person
student
gp_list_node
student_prof
professor
38. 38
Replicated Multiple Inheritance
• Default in C++
• Ex. gp_list_node
• Can only directly access one level deep
– To access a student view of gp_list_node, you must first
assign a student_prof pointer into a student or professor
pointer
42. 42
Mix-in Inheritance
• Only one base class can contain method definitions
– The other base class(es) contain only abstract methods
• Only type of MI supported in Java, but not necessarily
MI
• Traditional Java inheritance uses keyword extends
• Mix-in (interface) inheritance in Java uses keyword
implements
– Done via interfaces
43. 43
Java Interfaces
• public class String extends Object implements
Serializable, CharSequence, Comparable;
• Java interfaces can contain definition prototypes and
static variables