INHERITANCE BASICS Reusability is achieved by INHERITANCE

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Object Oriented Programming with Java
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Chapter 1 Inheritance University Of Ha’il.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Web Application Development Slides Credit Umair Javed LUMS.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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 Fundamental Data Types Lesson - 2.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CSC 142 Computer Science II Zhen Jiang West Chester University
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Topics Inheritance introduction
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
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)
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
BY:- TOPS Technologies
Modern Programming Tools And Techniques-I
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
2.7 Inheritance Types of inheritance
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
INHERITANCE IN JAVA.
Inheritance.
Week 8 Lecture -3 Inheritance and Polymorphism
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Interface.
Java Programming Language
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Programming, Second Edition
Inheritance:Concept of Re-usability
OOP Aga private institute for computer science 5th grade
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

INHERITANCE BASICS Reusability is achieved by INHERITANCE Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes. The class whose properties are extended is known as super or base or parent class. The class which extends the properties of super class is known as sub or derived or child class A class can either extends another class or can implement an interface

class B implements A { ….. } A interface B sub class class B extends A { ….. } A super class B sub class B <<class>> A <<interface>> class B implements A { ….. } A interface B sub class B <<class>>

Various Forms of Inheritance Single Inheritance Hierarchical Inheritance A A X X B B A B C A B C NOT SUPPORTED BY JAVA MultiLevel Inheritance Multiple Inheritance SUPPORTED BY JAVA A A A B A B B B C C C C

Forms of Inheritance WRONG WRONG Mulitiple Inheritance can be implemented by implementing multiple interfaces not by extending multiple classes Example : class Z extends A implements C , D { …………} OK class Z extends A ,B class Z extends A extends B { { OR } } A C D Z WRONG WRONG

Defining a Subclass Syntax : class <subclass name> extends <superclass name> { variable declarations; method declarations; } Extends keyword signifies that properties of the super class are extended to sub class Sub class will not inherit private members of super class

Access Control public protected friendly private Yes No Access Modifiers Access Location public protected friendly private Same Class Yes sub classes in same package No Other Classes in Same package Subclasses in other packages Non-subclasses in other packages

Inheritance Basics Whenever a sub class object is created ,super class constructor is called first. If super class constructor does not have any constructor of its own OR has an unparametrized constructor then it is automatically called by Java Run Time by using call super() If a super class has a parameterized constructor then it is the responsibility of the sub class constructor to call the super class constructor by call super(<parameters required by super class>) 4. Call to super class constructor must be the first statement in sub class constructor

When super class has a Unparametrized constructor Inheritance Basics When super class has a Unparametrized constructor class A { A() System.out.println("This is constructor of class A"); } } // End of class A class B extends A B() super(); System.out.println("This is constructor of class B"); } // End of class B Optional Cont…..

This is constructor of class A This is constructor of class B class inhtest { public static void main(String args[]) B b1 = new B(); } OUTPUT This is constructor of class A This is constructor of class B

File Name is xyz.java class A { A() System.out.println("This is class A"); } class B extends A B() System.out.println("This is class B"); class inherit1 public static void main(String args[]) B b1 = new B(); File Name is xyz.java /* E:\Java>java inherit1 This is class A This is class B E:\Java> */

class A { private A() System.out.println("This is class A"); } class B extends A B() System.out.println("This is class B"); class inherit2 public static void main(String args[]) B b1 = new B(); /* E:\Java>javac xyz1.java xyz1.java:12: A() has private access in A { ^ 1 error */

E:\Java>javac xyz2.java xyz2.java:7: A() is already defined in A class A { private A() System.out.println("This is class A"); } A() class B extends A B() System.out.println("This is class B"); class inherit2 public static void main(String args[]) B b1 = new B(); } } /* E:\Java>javac xyz2.java xyz2.java:7: A() is already defined in A A() ^ xyz2.java:16: A() has private access in A { 2 errors */

When Super class has a parametrized constructor. class A { private int a; A( int a) this.a =a; System.out.println("This is constructor of class A"); } class B extends A private int b; private double c; B(int b,double c) this.b=b; this.c=c; System.out.println("This is constructor of class B"); B b1 = new B(10,8.6); D:\java\bin>javac inhtest.java inhtest.java:15: cannot find symbol symbol : constructor A() location: class A { ^ 1 errors

This is constructor of class A This is constructor of class B { private int a; A( int a) this.a =a; System.out.println("This is constructor of class A"); } } class B extends A private int b; private double c; B(int a,int b,double c) super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); B b1 = new B(8,10,8.6); OUTPUT This is constructor of class A This is constructor of class B

Call to print() from super class A class B extends A { int b; double c; B(int a,String n,int b,double c) super(a,n); this.b=b; this.c =c; } void show() //System.out.println("a="+a); print(); System.out.println("name="+name); System.out.println("b="+b); System.out.println("c="+c); class A { private int a; protected String name; A(int a, String n) this.a = a; this.name = n; } void print() System.out.println("a="+a); a is private in class A Call to print() from super class A Accessing name field from super class (super.name)

class xyz3 { public static void main(String args[]) B b1 = new B(10,"OOP",8,10.56); b1.show(); } E:\Java>java xyz3 a=10 name=OOP b=8 c=10.56

USE OF super KEYWORD Can be used to call super class constrctor super(<parameter-list>); Can refer to super class instance variables/Methods super.<super class instance variable/Method>

class A { private int a; A( int a) this.a =a; System.out.println("This is constructor of class A"); } void print() System.out.println("a="+a); void display() System.out.println("hello This is Display in A"); } // End of class A class B extends A { private int b; private double c; B(int a,int b,double c) super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } void show() print(); System.out.println("b="+b); System.out.println("c="+c); } // End of class B

class inhtest1 { public static void main(String args[]) B b1 = new B(10,8,4.5); b1.show(); } /* OutPUt D:\java\bin>java inhtest1 This is constructor of class A This is constructor of class B a=10 b=8 c=4.5 */

class B extends A { private int b; private double c; B(int a,int b,double c) super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } void show() super.show(); System.out.println("b="+b); System.out.println("c="+c); display(); class A { private int a; A( int a) this.a =a; System.out.println("This is constructor of class A"); } void show() System.out.println("a="+a); void display() System.out.println("hello This is Display in A");

class inhtest1 { public static void main(String args[]) B b1 = new B(10,8,4.5); b1.show(); } /* OutPut D:\java\bin>java inhtest1 This is constructor of class A This is constructor of class B a=10 b=8 c=4.5 hello This is Display in A */

class A { int a; A( int a) { this.a =a; } void show() System.out.println("a="+a); } void display() System.out.println("hello This is Display in A"); class B extends A { int b; double c; B(int a,int b,double c) super(a); this.b=b; this.c=c; } void show() //super.show(); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c);

class inhtest2 { public static void main(String args[]) B b1 = new B(10,20,8.4); b1.show(); } /* D:\java\bin>java inhtest2 a=10 b=20 c=8.4 */

class B extends A { // super class variable a hides here int a; int b; double c; B(int a,int b,double c) super(100); this.a = a; this.b=b; this.c=c; } void show() System.out.println("Super class a="+super.a); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); class A { int a; A( int a) { this.a =a; } }

class inhtest2 { public static void main(String args[]) B b1 = new B(10,20,8.4); b1.show(); } /* Out Put D:\java\bin>java inhtest2 Super class a=100 a=10 b=20 c=8.4 */