Method Overriding:-
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider the following:
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
The output produced by this program is shown here:
k: 3
When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the versio n of show( ) inside B overrides the version declared in A. If you wish to access the superclass version of an overridden function, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass' version. This allows all instance variables to be displayed.
Next
// Methods with differing type signatures are overloaded – not
// overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
The output produced by this program is shown here:
This is k: 3
i and j: 1 2
The version of show( ) in B takes a string parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.
Access specifier:-
protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else. You use the protected access level when it is appropriate for a class's subclasses to have access to the method or field, but not for unrelated classes.
package Test;
class A
{
protected int i,j;
void setijA()
{
i = 10;
j = 20;
}
}
class B
{
void showA()
{
System.out.println("I : " + i);
System.out.println("J : " + j);
}
}
class DemoProtected
{
public static void main(String args[])
{
B ObjectB = new B();
ObjectB.showA();
}
}
The Protected Modifier:-
The protected visibility modifier allows a member of a base class to be accessed in the child
protected visibility provides more encapsulation than public does
protected visibility is not as tightly encapsulated as private visibility
All these methods can access the pages instance variable.
Note that by constructor chaining rules, pages is an instance variable of every object of class Dictionary.
Class Hierarchy
A child class of one parent can be the parent of another child, forming class hierarchies
At the top of the hierarchy there’s a default class called Object.
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 class hierarchy determines how methods are executed:
Previously, we took the simplified view that when variable v is an instance of class C, then a procedure call v.proc1() invokes the method proc1() defined in class C
However, if C is a child of some superclass C’ (and hence v is both an instance of C and an instance of C’), the picture becomes more complex, because methods of class C can override the methods of class C’
super Keyword
The keyword super is used to refer to the direct superclass of a class.
The keyword can be used to explicitly invoke the superclass methods and constructors.
To explicitly invoke a constructor of the superclass, the statement: super(args) is used. This statement invokes the superclass constructor, passing it the values to be initialized. The superclass constructor then initialized the values of the inherited data fields for the current object.
The calls to the superclass constructor must be the first statement in the method body. If it is omitted, the compiler will implicitly invoke the constructor by inserting a call to the constructor that has no parameters (default constructor).
The prefix “super. “ can also be used to call a method of the superclass. For example, super.toString() invokes the toString() method of the superclass
What is inheritance? How can you make constructor in class hierarchy? Explain with example.[2060]Ans:
Inheritance in JAVA programming is the process by which one class takes the property of another class. The new classes, known as derived classes or subclasses, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super classes.
It helps the programmer to re-use the class just by modifying minor objects and methods in it.
For class inheritance, Java uses the keyword extends and for interface inheritance Java uses the keyword implements.
Syntax:
Class ABC
{
…
}
Class derived class extends ABC
{
…
…
}
What is abstract base class? Explain it with an example.[2065]
Ans:
An abstract class is a class that leaves one or more method implementations unspecified by declaring one or more methods abstract. An abstract method has no body (i.e., no implementation). A subclass is required to override the abstract method and provide an implementation. Hence, an abstract class is incomplete and cannot be instantiated, but can be used as a base class.
Example:-
abstract class A
{
abstract void callme();
void callmetoo() {
System.out.println(“This is a concerte method”);
}
}
class B extends A {
void call me(){
System.out.println(“B’s implementation of callme.”);
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b =new B();
b.callme();
b.callmetoo();
}}
Notice that no object of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo(). This is perfectly acceptable.
Why is inheritance used in java? Describe the different types of classes in java class hierarchy with an example that create multilevel hierarchy. [2064]
Ans:In Java, inheritance is used for two purposes:
1. Class inheritance - create a new class as an extension of another class, primarily for the purpose of code reuse. That is, the derived class inherits the public methods and public data of the base class. Java only allows a class to have one immediate base class, i.e., single class inheritance.
2. Interface inheritance - create a new class to implement the methods defined as part of an interface for the purpose of sub typing. That is a class that implements an interface “conforms to” (or is constrained by the type of) the interface. Java supports multiple interface inheritance.
What are the uses of super in Java? Explain with an example. [2064]
Ans:
The keyword super is used to access methods/variables of the parent class.
ex:
public class A {
public String getName(){
.....
}
}
public class B extends A {
public String getName(){
.....
}
}
If you want to access the method getName of class A inside class B you cannot do directly because the compiler would get confused. you need not create an object of A since it is already available. At this point you can use super super.getName() is enough to call the method of the parent class
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider the following:
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
The output produced by this program is shown here:
k: 3
When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the versio n of show( ) inside B overrides the version declared in A. If you wish to access the superclass version of an overridden function, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass' version. This allows all instance variables to be displayed.
Next
// Methods with differing type signatures are overloaded – not
// overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
The output produced by this program is shown here:
This is k: 3
i and j: 1 2
The version of show( ) in B takes a string parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.
Access specifier:-
protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else. You use the protected access level when it is appropriate for a class's subclasses to have access to the method or field, but not for unrelated classes.
package Test;
class A
{
protected int i,j;
void setijA()
{
i = 10;
j = 20;
}
}
class B
{
void showA()
{
System.out.println("I : " + i);
System.out.println("J : " + j);
}
}
class DemoProtected
{
public static void main(String args[])
{
B ObjectB = new B();
ObjectB.showA();
}
}
The Protected Modifier:-
The protected visibility modifier allows a member of a base class to be accessed in the child
protected visibility provides more encapsulation than public does
protected visibility is not as tightly encapsulated as private visibility
All these methods can access the pages instance variable.
Note that by constructor chaining rules, pages is an instance variable of every object of class Dictionary.
Class Hierarchy
A child class of one parent can be the parent of another child, forming class hierarchies
At the top of the hierarchy there’s a default class called Object.
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 class hierarchy determines how methods are executed:
Previously, we took the simplified view that when variable v is an instance of class C, then a procedure call v.proc1() invokes the method proc1() defined in class C
However, if C is a child of some superclass C’ (and hence v is both an instance of C and an instance of C’), the picture becomes more complex, because methods of class C can override the methods of class C’
super Keyword
The keyword super is used to refer to the direct superclass of a class.
The keyword can be used to explicitly invoke the superclass methods and constructors.
To explicitly invoke a constructor of the superclass, the statement: super(args) is used. This statement invokes the superclass constructor, passing it the values to be initialized. The superclass constructor then initialized the values of the inherited data fields for the current object.
The calls to the superclass constructor must be the first statement in the method body. If it is omitted, the compiler will implicitly invoke the constructor by inserting a call to the constructor that has no parameters (default constructor).
The prefix “super. “ can also be used to call a method of the superclass. For example, super.toString() invokes the toString() method of the superclass
What is inheritance? How can you make constructor in class hierarchy? Explain with example.[2060]Ans:
Inheritance in JAVA programming is the process by which one class takes the property of another class. The new classes, known as derived classes or subclasses, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super classes.
It helps the programmer to re-use the class just by modifying minor objects and methods in it.
For class inheritance, Java uses the keyword extends and for interface inheritance Java uses the keyword implements.
Syntax:
Class ABC
{
…
}
Class derived class extends ABC
{
…
…
}
What is abstract base class? Explain it with an example.[2065]
Ans:
An abstract class is a class that leaves one or more method implementations unspecified by declaring one or more methods abstract. An abstract method has no body (i.e., no implementation). A subclass is required to override the abstract method and provide an implementation. Hence, an abstract class is incomplete and cannot be instantiated, but can be used as a base class.
Example:-
abstract class A
{
abstract void callme();
void callmetoo() {
System.out.println(“This is a concerte method”);
}
}
class B extends A {
void call me(){
System.out.println(“B’s implementation of callme.”);
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b =new B();
b.callme();
b.callmetoo();
}}
Notice that no object of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo(). This is perfectly acceptable.
Why is inheritance used in java? Describe the different types of classes in java class hierarchy with an example that create multilevel hierarchy. [2064]
Ans:In Java, inheritance is used for two purposes:
1. Class inheritance - create a new class as an extension of another class, primarily for the purpose of code reuse. That is, the derived class inherits the public methods and public data of the base class. Java only allows a class to have one immediate base class, i.e., single class inheritance.
2. Interface inheritance - create a new class to implement the methods defined as part of an interface for the purpose of sub typing. That is a class that implements an interface “conforms to” (or is constrained by the type of) the interface. Java supports multiple interface inheritance.
What are the uses of super in Java? Explain with an example. [2064]
Ans:
The keyword super is used to access methods/variables of the parent class.
ex:
public class A {
public String getName(){
.....
}
}
public class B extends A {
public String getName(){
.....
}
}
If you want to access the method getName of class A inside class B you cannot do directly because the compiler would get confused. you need not create an object of A since it is already available. At this point you can use super super.getName() is enough to call the method of the parent class