Object-Oriented Programming

Similar documents
UNIT-II 1 KNREDDY JAVA PROGRAMMING

JAVA MOCK TEST JAVA MOCK TEST II

Programming Techniques

Module 10 Inheritance, Virtual Functions, and Polymorphism

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Rectangle. Lecture 2 week 2 - Objects & Classes Exercise 1. A. Write a driver class code in java depending on the following UML:

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Object Oriented Programming 2015/16. Final Exam June 28, 2016

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Inheritance, and Polymorphism.

Chapter 6: Inheritance

CS1150 Principles of Computer Science Objects and Classes

ITI Introduction to Computing II

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Arrays Classes & Methods, Inheritance

ITI Introduction to Computing II

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

PROGRAMMING LANGUAGE 2

C++ Inheritance and Encapsulation

Programming 2. Inheritance & Polymorphism

Inheritance and Polymorphism

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Programming II (CS300)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

8. Polymorphism and Inheritance

C++ Inheritance and Encapsulation

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

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

Final Examination Semester 3 / Year 2010

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introductory Programming Inheritance, sections

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

BoxDemo1.java jueves, 02 de diciembre de :31

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Week 5-1: ADT Design

CS-202 Introduction to Object Oriented Programming

Inheritance, Polymorphism, and Interfaces

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

1.00 Lecture 13. Inheritance

Data Abstraction. Hwansoo Han

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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?

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 5 Object-Oriented Programming

Polymorphism. return a.doublevalue() + b.doublevalue();

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

More C++ : Vectors, Classes, Inheritance, Templates

Reusing Classes. Hendrik Speleers

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

C18a: Abstract Class and Method

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

CLASSES AND OBJECTS IN JAVA

Java Object Oriented Design. CSC207 Fall 2014

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Object-Oriented Concepts

Reviewing OO Concepts

Practice for Chapter 11

Introduction to Classes

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CSC 1214: Object-Oriented Programming

CST242 Object-Oriented Programming Page 1

ENCAPSULATION AND POLYMORPHISM

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

Java for Non Majors Spring 2018

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Inheritance and Polymorphism

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Inheritance (Part 5) Odds and ends

PART A. input as command line argument. 1. Write a program to find factorial of list of number reading

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

QUESTIONS FOR AVERAGE BLOOMERS

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

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

Midterm Exam 5 April 20, 2015

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

15CS45 : OBJECT ORIENTED CONCEPTS

Inheritance (Part 2) Notes Chapter 6

Programming Exercise 14: Inheritance and Polymorphism

Creating Java Programs with Greenfoot

PASCAL - OBJECT ORIENTED

Mobile Application Development ( IT 100 ) Assignment - I

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Transcription:

Object-Oriented Programming - Encapsulation Data & code bound together - > objects - Polymorphism Overloading of constructors & methods - Inheritance A class incorporates another class into its declaration A superclass is inherited by a subclass

- A class is defined Inheritance the basic idea - Then a second class is defined that extends the first class. The second class inherits all the variables and methods from the first class and adds its own unique members. - The first class is called a superclass - The second class is called a subclass - It is possible for the superclass to keep some of its members private: i.e. label them private

Example A super class: class TwoDShape { double width; double height; void showdim() { System.out.println("Width and height are " + width + " and " + height);

A subclass: class Triangle extends TwoDShape { String style; double area( ) { return width * height / 2; void showstyle() { System.out.println("Triangle is " + style);

class Shapes { public static void main(string args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t2.width = 8.0; t2.height = 12.0; t2.style = "outlined"; System.out.println("Info for t1: "); t1.showstyle(); t1.showdim(); System.out.println("Area is " + t1.area());

class Shapes { public static void main(string args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t2.width = 8.0; t2.height = 12.0; t2.style = "outlined"; Displayed text Info for t1: Triangle is filled Width and height are 4 and 4 Area is 8 System.out.println("Info for t1: "); t1.showstyle(); t1.showdim(); System.out.println("Area is " + t1.area());

class TwoDShape { private double width; // these are private double height; // now private Private members void showdim() { System.out.println("Width and height are " + width + " and " + height); class Triangle extends TwoDShape { String style; double area() { return width * height / 2; // Error! can't access void showstyle() { System.out.println("Triangle is " + style);

Use accessor methods to set and get private members. class TwoDShape { private double width; private double height; // these are // now private // Accessor methods for width and height. double getwidth( ) { return width; double getheight( ) { return height; void setwidth(double w) { width = w; void setheight(double h) { height = h; void showdim() { System.out.println("Width and height are " + width + " and " + height);

A subclass then uses the accessors class Triangle extends TwoDShape { String style; double area( ) { return getwidth() * getheight() / 2; void showstyle() { System.out.println("Triangle is " + style);

The main now uses the accessors also class Shapes2 { public static void main(string args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.setwidth(4.0); t1.setheight(4.0); t1.style = "filled"; System.out.println("Info for t1: "); t1.showstyle(); t1.showdim(); System.out.println("Area is " + t1.area());

A subclass can have a constructor class Triangle extends TwoDShape { private String style; Triangle(String s, double w, double h) { setwidth(w); setheight(h); style = s; constructor double area() { return getwidth() * getheight() / 2; void showstyle() { System.out.println("Triangle is " + style);

With constructors in the subclass, the main can specify parameters when creating objects class Shapes3 { public static void main(string args[]) { Triangle t1 = new Triangle("filled", 4.0, 4.0); Triangle t2 = new Triangle("outlined", 8.0, 12.0); System.out.println("Info for t1: "); t1.showstyle(); t1.showdim(); System.out.println("Area is " + t1.area());

The super class can have a constructor class TwoDShape { private double width; private double height; TwoDShape(double w, double h) { width = w; height = h; parameterized constructor // Accessor methods for width and height. double getwidth() { return width; double getheight() { return height; void setwidth(double w) { width = w; void setheight(double h) { height = h;

A subclass can call a superclass constructor class Triangle extends TwoDShape { private String style; Triangle(String s, double w, double h) { super(w, h); // call superclass constructor style = s; double area() { return getwidth() * getheight() / 2; void showstyle() { System.out.println("Triangle is " + style);

Comments about a constructor in super - The super class (TwoDShape) can construct its subobject anyway it wishes, even add functionality that is unknown to the subclass without breaking existing code. - Any form of constructor defined by the superclass can be called by super( ). The constructor executed will be the one that matches the supplied parameters.