A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
OOP: Inheritance By: Lamiaa Said.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
: Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Objects as a programming concept
Table of Contents Class Objects.
ATS Application Programming: Java Programming
Encapsulation.
Inheritance CT1513.
Inheritance.
By Rajanikanth B OOP Concepts By Rajanikanth B
CPS120: Introduction to Computer Science
Chapter 11 Inheritance and Encapsulation and Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods which are functions operating on these data and possibly on the data belonging to other class instances. This combining of the data and related operations is called data encapsulation. An object is an instance of a class, an entity created using a class definition. Object-oriented programming Data Structures and Algorithms in Java, Third EditionCh01 – 1

Object-oriented programming Classes correspond to concepts defined in terms of common properties and common behavior, objects corresponds to actualization of these concepts. name population area loan sender compute population density get international loan Country objects: Country class: Data Structures and Algorithms in Java, Third EditionCh01 – 2

Classes and objects in Java class Country { String name; long population; double area; double loan; String sender; double density() { return area/population; } void internationalLoan(double ln, String from) { loan = ln; sender = from; } } class Countries { Country poland = new Country(); Country brazil = new Country(); } data fields methods objects Data Structures and Algorithms in Java, Third EditionCh01 – 3

Objects in Java Country poland = new Country(); Country brazil = new Country(); "Poland" poland "Germany" "Brazil" brazil "USA" Data Structures and Algorithms in Java, Third EditionCh01 – 4

Inheritance A new class can be derived form an existing class whereby the new class automatically includes (inherits) fields and methods of the existing class. The former is called a subclass or derived class, the latter is called a superclass of base class. Data Structures and Algorithms in Java, Third EditionCh01 – 5

class C1 { int n; double d; void f(int m) {... } double g(double x) {... } Inheritance class C2 extends C1 { int n; double y = g(d); void f(int m) {... } double h(double x) { super.n = 10; return n * x; } establish inheritance C2 ’s variable n, C1 ’s variable n is still accessible method f() is redefined method h() is newly defined and so is variable y variable d and method g() are inherited Data Structures and Algorithms in Java, Third EditionCh01 – 6

Accessibility control The private modifier indicates methods and fields that can be used only by this class The protected modifier means that a method or a data field is accessible to derived classes and in the package that includes the class that declares the method or the data field. A default modifier is no modifier at all, which indicates access to methods and fields in the package that includes the class that declares the methods or the data fields. Methods and fields declared public can be used by any other object. Data Structures and Algorithms in Java, Third EditionCh01 – 7

place of access privateprotected no modifier (≠ C++) public same class same package subclass same package non-subclass different package subclass different package non-subclass yes no yes yes (≠ C++) yes no yes no yes Accessibility control Data Structures and Algorithms in Java, Third EditionCh01 – 8

class C1 { private int k = 11; protected int m = 12; int n = 13; // package public int p = 14; } class C2 extends C1 { C2() { k = 21; m = 22; n = 23; p = 24; } class C3 extends C1 { C3() { k = 31; m = 32; n = 33; p = 34; } class C4 { void f() { C1 c1 = new C1(); c1.k = 41; c1.m = 42; c1.n = 43; c1.p = 44; } class C5 { void f() { C1 c1 = new C1(); c1.k = 51; c1.m = 52; c1.n = 53; c1.p = 54; } // two packages m is protected k is private // n is in different package //

Static and dynamic binding Java checks the type of object to which a reference is made and chooses the method appropriate for this type at compilation time producing static binding during run time producing dynamic binding. Polymorphism is an ability to associate with the same method name different meanings through the mechanism of dynamic binding. Data Structures and Algorithms in Java, Third EditionCh01 – 10

class A { public void process() { System.out.println("Inside A"); } void f(A a) { a.process(); } class ExtA1 extends A { public void process() { System.out.println("Inside ExtA1"); } class ExtA2 extends A { public void process() { System.out.println("Inside ExtA2"); } Dynamic binding A ob = new A(); ob.process(); ob = new ExtA1(); ob.process(); ob = new ExtA2(); ob.process(); Inside A Inside ExtA1 Inside ExtA2 output Data Structures and Algorithms in Java, Third EditionCh01 – 11

class A { public void process() { System.out.println("Inside A"); } void f(A a) { a.process(); } Dynamic binding A ob = new A(); A a1 = new A(); ExtA1 a2 = new ExtA1(); ExtA2 a3 = new ExtA2(); ob.f(a1); ob.f(a2); ob.f(a3); Inside A Inside ExtA1 Inside ExtA2 output Data Structures and Algorithms in Java, Third EditionCh01 – 12