Chapter 4: Inheritance and Interface:: Page - 1 Prepared by - Prof Ankit Patel (CE, LJIET)
Chapter 4: Inheritance and Interface:: Page - 1 Prepared by - Prof Ankit Patel (CE, LJIET)
If one class can access the properties of another class that is called inheritance.
Inheritance is process by which new classes can be created using old class.
It is concept by which sub classes are created using some properties of super class.
The use of inheritance is that base class data we can reuse in sub class.
Inheritance is referred as “is-a” relationship.
B
Example
class Mclass
{
int a;
String s;
Mclass()
{
a=5;
s="single inheritance";
}
void show_mclass()
{
System.out.println("a= "+a);
System.out.println("s= "+s);
}
}
class Nclass extends Mclass
Page | 1
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
{
double d;
Nclass()
{
d=12.345;
}
void show_nclass()
{
System.out.println("d= "+d);
}
}
class pb1
{
public static void main(String args[])
{
Mclass n1=new Mclass();
Nclass n2=new Nclass();
n1.show_mclass();
n2.show_nclass();
}
}
Output:
a= 5
s= single inheritance
d= 12.345
Page | 2
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Example:
class Mclass
{
int a;
}
class Nclass extends Mclass
{
double b;
}
class Oclass extends Nclass
{
Oclass()
{
a=10;
b=45.678;
}
void show()
{
System.out.println("a= "+a);
System.out.println("b= "+b);
}
}
class pb1
{
public static void main(String args[])
{
Oclass n1=new Oclass();
n1.show();
}
}
Output:
a= 10
b= 45.678
B C
Page | 3
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Example:
class Mclass
{
int a;
}
class Nclass extends Mclass
{
Nclass()
{
a=10;
}
void show_nclass()
{
System.out.println("a= "+a);
}
}
class Oclass extends Mclass
{
Oclass()
{
a=20;
}
void show_oclass()
{
System.out.println("a= "+a);
}
}
class pb1
{
public static void main(String args[])
{
Nclass n1=new Nclass();
Oclass n2=new Oclass();
n1.show_nclass();
n2.show_oclass();
}
}
Output:
a= 10
a=20
Page | 4
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
A B
Page | 5
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
{
Aclass a1=new Aclass();
a1.show();
Bclass b1=new Bclass();
b1.show();
}
}
Output:
in A class
in B class
Page | 6
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
int a=10;
}
class demo extends abc
{
int a=20;
void show()
{
System.out.println(a);
System.out.println(super.a);
}
}
class test
{
public static void main(String s[])
{
demo d1=new demo();
d1.show();
}
}
Output:
20
10
Page | 7
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
class test
{
public static void main(String s[])
{
demo d1=new demo();
d1.show();
}
}
Output:
in abc class
in demo class
Page | 8
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
1. variable
2. method
3. class (Stop inheritance)
The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in the
static block only. We will have detailed learning of these. Let's first learn the basics of final
keyword.
1) Final variable.
If you make any variable as final, you cannot change the value of final variable (It will be
constant).
Example:
class test
{
public static void main(String s[])
{
final int a=10;
a=20; //it will generate error
}
}
Output:
This code will generate error because a is declare as final and final variable value can not be
change.
2) Final method
If you make any method as final, you cannot override it.
Example
class abc
{
final void show()
{
System.out.println("hello");
}
}
class demo extends abc
{
void show()
{
System.out.println("in demo class");
}
}
class test
{
public static void main(String s[])
Page | 9
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
{
demo d1=new demo();
d1.show();
}
}
Output:
This code will generate error because show method in abc class is declared as final method
so that you cannot override it.
Page | 10
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
return-type method_name2(parameter-list);
...
type varname1 = value;
type varname2 = value;
...
}
Hear, access specifier is either public or any. When no access specifier is included, then
default access specifier is public. interface is keyword. name_of_interface can be any valid
identifier.
The methods which are declared in interface have no body. They end with semicolon after the
parameter list. Each class that includes an interface must implements all of the methods .
Variable can be declared inside of interface declarations. They are implicitly final and static,
meaning they can not be change by the implementing class.
Example:
Write a program that illustrates interface inheritance. Interface P is extended by P1 and P2.
Interface P12 inherits from both P1 ans P2.Each interface declares one constant and one
method. classQ implements P12.Instantiate Q and invoke each of its methods. Each method
displays one of the constants.
interface P
{
int c=10;
void display();
}
interface P1 extends P
{
int c1=11;
void display();
}
interface P2 extends P
{
int c2=12;
void display();
}
interface P12 extends P1,P2
{
void display();
}
class Q implements P12
{
Page | 11
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
interface I1
{
void display();
}
interface I2
{
void display();
}
interface I3 extends I1,I2
{
void display();
Page | 12
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
}
interface I4
{
void display();
}
class X implements I3
{
public void display()
{
System.out.println("In class X");
}
}
class W extends X implements I4
{
}
class pb2
{
public static void main(String args[])
{
W w= new W();
w.display();
Output:
In class X
w is the instance of I1
w is the instance of I2
w is the instance of I3
w is the instance of I4
Page | 13
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Page | 14
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
of derived classes will work and then allow the programmers to fill the implementation in the
derived classes.
If a class contain any abstract method then the class is declared as abstract class. An abstract
class is never instantiated. It is used to provide abstraction. Although it does not provide
100% abstraction because it can also have concrete method.
Syntax :
abstract class class_name
{
// body
}
Abstract method
Method that are declared without any body within an abstract class is known as abstract
method. The method body will be defined by its subclass. Abstract method can never be final
and static. Any class that extends an abstract class must implement all the abstract methods
declared by the super class.
Syntax :
abstract return_typefunction_name (); // No definition
Example:Create a class to find out whether the given year is leap year or not. (using abstact
class)
abstract class A
{
int year;
abstract void Find();
}
class B extends A
{
B(int y)
{
year = y;
}
void Find()
{
if((year % 4)==0)
System.out.println(year+" is a leap year");
else
System.out.println(year+" is not a leap year");
}
Page | 15
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Class pb1
{
Public static void main(String s[])
{
int a=Integer.parseInt(s[0]);
B b=new B(a);
b.Find();
}
}
Output:
D:\>java pb1 2012
2012 is a leap year
Example: Describe abstract class called Shape which has three subclasses say Triangle,
Rectangle and Circle. Define one abstract method area()in the abstract class and override this
area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass
should calculate area of triangle etc. Same for Rectangle and Circle.
abstract class Shape
{
abstract void Area();
}
}
class Triangle extends Shape
{ int b,h;
Triangle(int B,int H)
{
b=B;
h=H;
}
void Area()
Page | 16
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
{
System.out.println("Area of Triangle="+0.5*h*b);
}
}
class Rectangle extends Shape
{ int b,l;
Rectangle(int L,int B)
{
l=L;
b=B;
}
void Area()
{
System.out.println("Area of Rectangle="+l*b);
}
}
class pb6
{
public static void main(String args[])
{
Page | 17
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
By default all methods are abstract. The default method is non abstract method
By default all variable are public, final and Abstract class can have final, non-final, static
static. and non-static variables.
interface cannot have constructor. Abstract class can have constructor.
interface can't provide the implementation of its Abstract class can provide implementation of
methods. its methods.
interface can't have static methods. Abstract class can have static methods.
Example: Example:
public interface shape public abstract class shape
{ {
void area(); abstract void area();
} }
4.12Understanding of System.out.println()
System.out.println("Hello World");
1)System:
It is the name of standard class that contains objects that encapsulates the standard I/O
devices of your system. This class has a final modifier, which means that, it cannot be
inherited by other classes.
It is contained in the package java.lang. Since java.lang package is imported in every java
program by default, therefore java.lang package is the only package in Java API which does
not require an import declaration.
2)out:
Page | 18
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
The object out represents output stream(i.e Command window)and is the static data member
of the class system.
So note here System.out (System -Class & out- static object i.e why its simply refered by
classname and we need not to create any object). static fields and methods must be accessed
by using the class name, so ( System.out ).
3).println():
The println() is method of out object that takes the text string as an argument and displays it
to the standard output i.eon monitor screen.
println() is a public method in PrintStream class to print the data values. Hence to access a
method in PrintStream class, we use out.println() (as non static methods and fields can only
be accessed by using the refrencevarialble)
Note
System -Class
out -static Object
println() –method
Conceptually the System class looks like this:
class System
{
public static final PrintStream out;
//…
}
// the printstream class belongs to java.io package
class PrintStream {
public void println();
//…
}
4.13 Explain Cosmic superclass and its methods.
The Object class is the ultimate ancestor—every class in Java extends Object. However, you
never have to write ―class Employee extends Object‖.
In Java, every class that is defined without an explicit extends clause automatically extends
the class
Object. That is, the class Object is the direct or indirect superclass of every class in Java.
The Object class is the parent class of all the classes in java bydefault. In other words, it is
the topmost class of java.
The Object class is beneficial if you want to refer any object whose type you don't know.
Notice that parent/super class reference variable can refer the child class object, know as
upcasting.
Method Description
returns the Class class object of this object. The
public final Class getClass() Class class can further be used to get the metadata
of this class.
public int hashCode() returns the hashcode number for this object.
public boolean equals(Object obj) compares the given object to this object.
Page | 19
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
protected Object clone() throws creates and returns the exact copy (clone) of this
CloneNotSupportedException object.
public String toString() returns the string representation of this object.
Page | 20
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Page | 21
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Method Description
void add(int index, E element) It is used to insert the specified element at the
specified position in a list.
protected void removeRange(int It is used to remove all the elements lies within
fromIndex, int toIndex) the given range.
Page | 22
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Example:
import java.util.*;
class ArrayList1
{
public static void main(String args[])
{
ArrayList list=new ArrayList();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Invoking arraylist object
System.out.println(list);
System.out.println("inserting elements in the array between");
list.add(2,45);
System.out.println(list);
System.out.println("array size="+list.size());
}
}
In above program,
1) We have created array list of String we can also pass some integer elements to array list.
2) As we insert the elements it grows and as we remove the elements it gets shrunk.
4.16 BigInteger and BigDecimal class
The BigInteger and BigDecimal are the classes that are used to represent the integer
or decimal number of any size and any precision.
Both these classes are use from java.math package
Both the classes are immutable
The BigInteger class allows representation of and calculations on arbitrarily large
integer (whole number).
The BigDecimal class allows precise representation of any real number that can be
with arbitrary precision.
Example of BigInteger
import java.math.*;
class demo
{
public static void main(String args[])
{
BigInteger x=new BigInteger("12345678910111213141516171819");
BigInteger y=new BigInteger("5");
BigInteger z=x.multiply(y);
System.out.println(“Z=”+z);
}
}
Output:
Page | 23
Prepared By.Prof Ankit Patel [CE,LJIET]
___________________________________________________________________Chapter 4
Z=61728394550556065707580859095
Example of BigDecimal
import java.math.*;
class demo
{
public static void main(String args[])
{
BigDecimal x=new BigDecimal("1.0");
BigDecimal y=new BigDecimal("6.0");
BigDecimal z=x.divide(y,30,BigDecimal.ROUND_UP);
System.out.println(z);
}
}
Output:
Z= 0.166666666666666666666666666667
Example: Factorial of 50
import java.math.*;
class extra
{
public static void main(String args[])
{
BigInteger fact=new BigInteger("1");
for(int i=1;i<=50;i++)
{
fact=fact.multiply(BigInteger.valueOf(i));
}
System.out.println("fact="+fact);
}
}
Page | 24
Prepared By.Prof Ankit Patel [CE,LJIET]