CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
Object Oriented Programming
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
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.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Chapter 10 Classes Continued
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.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Interface.
Extending Classes.
Java Programming Language
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2

CSM-Java Programming-I Lesson-1 final Keyword Any number that is not completely self- explanatory should be declared as a named constant. A final variable is a constant. Once its value has been set, it cannot be changed. Use named constants to make your programs easier to read and maintain. Eg: final int DAYS_PER_YEAR = 365;

CSM-Java Programming-I Lesson-1 static Keyword Java numbers are not objects, so you cannot invoke a method on a number. Eg: To calculate sqrt or power. A static method does not operate on an object. Eg: Math.sqrt(x); Math.round(y);

CSM-Java Programming-I Lesson-1 Static Revisited When a member is declared static, it can be accessed before any objects of its class are created. Both methods and variables can be declared static. A static member is a member that is only one per class, rather than one in every object created from that class. A static method can access only static variables and static methods of the class. There is no this reference because there is no specific object being operated on. classname.staticmethod();

CSM-Java Programming-I Lesson-1 Inheritance When a class B acquires the properties of another class A, then class B is said to have inherited class A. Here, class A is a superclass and class B is a subclass. A subclass inherits all the instance variables and methods defined by the superclass and adds its own, unique elsements. The keyword extends is used to inherit a class. Eg: class subclass extends superclass {

CSM-Java Programming-I Lesson-1 Inheritance Every class in Java is an extended class, whether it is declared with an extends keyword or not. If a class does not explicitly extend from another class, it implicitly extends the Object class.

CSM-Java Programming-I Lesson-1 Example: Inheritance public class BankAccount { int acctNum; String name; public BankAccount(int bAcctNum, String name) { acctNum = bAcctNum; ……. } public double getBalance () { …… }

CSM-Java Programming-I Lesson-1 Example: Inheritance public class CheckingAccount extends BankAccount { double interest; public CheckingAccount(int aNum, String aName, double aInt) { super (aNum, aName); interest = aInt; } public double calculateInterest() { ……. }}

CSM-Java Programming-I Lesson-1 super Whenever a subclass class super(), it is calling the constructor of its immediate superclass. super() must be the first statement executed inside a subclass constructor. super.member always uses the superclass’s member (member could be a method or an instance variable)

CSM-Java Programming-I Lesson-1 Example : super public class BankAccount { protected double getBalance() { return ; } public class SavingsAccount extends BankAccount{ protected double getBalance() { return ; }

CSM-Java Programming-I Lesson-1 super protected void printBalance() { BankAccount ba = this; //superclass variable //can reference //subclass object. System.out.println(super.getBalance()); System.out.println(this.getBalance()); System.out.println(ba.getBalance()); } Output:

CSM-Java Programming-I Lesson-1 Method Overriding When a method in a subclass has the same name and type signature and a method in its superclass, then the method in the subclass is said to override the method in the subclass. Only non-static methods can be overridden. Both signature and return type must be the same as the superclass. The throws clause of an overriding method can have fewer types listed than the method in the superclass, or more specific types or both.

CSM-Java Programming-I Lesson-1 Example: Method Overriding public class Parent { public void hello(){ System.out.println(“Hello from parent”); } public class Child extends Parent { public void hello(){ System.out.println(“Hello from Child”); }

CSM-Java Programming-I Lesson-1 Method Overriding An extended class can change the access of a superclass’s methods, but only if it provides more access. A method declared protected in the super class can be redeclared protected or public, but not private. Fields cannot be overridden; they can only be hidden. To access the hidden fields use the super keyword.

CSM-Java Programming-I Lesson-1 final and Inheritance A method declared final cannot be overridden A class can also be declared final. Such a class cannot be extended. final class Security { // } A final class’s methods are implicitly final. static and private methods cannot be overridden.

CSM-Java Programming-I Lesson-1 Abstract Classes A superclass that only defines a generalized form that will be shared by all its subclasses, leaving the implementation details to its subclasses is said to an abstract class. A concrete class has concrete methods, including implementations of any abstract methods inherited from its superclasses. Any class with abstract methods should be declared abstract.

CSM-Java Programming-I Lesson-1 Example: Abstract Class abstract class Shape { abstract double area(); public void display () {// concrete method // Do something } class Square extends Shape { double area() { // Do something }

CSM-Java Programming-I Lesson-1 Abstract Classes An abstract class cannot have objects because it is not complete, but it can have references. Static methods and constructors cannot be declared abstract. Any subclass of an abstract class must either implement all the abstract methods in the superclass or be itself declared abstract. A concrete method can be overriden to become abstract. It is illegal to declare a class both final and abstract.