Java Inheritance.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Object Oriented Programming with Java
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance in the Java programming language J. W. Rider.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Java Generics.
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Chapter 11 Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Object Oriented Programming
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Advanced Java Programming
Java – Inheritance.
Inheritance Cse 3rd year.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Java Inheritance

What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these members as is, or it can hide the member variables or override the methods.

A little more detail Subclasses inherit those superclass members declared as public or protected. Subclasses inherit those superclass members declared with no access specifier as long as the subclass is in the same package as the superclass. Subclasses don't inherit a superclass's member if the subclass declares a member with the same name. In the case of member variables, the member variable in the subclass hides the one in the superclass. In the case of methods, the method in the subclass overrides the one in the superclass.

Overriding Methods The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then override methods as needed. For example, all classes are descendents of the Object class. Object contains the toString method, which returns a String object containing the name of the object's class and its hash code. Most, if not all, classes will want to override this method and print out something meaningful for that class.

Calling the Overridden Method Sometimes, you don't want to completely override a method. Rather, you want to add more functionality to it. To do this, simply call the overridden method using the super keyword. For example, super.overriddenMethodName();

Methods a Subclass Cannot Override A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message similar to the following and refuses to compile the program:

Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass.

Methods a Subclass Must Override A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract

Overriding vs Overloading Overriding a method from a base class in a subclass allows the behavior to differ between the classes. In Base printName(){ System.out.println( “Name is “ + name): } In Subclass printName(){ System.out.println(“My name is “+name)

Overloading is having different parameters for the same method name such as in constructors Person() Person(String fname, String lname) Person(int age) Person(String address)

Upcasting A subclass reference can be assigned to a base class reference because…. A subclass object IS a baseclass

This and Super The reference “this” refers to the current object. When an method is called, this denotes the object in which the method is called. Super This is a call to the baseclass of an object and can be used to call methods in the baseclass from a subclass

Java Interfaces and Abstract Classes

Abstract Classes In polymorphism and inheritance some classes are so generic that they should not be instantiated. Declaring a class abstract keeps it from being instantiated. The abstract class may or may not implement all methods regardless a subclass will inherit all the members of the abstract class

References to Abstract classes if classB extends classA and class a is abstract class B still passes the “is a “ test classA = new classB() creates an instance of classB but is using a reference of classA When you call a non abstract method or an abstract method dynamic binding will determine which subclass method is being called.

Interfaces

Sharing common methods Inheritance is between objects that can pass the “is a” test. Objects that do not pass this test still may need to share methods or constants

Interfaces Interfaces are a collection of constants and methods Constants in interfaces are by default Public Static Final Methods are by default Abstract

Implementation class dog extends Animal implements Sounds{ code} To use an interface, you must provide the code for all methods in the interface. Classes may implement several interfaces. Separate by a comma

Syntax for an interface Similar to class public interface ConverstionFactors{ double Inch_to MM = 2.54; double OunceToGram = 28.342; public inchToMM(double inch); public ounceToGram(double ounce); }

Interfaces Interfaces can be extended public interface Conversion extends conversionFactors{ public double poundsToGrams(double pounds): } This will have all the constants and methods of the super-interface

What to use? Keep in mind that the core reason for interfaces is to be able to have shared members of more than one base type. However, a second reason for using interfaces is the same as using an abstract base class: to prevent the client programmer from making an object of this class and to establish that it is only an interface.

Should you use an interface or an abstract class? you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, your first choice should be to make it an interface, and only if you’re forced to have method definitions or member variables should you change to an abstract class, or if necessary a concrete class.