Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Chapter Four Object Oriented Programming with Java

Chapter Three
Inheritance
- In inheritance, a new class is defined by means of an older, pre-existing class.
This leads to a situation in which, the new class has all the functionality of the
older, pre-existing class and, additionally, introduces its own specific
functionality. We say the new class inherits the functionality of another existing
class.
- While speaking of inheritance, the existing class is called the superclass (base
class) and the new class is called subclass (derived class).

Concept of Inheritance
- One advantage of OOP is the re-usage of code. The capability to define custom
data types using classes enables us to reuse the code that we develop. This works
well if we always need exactly the same object. In real world, however, we may
need an object that is almost similar to an already developed object but not
exactly similar. Inheritance enables us to reuse an object more quickly; thus
making slight adjustments where necessary.

Superclasses and subclasses


- Often, an object of one class “is an” object of another class as well. For instance,
in geometry, a rectangle is a quadrilateral. Thus, in Java, class rectangle can be
said to inherit from class Quadrilateral. Because every subclass object “is an”
object of its superclass, and one superclass can have many subclasses, the set of
objects represented by a superclass typically is larger than the set of objects
represented by any of its subclasses. For example, the superclass vehicle
represents all vehicles, including cars, trucks, boats and so on. By contrast,
subclass car represents a smaller, more specific subset of all vehicles.
- inheritance relationship forms tree-like hierarchical structure

Shape

TwoDimensionalShap ThreeDimensionalShape

Circle Square Triangl Sphere Cube Prism

1
Chapter Four Object Oriented Programming with Java

- to specify that class TwoDimensionalShape extends (or inherits from) class


Shape, class TwoDimensionalShape could be declared in Java as follows:
public class TwoDimensionalShape extends Shape

- Objects of all classes that extend a common superclass can be treated as objects
of that superclass. (i.e., such objects have an “is-a” relationship with the
superclass). However, superclass objects can’t be treated as objects of their
subclasses. For example, all cars are vehicles, but not all vehicles are cars.

Protected Members
- A superclass’s protected members can be accessed by members of that superclass,
by members of any class derived from that superclass and by members of other
classes in the same package. (protected members have package access).

Overriding a method
- If a class declares an instance method, then the declaration of that method is said
to override any and all methods with the same signature in the superclass.
- If a method declaration overrides the declaration of another method, then a
compile-time error occurs if they have different return types or if one has a
return type and the other is void.
- The access modifier of the overriding method must provide at least as much
access as the overridden method, or a compile-time error occurs. on more detail:
If the overridden method is public, then the overriding method must be public;
otherwise, a compile-time error occurs.
If the overridden method is protected, then the overriding method must be
protected or public, otherwise, a compile-time error occurs.
If the overridden method has default (package) access, the overriding method
must not be private; otherwise, a compile-time error occurs.

- Notice that a private method is never accessible and so can’t be overridden in


the technical sense of these terms.

Using this() and super()


- Because you frequently want to call the superclass’s constructor explicitly, there
is a keyword in Java that makes this possible. super() will call the parent’s
constructor which has the appropriate supplied parameters. It is also possible to
have more than one constructor for a class (which was discussed in the
overloading section). If you are creating more than one constructor, you
typically do not want to duplicate the common code. So you can call a same
class constructor using this() keyword, sending any required parameters.
- The keyword super() has two general forms. The first is call of superclass
constructor. The second is used to access a member of the superclass that has
been hidden by a member of the subclass. let us see each use of the super
keyword one by one:

public class Student{

2
Chapter Four Object Oriented Programming with Java

private String name;


private int age;

public Student(String name,int age)


{
this.name = name;
this.age = age;
}

public void printData()


{
System.out.println(“Name->”+name);
System.out.println(“Age->”+age);
}
}//end class

public class MakeStudent extends Student{


int roll;
double avgMark;
public MakeStudent(String name,int age,int roll, double avgMark)
{
super(name,age);//calling superclass constructor
this.roll=roll;
this.avgMark=avgMark;
}

public void printMark()


{
System.out.println(“Roll No->”+roll);
System.out.println(“Average Mark->”+avgMark);
}
}//end class

public class TestStudent{


public static void main(String args[])
{
MakeStudent clsStud = new MakeStudent(“Ruth Leta”,15,100,65);
clsStud.printData();
clsStud.printMark();
}
}//end class

- The second form of super keyword is some what similar to that of this pointer.
The difference is, super() always refers to the superclass of the subclass in which
it is used. this usage has the following general form:
super.member;

3
Chapter Four Object Oriented Programming with Java

- Here, member can either be a method or an instance variable. This second form of
super() is most applicable to situations in which member names of a subclass hide
members by the same name in the superclass. Consider the following example.

public class ClassA{


int i;
}//end class

public class ClassB extends ClassA{


int i;//this i hides the i in class ClassA

public ClassB(int a,int b)


{
super.i = a;//i in ClassA
i = b;//i in ClassB
}

public void show()


{
System.out.println(“i in superclass : ”+super.i);
System.out.println(“i in subclass : ”+i);
}

}//end class

public class TestSuper{


public static void main(String args[])
{
ClassB clsB = new ClassB(10,20);
clsB.show();
}
}//end class

- Although the instance variable i in ClassB hides the i in ClassA, super allows
access to the i defined in the superclass. Super can also be used to call methods
that are hidden by subclasses.

Use of final with Inheritance


- A class can be declared final if its definition is complete and no subclasses are
desired or required. A compile-time error occurs if the name of a final class
appears in the extends clause of another class declaration; this implies that a
final class can’t have any subclasses.
- 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. Both are
examined here:

4
Chapter Four Object Oriented Programming with Java

- Final keyword prevents overriding. To disallow a method from being overridden,


specify final as a modifier at the start of its declaration. Methods declared as final
can’t be overridden. Have a look at the following example.

public class ClassA{


public final void firstMethod()
{
System.out.println(“this is a final method declared only once”);
}
}//end class

public class ClassB extends ClassA{


public void firstMethod()
{
System.out.println(“illegal to declare this method again”);
}
}//end class

- Final prevents inheritance. Sometimes, you will want to prevent a class from
being inherited. To do this, precede the class declaration with the keyword final.
Declaring a class as final implicitly declares all of its methods as final too.
Consider the following example.

public final clas


ClassA{ //…
}//end class

public class ClassB extends ClassA{//illegal


inheritance }//end class

Constructors in subclasses
- Instantiating a subclass object begins a chain of constructor calls in which the
subclass constructor, before performing its own tasks, invokes its direct
superclasse’s constructor either explicitly (via the super reference) or implicitly
(calling the superclasse’s default constructor or no-argument constructor).
Similarly, if the superclass was derived from another class, the superclass
constructor would be required to invoke the constructor of the next class up in the
hierarchy, and so on. The last constructor called in the chain is always the
constructor of class Object. The original subclass constructor’s body finishes
executing last.

You might also like