Topic 03: Introduction to Object Orientation

Post on 18-Jan-2015

766 views 3 download

Tags:

description

Slides

transcript

Topic 3 : Introduction to Object OrientationDDOOCP

Terminologies

› Encapsulation

› Polymorphism

› Inheritance

› Method Overriding

› final Keyword

› Abstract class and methods

› Interfaces

4 major principles of OOP

1. Encapsulation

2. Abstraction

3. Polymorphism

4. Inheritance

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.

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.

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.

Inheritance Example

Inheritance Example

Inheritance Example

Inheritance

Inheritance Example

Calling Constructor of parent class

ERROR

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.

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

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.

Abstract Methods & Classes Example

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.

Interface Example

Example

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.

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