Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
– Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Topics Inheritance introduction
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
CSII Final Review. How many bits are in a byte? 8.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
m/c 6-10 t/f 6-10 AP free style s/a
Programming in Java: lecture 7
Web Design & Development Lecture 9
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Advanced Programming in Java
Advanced Programming in Java
Final and Abstract Classes
One class is an extension of another.
Interface.
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Modern Programming Tools And Techniques-I Inheritance
One class is an extension of another.
More inheritance, Abstract Classes and Interfaces
Interface.
Interfaces.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
METHOD OVERRIDING in JAVA
Advanced Programming in Java
Java Inheritance.
Advanced Programming in Java
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004

Inheritance Example 1 class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { } public class InheritanceExample1 { public static void main(String args[]) { SubClass sc = new SubClass(); sc.doSomething(); BaseClass bc = new SubClass(); bc.doSomething(); }

Inheritance Example 2 class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { public void doSomething() { System.out.println("SubClass doSomething"); } public class InheritanceExample2 { public static void main(String args[]) { SubClass sc = new SubClass(); sc.doSomething(); BaseClass bc = new SubClass(); bc.doSomething(); }

Some more terminology A derived class is called a “ subclass ” of the base class. The base class is called the “ superclass ” of a derived class.

Example 3 (with super) class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { public void doSomething() { System.out.print("Super: "); super.doSomething(); System.out.println("SubClass doSomething"); }

A (slightly) more concrete example class ChessPiece { public void print() { System.out.println("Generic chess piece"); } class King extends ChessPiece { public void print() { System.out.println("King"); } class Queen extends ChessPiece { public void print() { System.out.println("Queen"); }

A (slightly) more concrete example class Bishop extends ChessPiece { public void print() { System.out.println("Bishop"); } public class InheritanceExample4 { public static void main(String args[]) { ChessPiece chessboard[][] = new ChessPiece[8][8]; chessboard[0][2] = new Bishop(); chessboard[0][3] = new Queen(); chessboard[0][4] = new King(); chessboard[0][5] = new Bishop(); for(int i=2; i<=5; i++) chessboard[0][i].print(); }

Abstract Classes abstract class ChessPiece { abstract public void print(); }

Interfaces interface ChessPiece { public void print(); } class King implements ChessPiece { public void print() { System.out.println("King"); } class Queen implements ChessPiece { public void print() { System.out.println("Queen"); }

Difference Between Classes and Interfaces An interface method cannot have a body. (interface methods are implicitly ‘ abstract ’ ) All member variables of an interface are constants (they are implicitly ‘ final ’ ). An abstract class can have bodies for one or more of its methods. An abstract class can have non-constant variables.

Other Differences When creating a class: –It can extend at most 1 class (i.e. a class can only have one superclass) –It can implement multiple interfaces