Amar Jukuntla, M.Tech.,
Assistant Professor,
Department Computer Science and Engineering
VFSTR Deemed to Be University,
Vadlamudi
1
§ Inheritance-Basics
§ Member Access Rules
§ Super
§ Final
§ The Object Class
§ Forms of inheritance
§ Benefits of Inheritance
§ Cost of Inheritance
§ Polymorphism
§ Method Overriding
§ Abstract Classes
2
§ Inheritance is the process of deriving a new class based on already
existing class.
§ The newly derived class is called “subclass” or “childclass”. The
existing class is called “superclass” or “baseclass” or “parentclass”.
§ A subclass is a specialized version of its superclass. It inherits all of the
instances variables and methods defined by the “superclass” and it
adds its own elements.
§ Inheritance is the process by which one object acquires the properties
of another object.
§ Inheritance allows code defining one class to be used in other class.
§ By using “extend” keyword we can inherit a class from an existing class.
3
§ The general form of a class that inherits a super class is:
class subclass_name extends superclass_name{
//instances
//methods
//variables
//……..
}
4
§ We can specify only one superclass for any subclass that we create.
§ Java does not support the inheritance of “multiple super classes” into a single
subclass.
§ We can create hierarchy of inheritance, which a subclass becomes a superclass of
another subclass.(Hierchial Association).
5
6
Base class
class A
{
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
Subclass
class B extends A {
int total;
void sum() {
total = i + j;
// ERROR, j is not
accessible here
}
}
Accessing
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " +
subOb.total);
}
}
7
§ Public
§ Private
§ Protected
8
9
Declared As
Accessible
Public Protected Private
Class
Other Class in
the Package
Sub Class in the
Package
Sub Class
outside the
package
Other package
§Whenever a subclass needs to refer to its
immediate superclass, it can do so by use of the
keyword super.
§super has two general forms.
§The first calls the superclass’ constructor.
§The second is used to access a member of the superclass
that has been hidden by a member of a subclass.
10
A subclass can call a
constructor defined
by its superclass by
use of the following
form of super:
super(arg-list);
Base Class
class Box{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
}
Sub Class
class BoxWeight extends Box{
double weight;
BoxWeight(double w,double h,double d,double m)
{
super(w,h,d);
weight=m;
}
}
11
The second form of super
acts somewhat like this,
except that it always refers
to the superclass of the
subclass in which it is used.
This usage has the
following general form:
super.member
Here, member can be either
a method or an instance
variable.
12
Base Class
class Box{
double width;
double height;
double depth;
double weight;
Box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
}
Sub Class
class BoxWeight extends Box{
BoxWeight(double w,double h,double d,double m)
{
super(w,h,d);
super.weight=m;
}
}
class Box{
double width, height,depth;
Box()
{ width = 10;height = 10;depth = 10;
}
Box(double w, double h, double d)
{width=w;height=h;depth=d;
}
double vol()
{
return width*height*depth;
}
}
class BoxDemo extends Box
{
BoxDemo()
{//super(10.0,20.0,30.0);}
void display()
{ double w=super.width;double h=super.height;double d=super.depth;
System.out.println("Width:"+w+"nDepth:"+d+"nHeight:"+h+"n");
double vol=super.vol();
System.out.println("Volume of the BOX:"+vol);
}
public static void main(String args[])
{
BoxDemo bd=new BoxDemo();
bd.display();
}
}
13
§ The keyword final has three uses.
§ First, it can be used to create the equivalent of a named constant.
§ The other two uses of final apply to inheritance.
14
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
15
§Declaring a class as final implicitly declares all of its
methods as final, too.
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}
16
§ A subtype is a class that satisfies the principle of substitutability.
§ A subclass is something constructed using inheritance, whether or not it satisfies the principle of substitutability.
§ The two concepts are independent. Not all subclasses are subtypes, and (at least in some languages) you can
construct subtypes that are not subclasses.
§ Substitutability is fundamental to many of the powerful software development techniques in OOP.
§ The idea is that, declared a variable in one type may hold the value of different type.
§ Substitutability can occur through use of inheritance, whether using extends, or using implements keywords.
17
§ Substitutability means, the type of the variable does not have to match with the
type of the value assigned to that variable.
§ Substitutability cannot be achieved in conventional languages in C, but can be
achieved in Object Oriented languages like Java.
§ We have already seen the concept of “Assigning a subclass object to superclass
variable or reference”. This is called substitutability. Here I am substituting the
superclass object with the object of subclass.
18
§ Specialization
§ The child class is a special case of the parent class; in other words, the child class is a subtype of the
parent class.
§ Specification
§ The parent class defines behavior that is implemented in the child class but not in the parent class.
§ Construction
§ The child class makes use of the behavior provided by the parent class, but is not a subtype of the
parent class.
§ Extension
§ The child class adds new functionality to the parent class, but does not change any inherited
behavior.
§ Limitation
§ The child class restricts the use of some of the behavior inherited from the parent class.
§ Combination
§ The child class inherits features from more than one parent class. Although multiple inheritance is not
supported directly by Java, it can be simulated in part by classes that use both inheritance and
implementation of an interface, or implement two or more interfaces
19
§ Software reusability
§ Code sharing
§ Consistency of interface
§ Software components
§ Rapid prototyping
§ Information hiding
20
§ Execution speed
§ Program size
§ Message-passing overhead
§ Program complexity
21
§Polymorphism
§ Ability to change form is known as Polymorphism.
§ Polymorphism means “many forms”.
§ Method overloading, method overriding, interfaces are the example
of Polymorphism.
§ Example:
22
23
§ A class that is declared as abstract is known as abstract class. It needs to be
extended and its method implemented. It cannot be instantiated.
§ Example: abstract class A{}
§ A method that is declared as abstract and does not have implementation is known
as abstract method.
§ Example:abstract void printStatus();//no body and abstract
24
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run()
{
System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}
25
§ There is one special class, Object, defined by Java. All other classes are subclasses
of Object.
26
§ The methods getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final.
You may override the others.
§ However,notice two methods now: equals( ) and toString( ).
§ The equals( ) method compares the contents of two objects. It returns true if the
objects are equivalent, and false otherwise. The precise definition of equality can
vary, depending on the type of objects being compared.
§ The toString( ) method returns a string that contains a description of the object on
which it is called.
27

Unit 2

  • 1.
    Amar Jukuntla, M.Tech., AssistantProfessor, Department Computer Science and Engineering VFSTR Deemed to Be University, Vadlamudi 1
  • 2.
    § Inheritance-Basics § MemberAccess Rules § Super § Final § The Object Class § Forms of inheritance § Benefits of Inheritance § Cost of Inheritance § Polymorphism § Method Overriding § Abstract Classes 2
  • 3.
    § Inheritance isthe process of deriving a new class based on already existing class. § The newly derived class is called “subclass” or “childclass”. The existing class is called “superclass” or “baseclass” or “parentclass”. § A subclass is a specialized version of its superclass. It inherits all of the instances variables and methods defined by the “superclass” and it adds its own elements. § Inheritance is the process by which one object acquires the properties of another object. § Inheritance allows code defining one class to be used in other class. § By using “extend” keyword we can inherit a class from an existing class. 3
  • 4.
    § The generalform of a class that inherits a super class is: class subclass_name extends superclass_name{ //instances //methods //variables //…….. } 4
  • 5.
    § We canspecify only one superclass for any subclass that we create. § Java does not support the inheritance of “multiple super classes” into a single subclass. § We can create hierarchy of inheritance, which a subclass becomes a superclass of another subclass.(Hierchial Association). 5
  • 6.
  • 7.
    Base class class A { inti; // public by default private int j; // private to A void setij(int x, int y) { i = x; j = y; } } Subclass class B extends A { int total; void sum() { total = i + j; // ERROR, j is not accessible here } } Accessing class Access { public static void main(String args[]) { B subOb = new B(); subOb.setij(10, 12); subOb.sum(); System.out.println("Total is " + subOb.total); } } 7
  • 8.
  • 9.
    9 Declared As Accessible Public ProtectedPrivate Class Other Class in the Package Sub Class in the Package Sub Class outside the package Other package
  • 10.
    §Whenever a subclassneeds to refer to its immediate superclass, it can do so by use of the keyword super. §super has two general forms. §The first calls the superclass’ constructor. §The second is used to access a member of the superclass that has been hidden by a member of a subclass. 10
  • 11.
    A subclass cancall a constructor defined by its superclass by use of the following form of super: super(arg-list); Base Class class Box{ double width; double height; double depth; Box(double w, double h, double d) { width=w; height=h; depth=d; } } Sub Class class BoxWeight extends Box{ double weight; BoxWeight(double w,double h,double d,double m) { super(w,h,d); weight=m; } } 11
  • 12.
    The second formof super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form: super.member Here, member can be either a method or an instance variable. 12 Base Class class Box{ double width; double height; double depth; double weight; Box(double w, double h, double d) { width=w; height=h; depth=d; } } Sub Class class BoxWeight extends Box{ BoxWeight(double w,double h,double d,double m) { super(w,h,d); super.weight=m; } }
  • 13.
    class Box{ double width,height,depth; Box() { width = 10;height = 10;depth = 10; } Box(double w, double h, double d) {width=w;height=h;depth=d; } double vol() { return width*height*depth; } } class BoxDemo extends Box { BoxDemo() {//super(10.0,20.0,30.0);} void display() { double w=super.width;double h=super.height;double d=super.depth; System.out.println("Width:"+w+"nDepth:"+d+"nHeight:"+h+"n"); double vol=super.vol(); System.out.println("Volume of the BOX:"+vol); } public static void main(String args[]) { BoxDemo bd=new BoxDemo(); bd.display(); } } 13
  • 14.
    § The keywordfinal has three uses. § First, it can be used to create the equivalent of a named constant. § The other two uses of final apply to inheritance. 14
  • 15.
    class A { finalvoid meth() { System.out.println("This is a final method."); } } class B extends A { void meth() { // ERROR! Can't override. System.out.println("Illegal!"); } } 15
  • 16.
    §Declaring a classas final implicitly declares all of its methods as final, too. final class A { // ... } // The following class is illegal. class B extends A { // ERROR! Can't subclass A // ... } 16
  • 17.
    § A subtypeis a class that satisfies the principle of substitutability. § A subclass is something constructed using inheritance, whether or not it satisfies the principle of substitutability. § The two concepts are independent. Not all subclasses are subtypes, and (at least in some languages) you can construct subtypes that are not subclasses. § Substitutability is fundamental to many of the powerful software development techniques in OOP. § The idea is that, declared a variable in one type may hold the value of different type. § Substitutability can occur through use of inheritance, whether using extends, or using implements keywords. 17
  • 18.
    § Substitutability means,the type of the variable does not have to match with the type of the value assigned to that variable. § Substitutability cannot be achieved in conventional languages in C, but can be achieved in Object Oriented languages like Java. § We have already seen the concept of “Assigning a subclass object to superclass variable or reference”. This is called substitutability. Here I am substituting the superclass object with the object of subclass. 18
  • 19.
    § Specialization § Thechild class is a special case of the parent class; in other words, the child class is a subtype of the parent class. § Specification § The parent class defines behavior that is implemented in the child class but not in the parent class. § Construction § The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class. § Extension § The child class adds new functionality to the parent class, but does not change any inherited behavior. § Limitation § The child class restricts the use of some of the behavior inherited from the parent class. § Combination § The child class inherits features from more than one parent class. Although multiple inheritance is not supported directly by Java, it can be simulated in part by classes that use both inheritance and implementation of an interface, or implement two or more interfaces 19
  • 20.
    § Software reusability §Code sharing § Consistency of interface § Software components § Rapid prototyping § Information hiding 20
  • 21.
    § Execution speed §Program size § Message-passing overhead § Program complexity 21
  • 22.
    §Polymorphism § Ability tochange form is known as Polymorphism. § Polymorphism means “many forms”. § Method overloading, method overriding, interfaces are the example of Polymorphism. § Example: 22
  • 23.
  • 24.
    § A classthat is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated. § Example: abstract class A{} § A method that is declared as abstract and does not have implementation is known as abstract method. § Example:abstract void printStatus();//no body and abstract 24
  • 25.
    abstract class Bike{ abstractvoid run(); } class Honda4 extends Bike{ void run() { System.out.println("running safely.."); } public static void main(String args[]) { Bike obj = new Honda4(); obj.run(); } } 25
  • 26.
    § There isone special class, Object, defined by Java. All other classes are subclasses of Object. 26
  • 27.
    § The methodsgetClass( ), notify( ), notifyAll( ), and wait( ) are declared as final. You may override the others. § However,notice two methods now: equals( ) and toString( ). § The equals( ) method compares the contents of two objects. It returns true if the objects are equivalent, and false otherwise. The precise definition of equality can vary, depending on the type of objects being compared. § The toString( ) method returns a string that contains a description of the object on which it is called. 27