Arrays Classes & Methods, Inheritance

Similar documents
JAVA MOCK TEST JAVA MOCK TEST II

C++ Important Questions with Answers

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Interfaces

Inheritance and Polymorphism

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

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

Programming overview

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

PROGRAMMING LANGUAGE 2

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

INHERITANCE AND EXTENDING CLASSES

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

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

K.Yellaswamy Assistant Professor CMR College of Engineering & Technology

1 Shyam sir JAVA Notes

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS260 Intro to Java & Android 03.Java Language Basics

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object-Oriented Concepts

Inheritance, and Polymorphism.

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

Inheritance. Transitivity

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Week 5-1: ADT Design

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

Inheritance (IS A Relationship)

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Instance Members and Static Members

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

Lecture 18 CSE11 Fall 2013 Inheritance

What is Inheritance?

Inheritance. COMP Week 12

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

More on Inheritance. Interfaces & Abstract Classes

CS-202 Introduction to Object Oriented Programming

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

Chapter 5. Inheritance

FAQ: Classes & Objects

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

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

Chapter 10 Classes Continued. Fundamentals of Java

G Programming Languages - Fall 2012

ECE 122. Engineering Problem Solving with Java

Object Oriented Programming. Java-Lecture 11 Polymorphism

JAVA Programming Language Homework I - OO concept

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

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

CMSC 132: Object-Oriented Programming II

Chapter 14 Abstract Classes and Interfaces

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance -- Introduction

ENCAPSULATION AND POLYMORPHISM

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

Chapter 4. Defining Classes I

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Exam Duration: 2hrs and 30min Software Design

CSC 1214: Object-Oriented Programming

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Java for Non Majors Spring 2018

CS111: PROGRAMMING LANGUAGE II

The Java language has a wide variety of modifiers, including the following:

CS 180 Final Exam Review 12/(11, 12)/08

Example: Count of Points

Object Oriented Programming

CMSC 132: Object-Oriented Programming II

Exercise: Singleton 1

STRUCTURING OF PROGRAM

Object Orientated Programming Details COMP360

1.00 Lecture 8. Using An Existing Class, cont.

QUESTIONS FOR AVERAGE BLOOMERS

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

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

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 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Chapter 6 Introduction to Defining Classes

Inheritance. OOP: Inheritance 1

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Inheritance and Polymorphism

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Comp 249 Programming Methodology

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Constructor in Java. What is a Constructor? Rules for create a Java Constructor

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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

ECE 122. Engineering Problem Solving with Java

CGS 2405 Advanced Programming with C++ Course Justification

CSE 401/M501 Compilers

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Transcription:

Course Name: Advanced Java

Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance

INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value of the specified data type. int number; double income; char letter; An array is an object containing a list of elements of the same data type

Arrays We can create an array by: Declaring an array reference variable to store the address of an array object. Creating an array object using the new operator and assigning the address of the array to the array reference variable. Here is a statement that declares an array reference variable named dailysales: double[ ] dailysales; The brackets after the key word double indicate that the variable is an array reference variable. This variable can hold the address of an array of values of type double. We say the data type of dailysales is double array reference.

The second statement of the segment below creates an array object that can store seven values of type double and assigns the address of the array object to the reference variable named dailysales : double[ ] dailysales; dailysales = new double[7]; The operand of the new operator is the data type of the individual array elements and a bracketed value that is the array size declarator. The array size declarator specifies the number of elements in the array.

It is possible to declare an array reference variable and create the array object it references in a single statement. The statement below creates a reference variable named dailysales and an array object that can store seven values of type double as illustrated below: Here is an example: double[ ] dailysales = new double[7]; dailysales address 1 st value 2 nd value 3 rd value 4 th value 5 th value 6 th value 7 th value

Accessing Array Elements We can access the array elements and use them like individual variables. Each array element has a subscript. This subscript can be used to select/pinpoint a particular element in the array. Array subscripts are offsets from the first array element. The first array element is at offset/subscript 0, the second array element is at offset/subscript 1, and so on. The subscript of the last element in the array is one less than the number of elements in the array.

final int DAYS = 7; double[ ] dailysales = new double[days]; dailysales address 0 0 0 0 0 0 0 [0] [1] [2] [3] [4] [5] [6] Subscripts dailysales[0], pronounced dailysales sub zero, is the first element of the array. dailysales[1], pronounced dailysales sub one, is the second element of the array. dailysales[6], pronounced dailysales sub six, is the last element of the array.

Array subscripts begin with zero and go up to n - 1, where n is the number of elements in the array. final int DAYS = 7; double[ ] dailysales = new double[days]; dailysales address 0 0 0 0 0 0 0 [0] [1] [2] [3] [4] [5] [6] Subscripts Typically, we use a loop to cycle through all the subscripts in the array to process the data in the array.

Array Initialization Like other variables, you may give array elements an initial value when creating the array. Example: The statement below declares a reference variable named temperatures, creates an array object with room for exactly tens values of type double, and initializes the array to contain the values specified in the initialization list. double[ ] temperatures = {98.6, 112.3, 99.5, 96, 96.7, 32, 39, 18.1, 111.5}; By default, Java initializes the array elements of a numeric array with the value 0. int[ ] attendance = new int[5] ;

Array Length Each array object has an attribute/field named length. This attribute contains the number of elements in the array. For example, in the segment below the variable named size is assigned the value 5, since the array referenced by values has 5 elements. int size; int[ ] values = {13, 21, 201, 3, 43}; size = values.length; Notice, length is an attribute of an array not a method - hence no parentheses.

class definition class classname { field declarations { initialization code } Constructors Methods }

Inheritance On the surface, inheritance is a code re-use issue. we can extend code that is already written in a manageable manner. Inheritance is more it supports polymorphism at the language level

Inheritance The derivation of one class from another class is called Inheritance. Types of inheritance

Inheritance One object type is defined as being a special version of some other object type. a specialization. The more general class is called: base class, super class, parent class. The more specific class is called: derived class, subclass, child class. 15

A class that is inherited is called a superclass. The class that does the inheriting is called as subclass. In above figure all class A is superclass. A subclass inherits all instance variables and methods from its superclass and also has its own variables and methods. One can inherit the class using keyword extends. Syntax : Class subclass-name extends superclass-name { // body of class. }

In java, a class has only one super class. Java does not support Multiple Inheritance. One can create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. However, no class can be a superclass of itself.

Example class A //superclass { int num1; //member of superclass int num2; //member of superclass void setval(int no1, int no2) //method of superclass { num1 = no1; num2 = no2; } } class B extends A //subclass B { int multi; //member of subclass void mul() //method of subclass { multi = num1*num2; //accessing member of superclass from subclass } } Output : Multiplication is 30 class inhe2 { public static void main(string args[]) { B subob = new B(); subob.setval(5,6); //calling superclass method through subclass object subob.mul(); System.out.println("Multiplication is " + subob.multi); } }

Note Private members of superclass are not accessible in sub class Superclass is also called parent class or base class, subclass is also called child class or derived class.