[PDF] [PDF] 1 Chapter 10 Topics Inheritance Concepts A Sample Vehicle

Abstract Classes and Methods See Example 10 3 CheckingAccountClient java (next slide) Home CheckingAccount class cannot directly access balance name yes protected fields yes yes, by calling method from subclass methods yes



Previous PDF Next PDF





[PDF] Abstract Superclass

An abstract method defines the method name and arguments, but there's no method code Methods that subclasses should implement are declared abstract Instances of the The java drawing class JComponent is an example of this sort of common superclass with Get our RT class name -- just showing off // some of 



[PDF] Abstract Classes and Interfaces Abstract Classes

6 août 2013 · 1 Abstract Classes • A Java class that cannot be instantiated, but instead Beer is a very general name for lots of different types of Concrete subclass is required to implement the abstract methods beersOnTap get(i)



[PDF] Java Classes, Objects, Inheritance, Abstract and Interfaces Recap 2

Classes, Objects, Inheritance, Abstract Classes and Interfaces 1 Constructors must have the same name as the class itself The get and set methods are used to read and in a subclass, the method defined in the superclass is hidden



[PDF] Chapter 4 Inheritance in Java

another class is called derived class, child class or subclass access class subclass_name extends superclass_name { abstract class {



Solutions to Exercises

The Java platform promotes portability by providing an abstraction over name or parameter's name with this or the class name followed by the member access access to the subclass's overriding method's access could be made private,



[PDF] Chapter 11 Abstract Classes and Interfaces

GeometricObject1 java +get Radius(): doubl e class ▻ If a subclass of an abstract superclass does not implement all the The interface name and the



[PDF] Chapter 7 Inheritance and Abstract Classes - GMU CS Department

hierarchy of creating separate classes, using fields to get some aggregation going, but We can say that the Student class is a subclass of the Person class, or that We get a complaint from the Java compiler that name and age are private



[PDF] CS200: Advanced OO in Java interfaces, inheritance, abstract

Object can shield variables from external access abstract public class abstract- base-class-name { Some subclass is required to override the abstract



[PDF] 1 Chapter 10 Topics Inheritance Concepts A Sample Vehicle

Abstract Classes and Methods See Example 10 3 CheckingAccountClient java (next slide) Home CheckingAccount class cannot directly access balance name yes protected fields yes yes, by calling method from subclass methods yes



Final and Abstract Classes Restricting Inheritance Abstract Classes

This is achieved in Java by using the keyword final as follows: final class Marks The abstract methods of an abstract class must be defined in its subclass £ We cannot declare example marks (test1 and test 2 marks) and access methods } class Results multiple methods which have the same name, but different 

[PDF] java abstract class method return subclass

[PDF] java abstract class return subclass

[PDF] java abstract class return subclass type

[PDF] java abstract class with example

[PDF] java access resources from jar

[PDF] java add edit delete sample program

[PDF] java advanced features and programming techniques

[PDF] java api tutorial for beginners pdf

[PDF] java application development tutorial

[PDF] java architect interview questions

[PDF] java array exercises with solutions pdf

[PDF] java array pointer

[PDF] java array programs exercise

[PDF] java arrays

[PDF] java assignments on collections

1 Home

Chapter 10

Object-Oriented Programming

Part 3:

Inheritance, Polymorphism, and

Interfaces

Date Chapter

11/6/2006 Chapter 10, start Chapter 11

11/13/2006 Chapter 11, start Chapter 12

11/20/2006 Chapter 12

11/27/2006 Chapter 13

12/4/2006 Final Exam

12/11/2006 Project Due

Home Home

Topics

• Inheritance Concepts • Inheritance Design - Inherited Members of a Class - Subclass Constructors - Adding Specialization to the Subclass - Overriding Inherited Methods • The protected Access Modifier •AbstractClasses and Methods • Polymorphism • Interfaces Home

Inheritance Concepts

• A common form of reuse of classes is inheritance. • We can organize classes intohierarchiesof functionality. • The class at the top of the hierarchy (superclass) defines instance variables and methods common to all classes in the hierarchy. • We derive a subclass,which inheritsbehavior and fields from the superclass. Home

A Sample Vehicle Hierarchy

• This hierarchy is depicted using a Unified

Modeling Language (UML) diagram.

• In UML diagrams, arrows point from the subclass to the superclass. 2 Home

Superclasses and Subclasses

• A superclass can have multiple subclasses. • Subclasses can be superclasses of other subclasses. • A subclass can inherit directly from only one superclass. • All classes inherit from the

Objectclass.

Home

Superclasses and Subclasses

• A big advantage of inheritance is that we can write common code once and reuse it in subclasses. - Generalization • A subclass can define new methods and instance variables, some of which may override(hide) those of a superclass. - Specialization Home

Specifying Inheritance

• The syntax for defining a subclass is to use the extends keyword in the class header, as in accessModifier class SubclassName extendsSuperclassName // class definition • The superclass name specified after the extendskeyword is called the direct superclass. • As mentioned, a subclass can have many superclasses, but only one direct superclass. Home

An Applet Hierarchy

• When we wrote an applet, we defined a subclass. • We say that inheritance implements an " is a" relationship, in that a subclass object " is a" superclass object as well. • Thus,RollABall"is a"JApplet(its direct superclass), Applet, Panel,

Container, Component, and

Object.

•RollABallbegins with more than

275 methods and 15 fields inherited

from its 6 superclasses. 3 Home

The Bank Account Hierarchy

• TheBankAccountclass is the superclass. - Instance variables: •balance(double) •MONEY (final DecimalFormat) - Methods: • Default and overloaded constructors •depositand withdrawmethods •balanceaccessor •toString •See Example 10.1 BankAccount.java (next slide) Home

BankAccount.java 1/3

import java.text.DecimalFormat; public class BankAccount { public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" ); private double balance; public

BankAccount( ) {

balance = 0.0; public

BankAccount( double startBalance ) {

deposit( startBalance ); Home

BankAccount.java 2/3

public doublegetBalance( ) { return balance; public void deposit( double amount ) { if ( amount >= 0.0 ) balance += amount; else

System.err.println( "Deposit amount must be

positive." ); Home

BankAccount.java 3/3

public void withdraw( double amount ) { if ( amount >= 0.0 && amount <= balance ) balance -= amount; else System.err.println( "Withdrawal amount must be positive " + "and cannot be greater than balance" ); public String toString( ) return ( "balance is " + MONEY.format( balance ) ); 4 Home

TheCheckingAccountClass

• We derive theCheckingAccountsubclass from

BankAccount:

public class CheckingAccount extends BankAccount • A subclass inherits all the publicmembers of a superclass. Thus, theCheckingAccountclass inherits - the MONEY instance variable - ThegetBalance, deposit, withdraw, andtoString methods •See Example 10.3 CheckingAccountClient.java (next slide) Home

CheckingAccountClient.java

public class CheckingAccountClient { public static void main( String [] args ) {

CheckingAccount c1 = new CheckingAccount( );

System.out.println( "New checking account: " + c1 ); c1.deposit( 350.75 ); System.out.println( "\nAfter depositing $350.75: " + c1 ); c1.withdraw( 200.25 ); System.out.println( "\nAfter withdrawing $200.25: " + c1 ); Home privateMembers • Superclass members declared as privateare NOT inherited, although they are part of the subclass. • Thus, the balanceinstance variable is allocated to all

CheckingAccountobjects, but methods of the

CheckingAccountclass cannot directly access balance. • To set or get the value of balance, theCheckingAccount methods must call the withdraw, deposit,orgetBalance methods of the Superclass BankAccount. • This simplifies maintenance because theBankAccount class enforces the data validation rules for balance. Home protectedMembers •protectedmembers are inherited by subclasses (like publicmembers), while still being hidden from client classes (like privatemembers). • Also, any class in the same package as the superclass can directly access a protectedfield, even if that class is not a subclass. • Disadvantage: - Because more than one class can directly access a protectedfield, protectedaccess compromises encapsulation and complicates maintenance of a program. - For that reason, we prefer to use private, rather than protected,for our instance variables. 5 Home

Inheritance Rules

nononoprivatemethodsno, must call accessorsand mutators no, must call accessors

and mutatorsnoprivate fieldsnoyes, by calling method from subclass methods yesprotected methodsno, must call accessorsand mutators

yes, by using field

nameyesprotected fieldsyesyes, by calling method from subclass methodsyespublic methodsyesyes, by using field nameyespublicfields

Directly Accessible by

Client of Subclass?Directly Accessible by Subclass?Inherited by subclass?Superclass Members Home

Subclass Constructors

• Constructors are not inherited. • However, the subclass can call the constructors of the superclass to initialize inherited fields. •Implicitinvocation - The default constructor of the subclass automatically calls the default constructor of the superclassquotesdbs_dbs19.pdfusesText_25