Module_2 (4)
Module_2 (4)
ce protected
Object Oriented
Programming
Module 2: Inheritance
[email protected]
School of Computer Science and Engineering (SCOPE)
VIT-AP University (Besides AP Secretariat) 522237
May 8, 2022
Agenda I
Java Inheritance
Inheritance is one of the key features of OOP that allows us
to create a new class from an existing class.
The new class that is created is known as subclass (child or
derived class) and the existing class from where the child class
is derived is known as superclass (parent or base class).
The extends keyword is used to perform inheritance in Java
1 class Animal {
2 // methods and fields
3 }
4
5 // use of extends keyword
6 // to perform inheritance
7 class Dog extends Animal {
8
9 // methods and fields of Animal
10 // methods and fields of Dog
11 }
12
1 class Animal {
2
3 // field and method of the parent class
4 String name ;
5 public void eat () {
6 System . out . println ( " I can eat " ) ;
7 }
8 }
9
10 // inherit from Animal
11 class Dog extends Animal {
12
13 // new method in subclass
14 public void display () {
15 System . out . println ( " My name is " + name ) ;
16 }
17 }
./code/InheritanceExample.java
18 class Main {
19 public static void main ( String [] args ) {
20
21 // create an object of the subclass
22 Dog labrador = new Dog () ;
23
24 // access field of superclass
25 labrador . name = " Jerry " ;
26 labrador . display () ;
27
28 // call method of superclass
29 // using object of subclass
30 labrador . eat () ;
31
32 }
33 }
./code/InheritanceExample.java
is-a relationship
Types of Inheritance
1 Single Inheritance
2 Multilevel Inheritance
3 Hierarchical Inheritance
4 Multiple Inheritance
5 Hybrid Inheritance
1. Single Inheritance
1. Single Inheritance
1 class Animal {
2 void eat () { System . out . println ( " eating " ) ;}
3 }
4 class Dog extends Animal {
5 void bark () { System . out . println ( " barking " ) ;}
6 }
7 class Te st I nh e ri ta n ce {
8 public static void main ( String args []) {
9 Dog d = new Dog () ;
10 d . bark () ;
11 d . eat () ;
12 }
13 }
./code/SingelLevelInheritance.java
2. Multilevel Inheritance
In multilevel inheritance, a subclass extends from a superclass
and then the same subclass acts as a superclass for another
class.
2. Multilevel Inheritance
1 class Animal {
2 void eat () { System . out . println ( " eating " ) ;}
3 }
4 class Dog extends Animal {
5 void bark () { System . out . println ( " barking " ) ;}
6 }
7 class Puppy extends Dog {
8 void weep () { System . out . println ( " weeping " ) ;}
9 }
10 class T e s t I n h e r i t a n c e 2 {
11 public static void main ( String args []) {
12 Puppy d = new Puppy () ;
13 d . weep () ;
14 d . bark () ;
15 d . eat () ;
16 }
17 }
./code/MultiLevelInheritance.java
3. Hierarchical Inheritance
3. Hierarchical Inheritance
1 class Animal {
2 void eat () { System . out . println ( " eating " ) ;}
3 }
4 class Dog extends Animal {
5 void bark () { System . out . println ( " barking " ) ;}
6 }
7 class Cat extends Animal {
8 void meow () { System . out . println ( " meowing " ) ;}
9 }
10 class T e s t I n h e r i t a n c e 3 {
11 public static void main ( String args []) {
12 Cat c = new Cat () ;
13 c . meow () ;
14 c . eat () ;
15 }
16 }
./code/HierarchicalInheritance.java
4. Multiple Inheritance
In multiple inheritance, a single subclass extends from multiple
superclasses.
5. Hybrid Inheritance
Rules of Inheritance
RULE 1: Multiple Inheritance is NOT permitted in Java.
1 class Demo1
2 {
3 // code here }
4 class Demo2
5 {
6 // code here }
7 class Demo3 extends Demo1 , Demo2
8 {
9 // code here }
10 class Launch
11 {
12 public static void main ( String args [])
13 {
14 // code here
15 } }
Rules of Inheritance
In the above code, both the classes are trying to inherit each other’s
characters which is not permitted as it leads to ambiguity.
Rules of Inheritance
./code/PrivateInheritance.java
Rules of Inheritance
./code/PrivateInheritance.java
Rules of Inheritance
./code/PrivateInheritance.java
When you execute the above code, guess what happens, do you
think the private variables an and pw will be inherited? Absolutely not.
It remains the same because they are specific to the particular class
Rules of Inheritance
1 class Animal {
2 public void displayInfo () {
3 System . out . println ( " I am an animal . " ) ;
4 }
5 }
6 class Dog extends Animal {
7 // @Override
8 public void displayInfo () {
9 System . out . println ( " I am a dog . " ) ;
10 }
11 }
12
13 class Main {
14 public static void main ( String [] args ) {
15 Dog d1 = new Dog () ;
16 d1 . displayInfo () ;
17 }
18 }
./code/MethodOverriding.java
Both the superclass and the subclass must have the same
method name, the same return type and the same parameter
list.
We cannot override the method declared as final and static.
We should always override abstract methods of the superclass
1 class Animal {
2 protected void displayInfo () {
3 System . out . println ( " I am an animal . " ) ;
4 }
5 }
6
7 class Dog extends Animal {
8 public void displayInfo () {
9 System . out . println ( " I am a dog . " ) ;
10 }
11 }
12
13 class Main {
14 public static void main ( String [] args ) {
15 Dog d1 = new Dog () ;
16 d1 . displayInfo () ;
17 }
18 }
./code/AccessSpecifierInOverriding.java
./code/SuperKeyword.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 30 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
21 class Main {
22 public static void main ( String [] args ) {
23
24 // create an object of the subclass
25 Dog labrador = new Dog () ;
26
27 // call the eat () method
28 labrador . eat () ;
29 labrador . bark () ;
30 }
31 }
./code/SuperKeyword.java
./code/SuperConstructor.java
./code/ProtectedDemo.java
15
16 class Main {
17 public static void main ( String [] args ) {
18
19 // create an object of the subclass
20 Dog labrador = new Dog () ;
21
22 // access protected field and method
23 // using the object of subclass
24 labrador . name = " Rocky " ;
25 labrador . display () ;
26
27 labrador . getInfo () ;
28 }
29 }
./code/ProtectedDemo.java
An abstract class can have both the regular methods and abstract
methods
1 abstract class Language {
2
3 // abstract method
4 abstract void method1 () ;
5
6 // regular method
7 void method2 () {
8 System . out . println ( " This is regular method " ) ;
9 }
10 }
./code/AbstarctMethiod.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 40 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
./code/AbstarctMethod.java
Note: If the Dog class doesn’t provide the implementation of the ab-
stract method makeSound(), Dog should also be declared as ab-
stract. This is because the subclass Dog inherits makeSound() from
Animal.
An abstract class can have constructors like the regular class. And,
we can access the constructor of an abstract class from the subclass
using the super keyword.
1 abstract class Animal {
2 Animal () {
3 ...
4 }
5 }
6
7 class Dog extends Animal {
8 Dog () {
9 super () ;
10 ...
11 }
12 }
Java Abstraction
./code/MotorBike.java
20
21 class Main {
22 public static void main ( String [] args ) {
23 MountainBike m1 = new MountainBike () ;
24 m1 . brake () ;
25 SportsBike s1 = new SportsBike () ;
26 s1 . brake () ;
27 }
28 }
./code/MotorBike.java
Java Interface
Implementing an Interface
To use an interface, other classes must implement it. We use
the implements keyword to implement an interface
1 interface Polygon {
2 void getArea ( int length , int breadth ) ;
3 }
4 // implement the Polygon interface
5 class Rectangle implements Polygon {
6
7 // implemen tation of abstract method
8 public void getArea ( int length , int breadth ) {
9 System . out . println ( " The area of the rectangle is " + (
length * breadth ) ) ;
10 }
11 }
12 class Main {
13 public static void main ( String [] args ) {
14 Rectangle r1 = new Rectangle () ;
15 r1 . getArea (5 , 6) ;
16 }
17 }
./code/InterfaceExample.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 51 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
./code/JavaInterfaceExample.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 52 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
Extending an Interface
Note: Interfaces
All the methods inside an interface are implicitly public and all fields
are implicitly public static final
1 interface Language {
2
3 // by default public static final
4 String type = " programming language " ;
5
6 // by default public
7 void getName () ;
8 }
./code/DefaultMethodInInterface.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 61 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
./code/DefaultMethodInInterface.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 62 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
The Java also added another feature to include static methods inside
an interface
Similar to a class, we can access static methods of an interface using
its references
1 // create an interface
2 interface Polygon {
3 staticMethod () {..}
4 }
5
6 // access static method
7 Polygon . staticMethod () ;
./code/InterfacePracticalexample.java
./code/InterfacePracticalexample.java
1
2 class Main {
3 public static void main ( String [] args ) {
4 Triangle t1 = new Triangle (2 , 3 , 4) ;
5
6 // calls the method of the Triangle class
7 t1 . getArea () ;
8
9 // calls the method of Polygon
10 t1 . getPerimeter (2 , 3 , 4) ;
11 }
12 }
./code/InterfacePracticalexample.java
Java Polymorphism
./code/JavaPolymorphism.java
21 class Main {
22 public static void main ( String [] args ) {
23
24 // create an object of Square
25 Square s1 = new Square () ;
26 s1 . render () ;
27
28 // create an object of Circle
29 Circle c1 = new Circle () ;
30 c1 . render () ;
31 }
32 }
./code/JavaPolymorphism.java
./code/MethodPolymorphism.java
12
13 class Main {
14 public static void main ( String [] args ) {
15
16 // create an object of Java class
17 Java j1 = new Java () ;
18 j1 . displayInfo () ;
19
20 // create an object of Language class
21 Language l1 = new Language () ;
22 l1 . displayInfo () ;
23 }
24 }
./code/MethodPolymorphism.java
./code/MethodOverloadingExample.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 73 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
Polymorphic Variables
Polymorphic Variables
1 class P r o g r a m m i n g L a n g u a g e {
2 public void display () {
3 System . out . println ( " I am Programming Language . " ) ; } }
4 class Java extends P r o g r a m m i n g L a n g u a g e {
5 @Override
6 public void display () {
7 System . out . println ( " I am Object - Oriented Programming
Language . " ) ;
8 } }
9 class Main {
10 public static void main ( String [] args ) {
11 // declare an object variable
12 P r o g r a m m i n g L a n g u a g e pl ;
13 // create object of P r o g r a m m i n g L a n g u a g e
14 pl = new P r o g r a m m i n g L a n g u a g e () ;
15 pl . display () ;
16 // create object of Java class
17 pl = new Java () ;
18 pl . display () ;
19 } }
./code/PolymorphicVariable.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 76 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
Polymorphic Variables
Java Encapsulation
Why Encapsulation?
Why Encapsulation?
Data Hiding
Data hiding is a way of restricting the access of our data mem-
bers by hiding the implementation details. Encapsulation also
provides a way for data hiding.
We can use access modifiers to achieve data hiding.
1 class Person {
2 // private field
3 private int age ;
4 // getter method
5 public int getAge () {
6 return age ;
7 }
8 // setter method
9 public void setAge ( int age ) {
10 this . age = age ;
11 }
12 }
./code/DataHiding.java
13
14 class Main {
15 public static void main ( String [] args ) {
16
17 // create an object of Person
18 Person p1 = new Person () ;
19
20 // change age using setter
21 p1 . setAge (24) ;
22
23 // access age using getter
24 System . out . println ( " My age is " + p1 . getAge () ) ;
25 }
26 }
./code/DataHiding.java
In Java, you can define a class within another class. Such class
is known as nested class
1 class OuterClass {
2 // ...
3 class NestedClass {
4 // ...
5 }
6 }
There are two types of nested classes you can create in Java.
Non-static nested class (inner class)
Static nested class
1 class CPU {
2 double price ;
3 // nested class
4 class Processor {
5
6 // members of nested class
7 double cores ;
8 String manufacturer ;
9
10 double getCache () {
11 return 4.3;
12 }
13 }
14 // nested protected class
./code/InnerClass.java
./code/InnerClass.java
./code/InnerClass.java
./code/AccessInnerClass.java
./code/AccessInnerClass.java
Dr. Nikhil Mhala VIT-AP CSE1012 May 8, 2022 91 / 101
Java Inheritance Types of inheritance Java Method Overriding Access Specifiers in Method Overriding super Keyword in Java Inheritance protected
./code/StaticInnerClass.java
./code/AccessOuterClassInsideStaticClass.java
Java Packages
Built-in Packages
User-defined Packages
To create your own package, you need to understand that Java uses
a file system directory to store them. Just like folders on your com-
puter
To create a package, use the package keyword:
1 package mypack ;
2 class My Packag eClass {
3 public static void main ( String [] args ) {
4 System . out . println ( " This is my package ! " ) ;
5 }
6 }
Contact:
[email protected]
Landline: 0863-2370961 (Mon-Fri 9 am - 5 pm)
Extension: 5961