Multiple Inheritance: Module-2
Multiple Inheritance: Module-2
Multiple Inheritance: Module-2
MODULE-2
MULTIPLE INHERITANCE
Inheritance
• Java, Inheritance is an important pillar of OOP(Object-Oriented
Programming).
• It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class.
• In Java, Inheritance means creating new classes based on existing ones.
• A class that inherits from another class can reuse the methods and fields of
that class.
• Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.
•
Why Do We Need Java Inheritance?
• Code Reusability: The code written in the Superclass is common to all
subclasses.
• Child classes can directly use the parent class code.
• Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
• Abstraction: The concept of abstract where we do not have to provide all
details is achieved through inheritance. Abstraction only shows the
functionality to the user.
YOGEESH S Page 1
Object Oriented Programming Using Java 22MCA22
YOGEESH S Page 2
Object Oriented Programming Using Java 22MCA22
extends
Single Inheritance
• When a class inherits another class, it is known as a single inheritance.
Example:
class Animal
{
void run()
{
System.out.println("MCA");
}
}
class Dog extends Animal
{
void display()
{
System.out.println("MBA");
}
}
class Single
{
public static void main(String args[])
{
Dog d=new Dog();
d.run();
d.display();
}
}
YOGEESH S Page 3
Object Oriented Programming Using Java 22MCA22
Multilevel Inheritance
• When there is a chain of inheritance, it is known as multilevel inheritance.
Example
class Animal
{
void run()
{
System.out.println("MCA");
}
}
class Dog extends Animal
{
void display()
{
System.out.println("MBA");
}
}
class Cat extends Dog
{
void ball()
{
System.out.println("BCA");
}
}
class multilevel
{
public static void main(String args[])
{
Cat d=new Cat();
d.ball();
d.run();
d.display();
}
}
Hierarchical Inheritance
• When two or more classes inherits a single class, it is known as
hierarchical inheritance.
Example
class Animal
{
void run()
{
System.out.println("MCA");
}
}
class Dog extends Animal
{
void display()
{
System.out.println("MBA");
}
}
YOGEESH S Page 4
Object Oriented Programming Using Java 22MCA22
}
}
YOGEESH S Page 5
Object Oriented Programming Using Java 22MCA22
having default access modifier are accessible only within the same package.
YOGEESH S Page 6
Object Oriented Programming Using Java 22MCA22
YOGEESH S Page 7
Object Oriented Programming Using Java 22MCA22
final method
• If you make any method as final, you cannot override it.
final class
• If you make any class as final, you cannot extend it.
YOGEESH S Page 8
Object Oriented Programming Using Java 22MCA22
obj.run();
}
}
//compile time error
YOGEESH S Page 9
Object Oriented Programming Using Java 22MCA22
YOGEESH S Page 10
Object Oriented Programming Using Java 22MCA22
toString() method
• If you want to represent any object as a string, toString() method comes into
existence.
• The toString() method returns the String representation of the object.
• If you print any object, Java compiler internally invokes the toString()
method on the object. So overriding the toString() method, returns the
desired output, it can be the state of an object etc. depending on your
implementation.
•
Example
class Student12
{
int rollno;
String name;
String city;
Student12(int rollno, String name, String city)
{
this.rollno=rollno;
this.name=name;
this.city=city;
}
public String toString()
{
return rollno+" "+name+" "+city;
}
YOGEESH S Page 11
Object Oriented Programming Using Java 22MCA22
hashCode() Method
• The hashCode() method is a Java Integer class method which returns the
hash code for the given inputs.
It returns true if this object is same as the obj argument else it returns false
otherwise.
class kiran
{
int a=25;
}
class bangaluru
{
public static void main(String[] args)
{
kiran s1 = new kiran();
kiran s2 = new kiran();
System.out.println(s1.equals(s2)); //false
kiran s3=s1;
System.out.println(s1.equals(s3)); //true
}
}
getClass() Method
• getClass() is the method of Object class. This method returns the runtime
class of this object.
public class Objectget
{
public static void main(String[] args)
{
Object obj = new String("23");
Class a = obj.getClass();
System.out.println("Class of Object obj is : " + a.getName());
}
}
Output: Class of Object obj is : java.lang.String
YOGEESH S Page 12
Object Oriented Programming Using Java 22MCA22
Polymorphism in Java
• Polymorphism in Java is a concept by which we can perform a single action
in different ways.
• Polymorphism is derived from 2 Greek words: poly and morphs. The word
"poly" means many and "morphs" means forms. So polymorphism means
many forms.
• There are two types of polymorphism in Java: compile-time polymorphism
and runtime polymorphism. We can perform polymorphism in java by
method overloading and method overriding.
• If you overload a static method in Java, it is the example of compile time
polymorphism. Here, we will focus on runtime polymorphism in java.
Runtime Polymorphism in Java
• Runtime polymorphism or Dynamic Method Dispatch is a process in which a
call to an overridden method is resolved at runtime rather than compile-
time.
• In this process, an overridden method is called through the reference
variable of a superclass.
• The determination of the method to be called is based on the object being
referred to by the reference variable.
Upcasting
• If the reference variable of Parent class refers to the object of Child class, it
is known as upcasting.
Example
class Bike
{
void run()
{
System.out.println("running");
}
}
class Splendor extends Bike
{
void run()
{
System.out.println(“RNSIT");
}
public static void main(String args[])
{
Bike b = new Splendor(); //upcasting
b.run();
}
}
Method overriding
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
Usage of Java Method Overriding
• Method overriding is used to provide specific implementation of a method
that is already provided by its super class.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• method must have same name as in the parent class
• method must have same parameter as in the parent class.
YOGEESH S Page 13
Object Oriented Programming Using Java 22MCA22
EX:
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
}
EX:2
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
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;
}
void show()
{
System.out.println("k: " + k);
}
}
class Override
{
public static void main(String args[])
{
B sub = new B(1, 2, 3);
sub.show();
YOGEESH S Page 14
Object Oriented Programming Using Java 22MCA22
}
}
EX:3
class Animal
{
void move()
{
System.out.println("Animals can move");
}
}
class Dog extends Animal
{
void move()
{
super.move();
System.out.println("Dogs can walk and run");
}
}
public class TestDog
{
public static void main(String args[])
{
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
YOGEESH S Page 15
Object Oriented Programming Using Java 22MCA22
Abstract classes
• A class that is declared with abstract keyword, is known as abstract class in
java. It can have abstract and non-abstract methods (method with body).
• Before learning java abstract class, let's understand the abstraction in java
first.
• Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
• Another way, it shows only important things to the user and hides the
internal details.
• A method that is declared as abstract and does not have implementation is
known as abstract method.
Ex:1
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();
}
}
Ex: 2
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle1 extends Shape
{
void draw()
{
System.out.println("drawing circle");
}
}
class TestAbstraction1
{
public static void main(String args[])
{
Shape s=new Circle1();
YOGEESH S Page 16
Object Oriented Programming Using Java 22MCA22
s.draw();
}
}
YOGEESH S Page 17
Object Oriented Programming Using Java 22MCA22
2.concat(String s)
• This method returns a String with the value of the String passed in to the
method appended to the end of the String used to invoke the method.
Example
String x = "book";
System.out.println( x.concat("author") );
// output is "bookauthor"
The overloaded + and += operators perform functions similar to the
concat()method
Example,
String x = "library";
System.out.println( x + " card");
// output is "library card"
String x = "United";
x += " States”
System.out.println( x );
// output is "United States"
3.equalsIgnoreCase(String s)
• This method returns a boolean value (true or false) depending on whether
the value of the String in the argument is the same as the value of the String
used to invoke the method.
Example
String x = "Exit"; System.out.println(x.equalsIgnoreCase("EXIT")); // is "true"
System.out.println(x.equalsIgnoreCase("tixe")); // is "false"
4. length()
• This method returns the length of the String used to invoke the method.
Example,
String x = "01234567";
System.out.println( x.length() ); // returns "8“
Output
RnSnT
YOGEESH S Page 18
Object Oriented Programming Using Java 22MCA22
6. toLowerCase()
• This method returns a String whose value is the String used to invoke the
method, but with any uppercase characters converted to lowercase.
Example,
String x = "A New Java Book";
System.out.println( x.toLowerCase() );
// output is "a new java book"
7. toUpperCase()
• This method returns a String whose value is the String used to invoke the
method, but with any lowercase characters converted touppercase.
Example,
String x = "A New Java Book"; System.out.println( x.toUpperCase());
// output is"A NEW JAVA BOOK"
8. trim() method
• The String class trim() method eliminates white spaces before and after the
String.
Example
class Stringoperation2
{
public static void main(String ar[])
{
String s=" Sachin ";
System.out.println(s.trim());//Sachin
}
}
YOGEESH S Page 19
Object Oriented Programming Using Java 22MCA22
9. char[ ] toCharArray( )
• This method will produce an array of characters from characters of String
object.
Example
String s = “Java”;
Char [] mca = s.toCharArray();
Parameter Passing
• People usually take the pass by value and pass by reference terms together.
• It is really confusing and overhear questions in interviews, Is java pass by
value or passes by reference, or both? So the answer to this question is Java
is strictly pass by value.
• There is no pass by reference in Java.
• People usually take the pass by value and pass by reference terms together.
• It is really confusing and overhear questions in interviews, Is java pass by
value or passes by reference, or both? So the answer to this question is Java
is strictly pass by value.
• There is no pass by reference in Java.
• Pass by Value: In the pass by value concept, the method is called by passing
a value.
• So, it is called pass by value. It does not affect the original parameter.
• Pass by Reference: In the pass by reference concept, the method is called
using an alias or reference of the actual parameter.
• So, it is called pass by reference. It forwards the unique identifier of the
object to the method.
• If we made changes to the parameter's instance member, it would affect the
original value.
Example1:
class Test
{
void display(int i, int j)
{
i *= 2;
j += 2;
}
}
class CallByValue
{
public static void main(String args[])
{
Test ob = new Test();
int a = 15, b = 20;
YOGEESH S Page 20
Object Oriented Programming Using Java 22MCA22
Example2:
class Test
{
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
void Display(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
class CallByRef
{
public static void main(String args[])
{
Test ob = new Test(15, 10);
System.out.println("ob.a and ob.b before call: " + ob.a+ " " + ob.b);
ob.Display(ob);
System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b);
}
}
Output
ob.a and ob.b before call: 15 10
ob.a and ob.b after call: 30 5
ENUMERATION:
• Enumeration is a list of named constants that define a new data type.
• An object of an enumeration type can hold only the values that are defined
by the list.
• Thus , an enumeration gives you a way to precisely define a new type of data
that has a fixed number of valid values.
• An enumeration is created using the enum keyword.
Ex:
enum Transport
{
CAR, TRUNK, TRAIN, BUS
}
• The identifiers CAR, TRUNK and so on are called enumeration constants or
enum constants.
• Each is implicitly declared as a public, static member of Transport.
YOGEESH S Page 21
Object Oriented Programming Using Java 22MCA22
• Once you have declared an enumeration, you can create a variable of that
type.
• Even though enumerations define a class type, you do not instantiate an
enum using new.
Ex:
Transport tp;
• Because tp is of type of Transport, the only values that it can be assigned
are those constants defined by the enumeration or null.
Ex:
tp= Transport.BUS;
• Here BUS is assiged to tp.
• Two enumeration constant can be compared for equality by using the ==
relational operator.
Ex:
if(tp==Transport.CAR)
• Here this statement compares the value in tp with the CAR.
• An enumeration value can also be used to control a switch statement.
YOGEESH S Page 22
Object Oriented Programming Using Java 22MCA22
Example
enum NAMES
{
RAGHU, PRASAD, DEVA, SHIVA,KIRAN
}
class EnumExample3
{
public static void main(String[] args)
{
NAMES s=NAMES.RAJ;
System.out.println(s);
}
}
D:\raghu java practice>javac EnumExample3.java
EnumExample3.java:9: cannot find symbol
symbol : variable RAJ
location: class NAMES
NAMES s=NAMES.RAJ;
^
1 error
class hello{
public enum Season
{
WINTER, SPRING, SUMMER, FALL
}
public static void main(String[] args)
{
for (Season s : Season.values())
System.out.println(s);
}
}
YOGEESH S Page 23
Object Oriented Programming Using Java 22MCA22
class EnumExample1{
public enum Season {
WINTER,
SPRING,
SUMMER,
FALL
}
public static void main(String[] args)
{
for (Season s : Season.values())
System.out.println(s);
Season a = Season.valueOf("SUMMER");
System.out.println("the value of a is");
System.out.println(a);
}
}
OUTPUT WINTER
SPRING
SUMMER
FALL
the value of a is
SUMMER
YOGEESH S Page 24