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

Similar documents
Java Object Oriented Design. CSC207 Fall 2014

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

ECE 122. Engineering Problem Solving with Java

CS-202 Introduction to Object Oriented Programming

Programming in C# Inheritance and Polymorphism

Inheritance -- Introduction

Practice for Chapter 11

Programming II (CS300)

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance, and Polymorphism.

Inheritance & Polymorphism

Programming II (CS300)

ITI Introduction to Computing II

CS1150 Principles of Computer Science Objects and Classes

Inheritance and Encapsulation. Amit Gupta

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

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

CMSC 132: Object-Oriented Programming II. Inheritance

Data Structures (list, dictionary, tuples, sets, strings)

Chapter 14 Abstract Classes and Interfaces

ITI Introduction to Computing II

Inheritance Motivation

Inheritance and Polymorphism

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

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

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

C++ Important Questions with Answers

What is Inheritance?

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

Object Orientated Programming Details COMP360

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

PROGRAMMING LANGUAGE 2

Inheritance, Polymorphism, and Interfaces

Advanced Placement Computer Science. Inheritance and Polymorphism

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

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

Darrell Bethea June 6, 2011

Chapter 5. Inheritance

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

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

Inheritance (Deitel chapter 9)

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Full file at Chapter 2 - Inheritance and Exception Handling

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance (an intuitive description)

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

index.pdf January 21,

What are the characteristics of Object Oriented programming language?

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

Classes and Inheritance Extending Classes, Chapter 5.2

Chapter 7. Inheritance

Making New instances of Classes

ITI Introduction to Computing II

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

Lecture 18 CSE11 Fall 2013 Inheritance

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

QUESTIONS FOR AVERAGE BLOOMERS

ITI Introduction to Computing II

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

Inheritance (continued) Inheritance

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS 251 Intermediate Programming Inheritance

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Inheritance. Chapter 7. Chapter 7 1

Inheritance. Transitivity

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

CH. 2 OBJECT-ORIENTED PROGRAMMING

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

INHERITANCE Mrs. K.M. Sanghavi

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Inheritance and Polymorphism

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

25. Generic Programming

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

More On inheritance. What you can do in subclass regarding methods:

Inheritance (Outsource: )

ENCAPSULATION AND POLYMORPHISM

8. Polymorphism and Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance

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

Transcription:

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

Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance? In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class. The terms parent class and child class are also acceptable terms to use respectively. A child inherits visible properties and methods from its parent while adding additional properties and methods of its own. Subclasses and super classes can be understood in terms of the is a relationship. A subclass is a more specific instance of a superclass. For example, an orange is a citrus fruit, which is a fruit. A shepherd is a dog, which is an animal. Here is a Chocolate class below(as an example): package cakes public class ChocolateCake public function ChocolateCake() public function bake():void trace("the chocolate cake is baking"); public function frost():void trace("the chocolate cake now has frosting");

public function putcandlesoncake(numberofcandles:int):void trace("putting " + numberofcandles + " candles on the cake. "); It's a simple class with three methods: bake(), frost(), and putcandlesoncake(). Q: What is the use of extend keyword? A subclass can inherit all data members and methods of superclass using extend keyword. Inheritance is a major feature of OPP (object oriented programming). It allows us to reuse the code. The class which is reused is called parent class. User class is called child class. We use the keyword extend to define a child class that uses a parent class. Syntax: Class super // code Class sub extend super Examples: // code Superclass Student Shape Quadrilateral Employee Subclass Graduate Students, Undergraduate Student. Circle, Rectangle, Triangle. Square, Rectangle. Faculty member, Staff members.

Is a relationship represents inheritance. Object of subclass is an object of superclass. Q: Define protected? Protected data members or methods is accessible only from within its package. Other classes can be used by inheritance only subclass can use protected data member or methods. Q: What is overloading? Defining multiple methods with same name but different signatures is known as overloading. Overloading methods must have different parameters and they may have different return type. Example: Class circle Static int Add(int a, int b) return a+b; Static int Add (int x, int y, int z) return x+y+z;

Q: What is overriding? When a method of super class is redefined in a subclass with same name and signature but different implementation details, this is known as overriding. Rules regarding to overriding methods: 1. Methods that are static, final and private cannot be overridden. 2. Protected method can override only these methods that don t have any access modifier. 3. Overriding method cannot have a more restricted access than the original method. 4. The overriding method cannot throw any new exception. Example: Class Animal public void move() System.out.println( Animal Moves ) Q: What is access control? Access modifiers define the access privileges of class, interface, constructors, methods and data members. Access modifiers consist of: Public. Private. Protected. If no modifier is present then default access is package private.

Q: What is composition? Composition is a design technique to represent has a relationship. Car has an engine. Person has a Job. Using inheritance: Use keyword extend. Private data members of superclass are not directly accessible to subclass. Example: Class point protected int x, y; public point() x=0; y=0; public point (int a, int b) x=a; y=b; public void setpoint (int a, int b) x=a; y=b; Data Members Constructor Methods

public class Circle extends point protected double rad; public circle () Setradius(0); public void SetRadius (double r) Using extend keyword getting Rad=; the radius of circle public double area () return 3.2438*rad*rad; Q: Explain polymorphism? Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding. If you overload static method in java, it is the example of compile time polymorphism.

Q Define runtime polymorphism? Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. Q: What is encapsulation? Encapsulation is one of the fourth fundamental concept of object oriented programming. Encapsulation in java is a mechanism of wrapping the data (variables) and code acting on the data together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore it is also known as data hiding. Declare variables of a class as private. Provide public setter and getter methods to modify and view the variable values. The field s of a class can be made read only or write only. 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 field and user if class do not need to change any of their code. Example: public class Car private String name; private double topspeed; public Car( ) public String getname( ) return name;

public void setname (String name) this.name=name; public void settopspeed(double SpeedMPH) Topspeed = SpeedMPH; public double gettopspeedmph( ) return TopSpeed; public double gettopspeedkmh( ) return TopSpeed*1.609344; Chapter Ended Visit: www.techo786.wordpress.com for DAE updates