module1 elementary concepts of objects and classes
DESCRIPTION
This presentation is to help students to prepare for the ICSE ComputerApplications examination.TRANSCRIPT
1
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
MODULE
ELEMENTARY CONCEPTS OF OBJECTS AND CLASSES
QUICK REVISION GUIDE – THEORY PORTION
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
TOPICS TO REVISE
class object abstraction encapsulation inheritance polymorphism
CLASS
Definition of a class in OOPDefinition of a class in the real
worldExample of a classes and objects in
the real worldFormat of defining a classAttributes required for defining a
classmembers of a class
i) instance variable/member variable/data member / fields ii) class variable / static variable iii) constructor iv)member functions / instance function v) class function /static function
Class as an object factoryClass as a reference data typeinitial classcomplete class name / fully
qualified class nameInstantiating a classClass as a basis of all the
computation in javaClass as an abstraction of a real
world objectClass as a user defined data typeClass as a composite data typenested classAbstract class
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
OBJECT
definition of object in the real world Definition of object in OOP Example of an object in the real world with
characteristics and behaviour Characteristics / state of an object Behaviiour of an object format of object declaration Format of object creation memory allocation of an object referencing members of a class using an
object representing real world objects in object
oriented programming
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
ABSTRACTION
Abstraction is the act of representing essential features of a system without including the background details or explanations
Example of abstraction in the real world
Example of abstraction in OOP
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
ENCAPSULATION
The wrapping up of data and the related functions into a single unit called class is known as encapsulation.
Data hiding /Information hidingpreventing unauthorized accessing of data from outside the class using the principle of encapsulation and private access modifier is called data hiding
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
INHERITANCE
Definition Need for Inheritance base class and derived class Different forms of inheritance
- types of inheritance supported by java
Use of key words extends and super
Function overriding
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Definition
Inheritance is the process by which a class acquires the properties of another class.
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Advantages of inheritance
help us to express inheritance relationship which ensures closeness with real-world models
reusability of existing classes
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Base class and Derived class
base class
derived class
base class is the class from which properties are acquired. It is also called super class or parent class
class A
class B
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Base class and derived class
base class derived
class derived class is the class that
acquire properties from another class. It is also called sub class or child class or extended class
class A
class B
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Different forms of inheritance
single inheritance
A sub class inherits only from a base class
class A
class B
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Different forms of inheritance
multilevel inheritanceclass A
class B
class C
a sub class inherits from a class that itself inherits from another class
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Different forms of inheritance
multiple inheritance
class C
class A class B
a class is derived from two or more base classes
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Different forms of inheritance
hybrid inheritance
class D
class B class C
a class acquires properties from another class through two or more paths
class A
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Different forms of inheritance
hierarchical inheritance
class Dclass B class C
a class has several sub classes
class A
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Use of access modifiers in Inheritance
private members of a class can be used only with in the class
members of a class with default access can be used with in the class as well as in all the classes with in the same package
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Use of access modifiers in Inheritance
protected members of a class can be used with in the class, in a class of the same package as well as in a sub class. The sub class may be in the same package or in a different package
public members of a class can be used everywhere
19
access modifier of member variable / member function
can be accessed with in the class
can be accessed in a sub class of same package
can be accessed in a class of same package
can be accessed in a sub class of a different package
can be accessed in a class of another package
private access modifier
yes no no no no
default access modifier
yes yes yes no no
protected access modifier
yes yes yes yes no
public access modifier
yes yes yes yes yes
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
POLYMORPHISM
Polymorphism comes from Greek meaning “many forms.”
Polymorphism is the process by which the same message can be given to objects of different classes and each object responds to this message in a different manner depending on its class.
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
passing a message to an object is same as calling a function using an object.
for e.g: String s1=s.substring(5,8); In the above statement we invoke
the substring function of class string using the object s1.
We can also say that we pass a message substring to the object s1 with the values 5 and 8
POLYMORPHISM
Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students
Function overloading can be considered as an example of polymorphism
Binding - dynamic binding or late binding - static binding or early binding
POLYMORPHISM