Professional Documents
Culture Documents
Module II Oops
Module II Oops
Module II Oops
4. Multiple Inheritance
Problem:
Create a class Shape has attribute length and readLength() method to read the length.
Create a sub class Rectangle which inherited from Shape class. Rectangle class has
attribute width and readWidth() method to read the width. It also have another method
area() to calculate area of a rectangle. Write a suitable test Java program.
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Single level Inheritance:
class Shape { class TestDemo{
double length; public static void main(String[] args) {
void readLength(double l) Rectangle r=new Rectangle();
{
r.readLength(5);
length=l; r.readWidth(6);
}} r.area();
class Rectangle extends Shape { }
double width; }
void readWidth(double w)
{
width=w;
}
void area()
{
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Multi-level Inheritance
• A new class is derived from already derived class.
class Parent{ class Grandson extends Son {
int p_property=10; int gs_property=30;
void showP() void showGS()
{ {
System.out.println("Parent property System.out.println("Grandson property is="+
is="+p_property); (p_property+s_property+gs_property));
}} }}
Problem:
Create a class Shape has attribute length and readLength() method to read the length.
Create a sub class Rectangle which inherited from Shape class. Rectangle class has
attribute width and readWidth() method to read the width. It also have another method
area() to calculate area of a rectangle. Create another subclass Square which inherited
from the Shape class. Square class should have method area() to calculate area of a
square. Write a suitable test Java program.
Create a class Vehicle has fields manufacturer’s name, year made, color
and readData() method to read the field values. Create a sub class Car
which is inherited from Vehicle class. Car class has one attribute
number of seats and setData() method to read the number of seats. It
also has another method display () to display the car information.
Create another subclass Bus with one attribute number of seats and
setData() method to read the number of seats. It also has another
method display () to display the Bus information. Write a suitable test
Java program.
class A {
int x=20; class SuperEx{
void show() public static void main(String[] args) {
{ B ob=new B();
System.out.println("x in super class is="+x); ob.show();
} }
} }
class B extends A {
int x=30;
void show()
{
System.out.println("x in sub class is="+x);
System.out.println("x in super class is="+super.x);
super.show();
}}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
super keyword
class Shape {
double length,width;
Shape(double l,double w) class SuperEx2{
{ public static void main(String[] args) {
length=l; Rectangle r=new Rectangle();
width=w; r.area();
}} }
class Rectangle extends Shape { }
Rectangle()
{
super(5,6);
}
void area()
{
System.out.println("Area of Rectangle is="+(length*width));
}}
A boy has his money deposited $1000, $1500 and $2000 in banks-Bank A,
Bank B and Bank C respectively. We have to print the money deposited by
him in a particular bank.
Create a class 'Bank' with a method 'getBalance()' which returns 0. Make
its three subclasses named 'BankA', 'BankB' and 'BankC' with a method
with the same name 'getBalance()' which returns the amount deposited in
that particular bank. Call the overridden method 'getBalance()' by the
object of each of the three banks.
} }
}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
final keyword
• final keyword is used to modifies the accessibility of a variable, method and class.
Final variables: When a variable is declared as final, its value cannot be changed once it has
been initialized.
Ex: final double pi=3.14;
Final methods: When a method is declared as final, it cannot be overridden by a subclass.
Final classes: When a class is declared as final, it cannot be extended by a subclass.
• An abstract class does not allow you to create objects of its type.
• The child classes which inherit the abstract class must provide the implementation of
these inherited abstract methods.
• If abstract methods are not implemented in its sub classes then you should declare those
sub class as abstract.
• An abstract class can have parameterized constructors.
We have to calculate the area of a rectangle, and a triangle. Create an abstract class
'Shape' with two fields dim1 and dim2 and abstract method area(). Include a constructor to
initialize dim1 and dim2. Create two child classes of Shape: Rectangle and Triangle. The
constructor of both the sub classes takes dim1 and dim2 values as its parameters. Create
an object for each of the two classes and print the area.
Create an abstract class named Book. Include a String field for the book’s title and a double
field for the book’s price. Within the class, include a constructor that requires the book title,
and add two get methods—one that returns the title and one that returns the price. Include
an abstract method named setPrice(). Create two child classes of Book: Fiction and
NonFiction. Each must include a setPrice() method that sets the price for all Fiction Books
to $24.99 and for all NonFiction Books to $37.99. Write a constructor for each subclass,
and include a call to setPrice() within each. Write a class demonstrating that you can create
both a Fiction and a NonFiction Book, and display their fields.
gc() method:
• The gc() method is used to invoke the garbage collector to perform cleanup processing.
The gc() is found in System class.
finalize() method:
• The finalize() method is invoked each time before the object is garbage collected.
• This method can be used to perform cleanup processing.
• This method is defined in Object class
protected void finalize(){}
ob=null;
System.gc();
}
}
O/P:
object is garbage collected
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
The Object class
• The Object class is the parent class of all the classes in java by default.
• All other classes are subclasses of Object. That is, Object is a superclass of all other
classes.
class Outer{
int x = 10; public class Main {
public static void main(String[] args) {
class Inner { Outer Out= new Outer();
int y = 5; Outer. Inner In = Out. new Inner();
void add() In.add();
{
System.out.println(x+y); }
} }
}
}
O/P: 15
class Outer{
public class Demo {
int x = 10;
public static void main(String[] args) {
Outer Out= new Outer();
private class Inner {
Outer. Inner In = Out. new Inner();
int y = 5;
In.add();
void add()
{
}
System.out.println(x+y);
}
}
} Error: Outer.Inner has private access in Outer
} Outer.Inner In = Out.new Inner();
O/P: 5