Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 1 Inheritance University Of Ha’il.
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.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 10: Introduction to Inheritance
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
More about classes and objects Classes in Visual Basic.NET.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
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.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz.
Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Object-Oriented Programming: Inheritance
Inheritance using Java
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
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.
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
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.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
These materials where developed by Martin Schray
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
Object-Oriented Programming: Inheritance
Object Oriented Programming
CS240: Advanced Programming Concepts
One class is an extension of another.
Object-Oriented Programming: Inheritance
Java Programming Language
Advanced Java Programming
METHOD OVERRIDING in JAVA
Java – Inheritance.
Object-Oriented Programming: Inheritance
Java Inheritance.
Object-Oriented Programming: Inheritance
Final and Abstract Classes
Topics OOP Review Inheritance Review Abstract Classes
Object-Oriented Programming: Inheritance
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz

Compunet Corporation2 Inheritance –The original class is called the base class –The new class is often called the derived class or the subclass –Inheritance is often referred to as extending the base class

Compunet Corporation3 Inheritance One of the primary objective of OOP, the reuse the code can be achieved by this technique The Inherits clause in a class declaration establishes an inheritance relationship between two classes class ClassName2 Inherits ClassName1 // body of the class End Class

Compunet Corporation4 Inheritance Method Example Imports System.Console Class Class1 Sub Method1() WriteLine("Base class method1") End Sub End Class Class Class2 Inherits Class1 ' On next Line Sub Method2() WriteLine("Sub class method2") End Sub End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() C1.Method1() C2.Method1() C2.Method2() End Sub End Class Output: Base class method1 Sub class method2

Compunet Corporation5 Inheritance and Variables Inherit Variable A class inherits the state (variable) and behavior ( method ) defined by all of its super classes. Therefore an object has one copy of every instance variable of its own class and its super classes.

Compunet Corporation6 Inheritance Public Variable Example Imports System.Console Class Class1 Public Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Public Dim K As Integer=300 End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() WriteLine(C1.J) ' 100 WriteLine(C2.J) ' 100 WriteLine(C2.K) ' 300 End Sub End Class Output:

Compunet Corporation7 Inheritance Private Variable Example Imports System.Console Class Class1 Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Dim K As Integer=300 End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() WriteLine(C1.J) ' Error WriteLine(C2.J) ' Error WriteLine(C2.K) ' Error End Sub End Class Output: Compilation Error, because J and K are private and are Not accessible outside the class

Compunet Corporation8 Inheritance Protected Variables Protected elements are accessible only from within their own class or from a derived class.

Compunet Corporation9 Inheritance Example Imports System.Console Class Class1 Protected Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Dim K As Integer=300 Sub Method1() WriteLine (J) WriteLine (K) End Sub End Class Class M1 Shared Sub Main() Dim C2 As New class2() C2.Method1() End Sub End Class Output:

Compunet Corporation10 Inheritance and Variables Inherit Variable Hiding If a variable of a class has a same name (type maybe different) as a base class variable, in that case derived class variable hides the base class variable.

Compunet Corporation11 Inheritance Shadows Example Imports System.Console Public Class BaseCls Public Z As Integer = 100 End Class Public Class DervCls Inherits BaseCls Public Shadows Z As String = "*" End Class Public Class UseClasses shared Sub Main() Dim BObj As BaseCls = New DervCls() Dim DObj As DervCls = New DervCls() WriteLine("Accessed through base class: " & BObj.Z) WriteLine("Accessed through derived class: " & DObj.Z) End Sub End Class

Compunet Corporation12 Inheritance Shadows Example Output: Accessed through base class: 100 Accessed through derived class: *

Compunet Corporation13 Exercise (Inheritance and Variables) Write an application that demonstrates a class inheritance hierarchy. Class M extends Object and has two instance variable of type float and String. Class N extends M and has one instance variable of type Double. Instantiate class N. Initialize and display its variables. Write an application that illustrates how to access a hidden variable. Class G declares a static variable x. Class H extends G and declares an instance variable x. A display() method in H displays both of these variables.

Compunet Corporation14 Method Overriding Method overriding occurs when a class declares a method that has the same type signature as a method declared by one of its Base classes When a method in a subclass override a method in a Base class, the method in the Base class is hidden relative to the subclass object. In Derived class you need to Shadows the method Method overriding is a very important capability because it forms the basis for run-time polymorphism(means one interface, multiple implementation)

Compunet Corporation15 Example: Method Overriding Imports System.Console Class Class1 Sub Method1() WriteLine("Base class method1") End Sub End Class Class Class2 Inherits Class1 ' On next Line Shadows Sub Method1() WriteLine("Sub class method1") End Sub Sub Method2() WriteLine("Sub class method2") End Sub End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() C1.Method1() C2.Method1() C2.Method2() End Sub End Class Output: Base class method1 Sub class method1 Sub class method2

Compunet Corporation16 Abstract Classes Abstract classes cannot be instantiated. A class that is derived from an abstract class may still implement interfaces. An abstract class is denoted in Visual Basic by the keyword MustInherit Abstract methods that are to be implemented are marked in Visual Basic with the MustOverride modifier

Compunet Corporation17 Abstract Classes Public MustInherit Class WashingMachine Sub New() ' Code to instantiate the class goes here. End sub Public MustOverride Sub Wash Public MustOverride Sub Rinse (loadSize as Integer) Public MustOverride Function Spin (speed as Integer) as Long End Class

Compunet Corporation18 Abstract Classes Public Class MyWashingMachine Inherits WashingMachine Public Overrides Sub Wash() ' Wash code goes here End Sub Public Overrides Sub Rinse (loadSize as Integer) ' Rinse code goes here End Sub Public Overrides Function Spin (speed as Integer) as Long ' Spin code goes here End Sub End Class