Вы находитесь на странице: 1из 15

OBJECT ORIENTED CONCEPTS

Introduction - Object Oriented Concepts

Class

A blueprint that defines the attributes and methods

Object
An instance of a Class

Abstraction
Hide certain details and show only essential details

Encapsulation
Binding data and methods together

Inheritance
Inherit the features of the superclass

Polymorphism
One name having many forms
2

OOP

Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones...
Relevant to what?

... relevant to the given project (with an eye to future reuse in similar projects).

Data Abstraction Programming languages define constructs to simplify the way information is presented to the programmer. Functional Abstraction Programming languages have constructs that gift wrap very complex and low level instructions into instructions that are much more readable. Object Abstraction OOP languages take the concept even further and abstract programming constructs as objects.

OOPS Explored - Abstraction

Abstraction
Hide certain details and show only essential details public abstract class Shape { String color; public abstract double getArea(); } public interface Shape { String static final String color = BLACK; public abstract double getArea(); }

Abstract class v/s interface

Inheritance

allows a software developer to derive a new class from an existing one


existing class is called the parent class, or superclass, or base class derived class is called the child class or subclass. the name implies, the child inherits characteristics of the parent is, the child class inherits the methods and data defined for the parent class
7

The

The

As

That

Inheritance in Java
Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object
public class Person { private String name; private Date dob; [...]

Person - name: String - dob: Date

public class Employee extends Person { private int employeID; private int salary; private Date startDate; [...]

Employee - employeeID: int - salary: int - startDate: Date

Employee anEmployee = new Employee();

(a) (b)

(a) Single (b) Multiple (c) Repeated (c)


9 CS540 Software Design

Lecture 13

A reference can be polymorphic, which can be defined as "having many forms"

obj.doIt();

This line of code might execute different methods at different times if the object that obj points to changes

Polymorphic references are resolved at run time; this is called dynamic binding
Careful use of polymorphic references can lead to elegant, robust software designs Polymorphism can be accomplished using inheritance or using interfaces

OOPS Explored Polymorphism - Example

public class Shape { public void display() { System.out.println(In Shape); } } public class Square extends Shape { public void display() { System.out.println(In Square); } }

Shape s1 = new Shape(); s1.display();

Square s2 = new Square (); s2.display();


Shape s3 = new Square (); s3.display(); Square s4 = new Shape (); s4.display();

s4

square
S3 s2 s1
11

shape

One

of the features of object oriented languages Hide the data of an object (variable) Group operations and data together into a new data type Usually easier to use something than understand exactly how it works

microwave, car, computer, software, mp3 player

Encapsulation - Implementing Classes

CS 314

12

OOPS Explored - Encapsulation

I want to share my wedding gift only with my friends

I can share my puppy video with everyone

My YouTube videos My YouTube videos

My Cute My Cute puppy cat

My Wedding My Hawaii Gift trip


13

Advantage of Encapsulation ( Information hiding )


1. It prevents others accessing the insides of an object. The only thing that can manipulate the data in an object is that objects method or member function. 2. It main aim is to prevent accident. It builds a protective wall (encapsulation) around the member data and member function of the class, and hiding implementation details of object. So It keeps data safe from accident.

Вам также может понравиться