module1 elementary concepts of objects and classes

22
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

Upload: mersonsir

Post on 19-Jan-2015

229 views

Category:

Education


4 download

DESCRIPTION

This presentation is to help students to prepare for the ICSE ComputerApplications examination.

TRANSCRIPT

Page 1: Module1 elementary concepts of objects and classes

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

Page 2: Module1 elementary concepts of objects and classes

Prepared by Merson Mathew, St. Joseph’s School Bhagalpur for ICSE Computer Applications Students

TOPICS TO REVISE

class object abstraction encapsulation inheritance polymorphism

Page 3: Module1 elementary concepts of objects and classes

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

Page 4: Module1 elementary concepts of objects and classes

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

Page 5: Module1 elementary concepts of objects and classes

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

Page 6: Module1 elementary concepts of objects and classes

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

Page 7: Module1 elementary concepts of objects and classes

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

Page 8: Module1 elementary concepts of objects and classes

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.

Page 9: Module1 elementary concepts of objects and classes

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

Page 10: Module1 elementary concepts of objects and 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

Page 11: Module1 elementary concepts of objects and classes

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

Page 12: Module1 elementary concepts of objects and classes

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

Page 13: Module1 elementary concepts of objects and classes

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

Page 14: Module1 elementary concepts of objects and classes

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

Page 15: Module1 elementary concepts of objects and 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

Page 16: Module1 elementary concepts of objects and classes

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

Page 17: Module1 elementary concepts of objects and classes

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

Page 18: Module1 elementary concepts of objects and classes

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

Page 19: Module1 elementary concepts of objects and classes

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

Page 20: Module1 elementary concepts of objects and classes

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.

Page 21: Module1 elementary concepts of objects and classes

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

Page 22: Module1 elementary concepts of objects and classes

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