CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Similar documents
Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

OVERRIDING. 7/11/2015 Budditha Hettige 82

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 5 Object-Oriented Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 5. Inheritance

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS-202 Introduction to Object Oriented Programming

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Concepts


Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

C++ Important Questions with Answers

Object Orientated Analysis and Design. Benjamin Kenwright

Chapter 5: Classes and Objects in Depth. Information Hiding

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Practice for Chapter 11

Microsoft Visual Basic 2005: Reloaded

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

QUESTIONS FOR AVERAGE BLOOMERS

1.00 Lecture 13. Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Principles of Object Oriented Programming. Lecture 4

Full file at Chapter 2 - Inheritance and Exception Handling

CS 251 Intermediate Programming Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming. Solved MCQs - Part 2

Upcasting. Taking an object reference and treating it as a reference to its base type is called upcasting.

Programming II (CS300)

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Class, Variable, Constructor, Object, Method Questions

What are the characteristics of Object Oriented programming language?

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

PROGRAMMING LANGUAGE 2

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CO Java SE 8: Fundamentals

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Object-Oriented ProgrammingInheritance & Polymorphism

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

EKT472: Object Oriented Programming. Overloading and Overriding Method

Inheritance and Polymorphism

What is Inheritance?

VIRTUAL FUNCTIONS Chapter 10

Inheritance and object compatibility

Classes, Objects, and OOP in Java. June 16, 2017

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Exercise: Singleton 1

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Data Abstraction. Hwansoo Han

Chapter 9 Inheritance

Java Magistère BFA

ENCAPSULATION AND POLYMORPHISM

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Java Fundamentals (II)

Classes, objects, methods and properties

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java is an Object Oriented Programming language i.e. it supports four major pillars of OOPS.

Polymorphism CSCI 201 Principles of Software Development

Inheritance and Polymorphism

Introduction to Java. March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman

JAVA: A Primer. By: Amrita Rajagopal

Fundamentals of Object Oriented Programming

15CS45 : OBJECT ORIENTED CONCEPTS

Programming overview

Tutorial 8 Date: 15/04/2014

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CS260 Intro to Java & Android 03.Java Language Basics

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Introduction to Programming Using Java (98-388)

Data Structures and Other Objects Using C++

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Overview of Java s Support for Polymorphism

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

OBJECT ORIENTED PROGRAMMING

BASIC CONCEPT OF OOP

Arrays Classes & Methods, Inheritance

Transcription:

CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University

OOP Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance Polymorphism

Encapsulation To hide the implementation details from users In Java, a mechanism of wrapping the data and code acting one the data together. The variables of a class will be hidden from other class with access modifiers. Then, the variables can be accessed only through the methods of their current class. Data hiding http://beginnersbook.com/2013/05/encapsulation-in-java/

Encapsulation Conventional Approach Declare the variables of a class as private Provide public setter/getter methods to modify and view the variables values http://www.tutorialspoint.com/java/java_encapsulation.htm

Encapsulation Benefits of Encapsulation: The fields of a class can be made read-only or writeonly A class can have total control over what is stored in its fields. The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code. http://www.tutorialspoint.com/java/java_encapsulation.htm

Polymorphism Literally means a state of having many shapes The capacity to take on different forms In Java, process objects of various types and classes through a single/uniform interface. http://www.sitepoint.com/quick-guide-to-polymorphism-in-java/

Polymorphism Example: In Pac-man game Two objects: Pac-man and Ghost Both pac-mans and ghosts can be derived from a PacManObject class that defines basic behaviors of the objects: Move() However, Pac-man and Ghost may have different details in the Move() method. Pac-man eats pac-dots, while ghosts do not

Method Overloading One solution for polymorphism Overloaded Methods public class Pacman { public void move() { System.out.println("Pacman::move()"); public void move(boolean eatpacdots){ if (eatpacdots) eatpacdots();

Method Overloading One solution for polymorphism Overloaded Methods public class PacmanGame { public static void main(string[] args) { Pacman pacman = new Pacman(); pacman.move(); pacman.move(true);

Method Overriding A variable is declared as superclass but is created with child class. Java decides what method to call during runtime public class PacmanObject { public void move() { System.out.println( PacmanObject:Move() ); public class Ghost extends PacmanObject {

Method Overriding A variable is declared as superclass but is created with child class. Java decides what method to call during runtime public class Pacman extends PacmanObject { public void move() { super.move(); System.out.println("Pacman::move()"); public void move(boolean eatpacdots){ super.move(); if (eatpacdots) eatpacdots();

Method Overriding A variable is declared as superclass but is created with child class. Java decides what method to call during runtime public class PacmanGame { public static void main(string[] args){ // obj is declared as an PacmanObject PacmanObject obj1 = new Pacman(); PacmanObject obj2 = new Ghost(); obj1.move(); obj2.move();

Method Overriding A variable is declared as superclass but is created with child class. Java decides what method to call during runtime public class PacmanGame { public static void main(string[] args){ PacmanObject[2] objs; objs[0] = new Pacman(); objs[1] = new Ghost(); for (int i = 0; i < objs.length; i++) objs[i].move();

Method Binding Process to decide what method to call Two types Static Polymorphism (early binding) Compile time polymorphism Private, final, and static methods Method overloading Dynamic polymorphism (late binding) Runtime binding Method overriding

Method Binding class Top { public String f(object o) {return "Top"; class Sub extends Top { public String f(string s) {return "Sub"; public String f(object o) {return "SubObj"; http://stackoverflow.com/questions/5658553/question-about-java-overloading-dynamic-binding

Method Binding public class Test { public static void main(string[] args) { Sub sub = new Sub(); Top top = sub; String str = "Something"; Object obj = str; System.out.println(top.f(obj)); System.out.println(top.f(str)); System.out.println(sub.f(obj)); System.out.println(sub.f(str)); http://stackoverflow.com/questions/5658553/question-about-java-overloading-dynamic-binding

Method Binding Sub sub = new Sub(); // Ref: Sub, Act: Sub Top top = sub; // Ref: Top, Act: Sub String str = "Something"; // Ref: String, Act: String Object obj = str; // Ref: Object, Act: String top.f(obj); // Top.f(Object) at compile-time // Sub.f(Object) at run-time top.f(str); // Top.f(Object) at compile-time // Sub.f(Object) at run-time sub.f(obj); sub.f(str); // Static Binding (overloading) http://stackoverflow.com/questions/5658553/question-about-java-overloading-dynamic-binding