22
Topic 3 : Introduction to Object Orientation DDOOCP

Topic 03: Introduction to Object Orientation

Embed Size (px)

DESCRIPTION

Slides

Citation preview

Page 1: Topic 03: Introduction to Object Orientation

Topic 3 : Introduction to Object OrientationDDOOCP

Page 2: Topic 03: Introduction to Object Orientation

Terminologies

› Encapsulation

› Polymorphism

› Inheritance

› Method Overriding

› final Keyword

› Abstract class and methods

› Interfaces

Page 3: Topic 03: Introduction to Object Orientation

4 major principles of OOP

1. Encapsulation

2. Abstraction

3. Polymorphism

4. Inheritance

Page 4: Topic 03: Introduction to Object Orientation

Encapsulation

› Encapsulation is the hiding of data implementation by restricting access to accessors and mutators (getters and setters).

› Concept of encapsulation is creating the class with private attributes and public methods to interact those attributes.

Page 5: Topic 03: Introduction to Object Orientation

Polymorphism

› Derived from two Greek words : poly which means many and morphs, which means form.

› So, the meaning of the word polymorphism is many forms.

› Two types1. Static polymorphism or early binding or compile-time polymorphism

2. Dynamic polymorphism or late binding or runtime polymorphism

› Static polymorphism is obtained through method overloading.

› Write examples of method overloading and constructor overloading.

Page 6: Topic 03: Introduction to Object Orientation

Inheritance

› The feature by which one class acquires the characteristics of an existing class is known as Inheritance.

› A class that is inherited is called super class or parent class.

› A class that inherits the superclass is called a sub class or child class.

› The extends keyword is used in sub class to inherit a super class.

› A class can extend from only one parent.

Page 7: Topic 03: Introduction to Object Orientation

Inheritance Example

Page 8: Topic 03: Introduction to Object Orientation

Inheritance Example

Page 9: Topic 03: Introduction to Object Orientation

Inheritance Example

Page 10: Topic 03: Introduction to Object Orientation

Inheritance

Page 11: Topic 03: Introduction to Object Orientation

Inheritance Example

Page 12: Topic 03: Introduction to Object Orientation

Calling Constructor of parent class

ERROR

Page 13: Topic 03: Introduction to Object Orientation

Method Overriding

› Do you remember the previous example of ParentClass and ChildClass?

› Did you notice a display() method in ParentClass and also in ChildClass?

› The display() method in ChildClass overwrites definition of display() method of ParentClass.

› This is known as Method Overriding.

Page 14: Topic 03: Introduction to Object Orientation

The final keyword

› Purposes of final keyword in Java

1. Declaring a constant variable

2. Preventing a method from being overridden

3. Preventing a class from being inherited.Demo

Page 15: Topic 03: Introduction to Object Orientation

Abstract Methods & Classes

› A method without a definition is known as abstract method.

› A method is declared abstract when it needs to be overridden in its subclasses.

› An abstract class is a class that has at least one abstract method.

› Java allows us to define abstract methods and classes by using the abstract keyword.

› An abstract class cannot be instantiated i.e. you cannot create objects of an abstract class.

Page 16: Topic 03: Introduction to Object Orientation

Abstract Methods & Classes Example

Page 17: Topic 03: Introduction to Object Orientation

Interface

› An interface is a collection of various methods without definition.

› An interface is declared using interface keyword instead of class.

› An interface can be implemented by one or more class using the implements keyword.

› An advantage of using interfaces is that we can implement multiple interfaces.

› An interface cannot be instantiated i.e. you cannot create objects of an interface.

› All the methods declared in the interface must be overridden.

Page 18: Topic 03: Introduction to Object Orientation

Interface Example

Page 19: Topic 03: Introduction to Object Orientation

Example

Page 20: Topic 03: Introduction to Object Orientation
Page 21: Topic 03: Introduction to Object Orientation

Questions

› Describe the structure of the syntax of a Java statement that instances an object.

› Explain the terms encapsulation and polymorphism with reference to natural and manufactured real universe objects.

› Describe, giving examples in Java, the purpose of the extends keyword.

› Explain why the facility of inheritance, in the Java programming language, is regarded as being so very important.

› Describe why the Object-Oriented paradigm is considered superior to the Structured Programming paradigm.

Page 22: Topic 03: Introduction to Object Orientation

References

› http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/

› http://stackoverflow.com/questions/8960918/how-encapsulation-is-different-from-abstraction-as-a-object-oriented-concept-in