Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
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.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
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.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 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.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Lecture 12 Inheritance.
Inheritance-Basics.
Inheritance and Polymorphism
INHERITANCE IN JAVA.
Week 8 Lecture -3 Inheritance and Polymorphism
Modern Programming Tools And Techniques-I Inheritance
Interface.
Interfaces.
METHOD OVERRIDING in JAVA
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey

Inheritance The concept of classes automatically containing the variables and methods defined in their super classes is kown as inheritance. Inheritance allows us to extend the capabilities of existing classes and hence it allows us to reuse the existing classes and obtain new classes from them. Person Student Teacher

Forms of Inheritance ● Single Inheritance – A Student is a Person. ● Hierarchical Inheritance – A Student is a Person. A Teacher is a Person... ● Multilevel Inheritance – A Student is a Person. An O/L Student is a Student... ● Multiple Inheritance (Not Directly Supported) – A Student is a Person and a Child and a...

A Closer Look A Student is a Person Parent/Super or Base class subclass/ derived class or child class

Defining a Subclass class Student extends Person { byte year; String regNo; void showInfo() { // Overriding.... } class subclassname extends superclassname { additional variables; additional methods; }

Exercise Complete the showInfo method in the above class to show all the information of an instace of a Student class. Write a constructor for the Student class that will initialize all the variables: name, age, year and regNo. The constructor should display an error message if the age of a student is found to be greater than 24 years.

Answers for the First Exercise // Here is one possible answer: void showInfo() { System.out.println(name + " is " + age + " years old."); System.out.println(name + " is in year " + year); System.out.println(name + "\'s registration number is " + regNo); } // Here is the prefered answer: void showInfo() { super.showInfo(); // calls the showInfo of super class System.out.println(name + " is in year " + year); System.out.println(name + "\'s registration number is " + regNo); }

Answer for the Second Exercise class Student extends Person { byte year; String regNo; Student(String name, byte age, byte year, String regNo) { super(name, age);// calls the super class constructor this.year = year; if(year > 24) {// produce the error message System.out.println("Age is greater than 24"); } this.regNo = regNo; }... } Shoud be the first statement in the constructor

Method Overriding Overriding allows us to redefine methods in the super class to obtain different behavior in subclasses. Now both the super class and the subclass have methods with equivalent signatures. If you call an overridden method of of an instance of the subclass, then the super class method is NOT executed. Example: Student st = new Student(...); st.showInfo();// Overridden method in the Student class // will be executed here.

Final Variables/Methods If you declare a method as final in a super class, the method cannont be overridden in a subclass. final void showInfo() { // should appear in super class... } If you declare a final variable in a super class, the value of it cannot be changed in subclasses. final int SIZE = 110;// should appear in super class

Final Classes A final class cannot be subclassed. You may declare a class to be final mostly for security purposes. final class A {// Declare a final class... } class B extends A {// This does not work!... } final class C extends D { // This will work if D is not a final class....// But C is now final and you cannot say }// class E extends C

"finalize()" Method If the Garbage Collector determins that a cirtain object is not going to be used anymore, it is removed from the memory. Before the removal GC calls the finalize() method of the corresponding object. finalize() is a method of the "Object" class and hence all the classes we write may override this method. Next slide illustrates a simple example of using the finalize method to find the number of objects created when GC is removing the first created object.

class A {// A simple test class static int i = 0;// Why did I declare this as static? A() {i++; }// The constructor public void finalize() { System.out.println(i + " objects created"); System.exit(0); // Exit when the first object is being removed } class FinalizeTest { public static void main(String[] args) { while(true) {// This loop will never terminate new A();// Instantiate A } Creates lots of A

Abstract Methods/Classes If we declare variables/methods as final, they cannot be changed in subclasses. Opposed to final, if we declare a method as abstract in a class, we cannot instantiate that class and always we should subclass and define the abstract method and then we can instantiate the subclass. Abstract methods are simply declarations of methods and they do not have a method body (ie {...}). Subclasses should always define them. Now the subclasses can be instantiated. A class having abstract methods should be declared abstract. We cannot define abstract constructors of abstract static methods.

Abstract Methods/Classes (Cont.) abstract class Person { String name; byte age; abstract void showInfo();// No method body void setInfo(String _name, byte _age) { name = _name; age = _age; } We cannot say new Person().

Abstract Methods/Classes (Cont.) Here is a Student class: class Student extends Person { byte year; String regNo; void showInfo() {// Define the method body System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Year: " + year); System.out.println("Reg: " + regNo); }

Abstract Methods/Classes (Cont.) Here is a Teacher class: class Student extends Person { int epf; String subject; void showInfo() {// Define the method body System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("EPF Number: " + epf); System.out.println("Subject " + subject); }

Abstract Methods/Classes (Cont.) class PeopleTest {// Testing our classes public static void main(String[] args) { Student st = new Student(); st.setInfo("Nimal", (byte) 22); st.year = 2; st.regNo = new String("K/2003/34"); Teacher jTeacher = new Teacher(); jTeacher.setInfo("Kamal", (byte)26); jTeacher.epf = 742; jTeacher.subject = "Java"; st.showInfo(); jTeacher.showInfo(); } Polymorphism!

Visibility Control Access to the members of a class can be limited by using visibility modifiers of access modifiers.

Exercises Do all the exercises in your book!