




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































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
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
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.
Variables of same name both in base class and derived
class
Use of keyword super
// 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
public class Animal {
public Animal(String aType) {
type = new String(aType);
}
public String toString() {
return “This is a “ + type;
}
private String type;
}
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; }