Inheritance class TwoDShape { private double width;

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Intro to OOP with Java, C. Thomas Wu
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
OBJECT ORIENTED PROGRAMMING B.TECH II YR II SEMESTER(TERM 08-09) UNIT 3 PPT SLIDES TEXT BOOKS: 1. Java: the complete reference, 7th edition, Herbert schildt,
Inheritance Like father like son Image from:
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
C++ Programming: chapter 7 – inheritence 2014, Spring Pusan National University Ki-Joune Li 1.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
L 3.1 Forms of Inheritance Inheritance is used in a variety of way and for a variety of different purposes. Inheritance for Specialization Inheritance.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Introducing Classes By: Pavan D.M.
INHERITANCE.
Inheritance-Basics.
Java Inheritance.
UNIT-2.
C# - Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Week 4 – OOP II EE422C - Software Design and Implementation II • Fall 2017 (Slides modified from originals by Prof. Mike Scott)
Polymorphism 11-Nov-18.
Modern Programming Tools And Techniques-I Inheritance
Introducing Classes By: Pavan D.M.
OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING
INHERITANCE.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Code Animation Examples
Inheritance Cse 3rd year.
Object Oriented Programming in JAVA
Inheritance CT1513.
class PrintOnetoTen { public static void main(String args[]) {
Index Understanding static Final Inheritance Super
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism
An Example of Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Key Concepts from 1301: What you should already know
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
class Box { double width; double height; double depth; }
Presentation transcript:

Inheritance class TwoDShape { private double width; // Add more constructors to TwoDShape. class TwoDShape { private double width; private double height; // A default constructor. TwoDShape() { width = height = 0.0; } // Parameterized constructor. TwoDShape(double w, double h) { width = w; height = h;

Inheritance // Construct object with equal width and height. TwoDShape(double x) { width = height = x; } // 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);

Inheritance class Triangle extends TwoDShape { private String style; // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { private String style; // A default constructor. Triangle() { super(); style = "null"; } // Constructor Triangle(String s, double w, double h) { super(w, h); // call superclass constructor style = s;

Inheritance // Construct an isosceles triangle. Triangle(double x) { super(x); // call superclass constructor style = "isosceles"; } double area() { return getWidth() * getHeight() / 2; void showStyle() { System.out.println("Triangle is " + style);

Inheritance Triangle t2 = new Triangle("right", 8.0, 12.0); class Shapes5 { public static void main(String args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle("right", 8.0, 12.0); Triangle t3 = new Triangle(4.0); t1 = t2; System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println(); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); System.out.println("Info for t3: "); t3.showStyle(); t3.showDim(); System.out.println("Area is " + t3.area()); }

Inheritance class A { int i; } // Using super to overcome name hiding. class A { int i; } // Create a subclass by extending class A. class B extends A { int i; // this i hides the i in A B(int a, int b) { super.i = a; // i in A i = b; // i in B void show() { System.out.println("i in superclass: " + super.i); System.out.println("i in subclass: " + i); class UseSuper { public static void main(String args[]) { B subOb = new B(1, 2); subOb.show();

Inheritance class A { int i, j; A(int a, int b) { i = a; j = b; } // Method overriding. class A { int i, j; A(int a, int b) { i = a; j = b; } // display i and j void show() { System.out.println("i and j: " + i + " " + j); class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c;

Inheritance // display k – this overrides show() in A void show() { System.out.println("k: " + k); } class Override { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); // this calls show() in B

Inheritance class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show() { super.show(); // this calls A's show() System.out.println("k: " + k);

Inheritance /* Methods with differing type signatures are overloaded and not overridden. */ class A { int i, j; A(int a, int b) { i = a; j = b; } // display i and j void show() { System.out.println("i and j: " + i + " " + j); // Create a subclass by extending class A. class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c;

Inheritance // overload show() void show(String msg) { System.out.println(msg + k); } class Overload { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show("This is k: "); // this calls show() in B subOb.show(); // this calls show() in A

Inheritance class A { final void meth() { System.out.println("This is a final method."); } class B extends A { void meth() { // ERROR! Can't override. System.out.println("Illegal!");