Inheritance in Java-Java Programminng-Lab Lecture, Lecture notes of Java Programming

This lecture was delivered by Prof. Mudita Tiwari at Cochin University of Science and Technology for Java Programming course. It includes: Inheritance, Java, Access, Specifiers, Class, Package, Software, Developer, Parent, Child

Typology: Lecture notes

2011/2012

Uploaded on 07/19/2012

rohit-sharma
rohit-sharma 🇮🇳

4.3

(11)

200 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Inheritance in Java
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Inheritance in Java-Java Programminng-Lab Lecture and more Lecture notes Java Programming in PDF only on Docsity!

Inheritance in Java

Access Specifiers

 private keyword  Used for most instance variables  private variables and methods are accessible only to methods of the class in which they are declared  Declaring instance variables private is known as data hiding  public keyword  Exposes variables or methods outside the Class.  Declaring public methods is know as defining the class’ public interface.  Protected keyword  Provides limited access  Provides access to sub classes but not to the classes outside a package

Inheritance

 Inheritance allows a software developer to reuse classes by

deriving a new class from an existing one

 The existing class is called the parent class, or superclass, or

base class

 The derived class is called the child class or subclass.

 As the name implies, the child inherits characteristics of the

parent

 That is, the child class inherits the methods and data defined

for the parent class

Inheritance Relationship

 Inheritance should create an is-a relationship , meaning the

child is a more specific version of the parent

Class Hierarchy

 Good class design puts all common features as high in

the hierarchy as reasonable

 inheritance is transitive

 An instance of class Parrot is also an instance of Bird, an instance of Animal, …, and an instance of class Object

Class inheritance

 The inclusion of members of a base class in a derived class so that they are accessible in that derived class is called class inheritance.

 An inherited member of a base class is one that is accessible within the derived class.

 If a base class member is not accessible in a derived class, then it is not an inherited member of the derived class.

Inheriting Data Members

Hidden Data Members

 Variables of same name both in base class and derived

class

 Use of keyword super

Example

// A simple example of inheritance. // Create a superclass. class A { int i, j; void showij() { System.out.println("i and j: " + i + " " + j); } } // Create a subclass by extending class A. class B extends A { int k; void showk() { System.out.println("k: " + k); } void sum() { System.out.println("i+j+k: " + (i+j+k)); } }

class SimpleInheritance {

public static void main(String args[]) {

A superOb = new A();

B subOb = new B();

// The superclass may be used by itself.

superOb.i = 10;

superOb.j = 20;

System.out.println("Contents of superOb: ");

superOb.showij();

System.out.println();

OUTPUT

 The output from this program is shown here:

 Contents of superOb:

 i and j: 10 20

 Contents of subOb:

 i and j: 7 8

 k: 9

 Sum of i, j and k in subOb:

 i+j+k: 24

Example

public class Animal {

public Animal(String aType) {

type = new String(aType);

}

public String toString() {

return “This is a “ + type;

}

private String type;

}

Constructors of the Derived Class

public class Dog extends Animal { public Dog(String aName) { super(“Dog”); name = aName; breed = “Unknown”; } public Dog(String aName, String aBreed) { super(“Dog”); name = aName; breed = aBreed; } private String name; private String breed; }

Overriding a Base Class Method

public String toString() {

return “It’s “ + name + “ the “ + breed;