CSE Module 2
CSE Module 2
public c l a s s Sum {
public i n t sum ( i n t x , i n t y )
{
return (x + y );
}
public i n t sum ( i n t x , i n t y , i n t z )
{
return (x + y + z );
}
public d o u b l e sum ( d o u b l e x , d o u b l e y )
{
return (x + y );
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
Sum s = new Sum ( ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 , 2 0 ) ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 , 2 0 , 3 0 ) ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 . 5 , 2 0 . 5 ) ) ;
}
}
c l a s s MethodOverloading {
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a ){
System . o u t . p r i n t l n ( ” Arguments : ” + a ) ;
}
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a , i n t b ){
System . o u t . p r i n t l n ( ” Arguments : ” + a + ” and ” + b ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
display (1);
d i s p l a y (1 , 4);
}
}
c l a s s MethodOverloading1 {
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a ){
System . o u t . p r i n t l n ( ” Got I n t e g e r d a t a . ” ) ;
}
p r i v a t e s t a t i c v o i d d i s p l a y ( S t r i n g a ){
System . o u t . p r i n t l n ( ” Got S t r i n g o b j e c t . ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
display (1);
d i s p l a y (” Hello ” ) ;
}
}
class Vehicle {
void run ( )
{
System . o u t . p r i n t l n ( ” V e h i c l e i s r u n n i n g ” ) ;
}
}
c l a s s Bike2 extends V e h i c l e {
void run (){
System . o u t . p r i n t l n ( ” B i k e i s r u n n i n g s a f e l y ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
B i k e 2 o b j = new B i k e 2 ( ) ;
obj . run ( ) ;
}
}
c l a s s Parent {
v o i d show ( )
{
System . o u t . p r i n t l n ( ” P a r e n t ’ s show ( ) ” ) ;
}
}
c l a s s Child extends Parent {
v o i d show ( )
{
System . o u t . p r i n t l n ( ” C h i l d ’ s show ( ) ” ) ;
}
}
c l a s s Person
{
p u b l i c s t a t i c void speak ()
{
System . o u t . p r i n t l n ( ” P e r s o n s p e a k s ” ) ;
}
}
From the above code, we got the same output from the parent class. This
happened because:
The reference for the parent class and the child class is the
same(Person). That is, a single object refers to both of them.
Since the method is static, the compiler is aware that this method
can not be overridden in the child class and it knows which method
to call.
Therefore there is no ambiguity and the output is the same for both
cases.
c l a s s Person
{
p u b l i c void speak ()
{
System . o u t . p r i n t l n ( ” P e r s o n s p e a k s ” ) ;
}
}
c l a s s Teacher extends Person
{
p u b l i c void speak ()
{
System . o u t . p r i n t l n ( ” T e a c h e r s p e a k s ” ) ;
}
}
p u b l i c c l a s s DynamicBinding
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
P e r s o n o b j 2 = new P e r s o n ( ) ;
obj2 . speak ( ) ;
P e r s o n o b j = new T e a c h e r ( ) ;
obj . speak ( ) ;
}
}
The extent to which a module hides its internal data and other
implementation details from the other modules is the most important
factor that distinguishes a well-designed Object-Oriented module from
other modules.
A well-designed module hides all of its implementation details and
cleanly separates its interface from its implementation.
These modules then communicate with each other only through the
interfaces.This concept is supported with the help of Abstraction in
Java.
The meaning of the word “Abstraction”, in general words, is the
process of working with ideas rather than their implementation.
For example, consider the example of an email, the user does not
know about the complex details such as what happens just after
sending an email, which protocol is used by the server to send the
message.
Therefore, we just need to mention the address of the receiver, type
the content and click the send button.
This is basically called Abstraction in which the complex details are
being hidden from the users.
Similarly, in Object-oriented programming, abstraction is a process of
providing functionality to the users by hiding its implementation
details from them.
In other words, the user will have just the knowledge of what an
entity is doing instead of its internal working.
abstract c l a s s BaseClass
{
a b s t r a c t p u b l i c v o i d show1 ( ) ;
p u b l i c v o i d show2 ( )
{
System . o u t . p r i n t l n ( ” C o n c r e t e method ” ) ; }
}
c l a s s ChildClass extends BaseClass
{
p u b l i c v o i d show1 ( )
{
System . o u t . p r i n t l n ( ” show1 ” ) ;
}
p u b l i c v o i d show2 ( )
{
System . o u t . p r i n t l n ( ” show2 ” ) ; }
}
p u b l i c c l a s s AbstractionDemo
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
C h i l d C l a s s o b j = new C h i l d C l a s s ( ) ;
o b j . show1 ( ) ;
o b j . show2 ( ) ;
}
p u b l i c c l a s s AbstractionDemo1
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
P a r e n t C l a s s o b j = new P a r e n t C l a s s ( ) ;
obj . showDetails ( ) ;
}
}
i n t e r f a c e <i n t e r f a c e n a m e >{
// d e c l a r e c o n s t a n t f i e l d s
// d e c l a r e methods t h a t a b s t r a c t
// by d e f a u l t .
}
The Java compiler adds public and abstract keywords before the
interface method.
Moreover, it adds public, static and final keywords before data
members.
In other words, Interface fields are public, static and final by default,
and the methods are public and abstract.
interface printable{
void print ( ) ;
}
c l a s s A6 i m p l e m e n t s p r i n t a b l e {
p u b l i c v o i d p r i n t ( ) { System . o u t . p r i n t l n ( ” H e l l o ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A6 o b j = new A6 ( ) ;
obj . p r i n t ( ) ;
}
}
i n t e r f a c e Drawable {
v o i d draw ( ) ;
}
c l a s s R e c t a n g l e i m p l e m e n t s Drawable {
p u b l i c v o i d draw ( ) {
System . o u t . p r i n t l n ( ” d r a w i n g r e c t a n g l e ” ) ; }
}
c l a s s C i r c l e i m p l e m e n t s Drawable {
p u b l i c v o i d draw ( ) {
System . o u t . p r i n t l n ( ” d r a w i n g c i r c l e ” ) ; }
}
interface Printable {
void print ( ) ;
}
i n t e r f a c e Showable {
v o i d show ( ) ;
}
c l a s s A7 i m p l e m e n t s P r i n t a b l e , Showable {
p u b l i c v o i d p r i n t ( ) { System . o u t . p r i n t l n ( ” H e l l o ” ) ; }
p u b l i c v o i d show ( ) { System . o u t . p r i n t l n ( ” Welcome ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A7 o b j = new A7 ( ) ;
obj . p r i n t ( ) ;
o b j . show ( ) ;
}
}
While creating a package, you should choose a name for the package
and include a package statement along with that name at the top of
every source file that contains the classes, interfaces, enumerations,
and annotation types that you want to include in the package.
The package statement should be the first line in the source file.
There can be only one package statement in each source file, and it
applies to all types in the file.
If a package statement is not used then the class, interfaces,
enumerations, and annotation types will be placed in the current
default package.
To compile the Java programs with package statements, you have to use
-d option as shown below.
j a v a c −d D e s t i n a t i o n f o l d e r f i l e n a m e . j a v a
j a v a c −d . Animal . j a v a
j a v a c −d . MammalInt . j a v a
Built-in packages
These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
1 java.lang: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2 java.io: Contains classed for supporting input / output operations.
3 java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4 java.applet: Contains classes for creating Applets.
5 java.awt: Contain classes for implementing the components for
graphical user interfaces (like button ,menus etc).
6 java.net: Contain classes for supporting networking operations.
p a c k a g e myPackage ;
p u b l i c c l a s s MyClass
{
p u b l i c v o i d getNames ( S t r i n g s )
{
System . o u t . p r i n t l n ( s ) ;
}
}
p u b l i c c l a s s PrintName
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
// I n i t i a l i z i n g t h e S t r i n g v a r i a b l e
// w i t h a v a l u e
S t r i n g name = ” S u d h e e r ” ;
// C r e a t i n g an i n s t a n c e o f c l a s s MyClass i n
// t h e p a c k a g e .
MyClass o b j = new MyClass ( ) ;
o b j . getNames ( name ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
57 / 76
Illustration of user-defined packages
Creating our first package: File name – ClassOne.java
p a c k a g e package name ;
p u b l i c c l a s s ClassOne {
p u b l i c v o i d methodClassOne ( ) {
System . o u t . p r i n t l n ( ” H e l l o t h e r e i t s C l a s s O n e ” ) ;
}
}
Creating our second package: File name – ClassTwo.java
package package one ;
p u b l i c c l a s s ClassTwo {
p u b l i c v o i d methodClassTwo ( ) {
System . o u t . p r i n t l n ( ” H e l l o t h e r e i am ClassTwo ” ) ;
}
}
public c l a s s Testing {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
ClassTwo a = new ClassTwo ( ) ;
C l a s s O n e b = new C l a s s O n e ( ) ;
a . methodClassTwo ( ) ;
b . methodClassOne ( ) ;
}
}
The Object class is the parent class of all the classes in java by
default. 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.
Let’s take an example, there is getObject() method that returns an
object but it can be of any type like Employee,Student etc, we can
use Object class reference to refer that object.
O b j e c t o b j=g e t O b j e c t ( ) ;
//we don ’ t know what o b j e c t w i l l be r e t u r n e d from t h i s
The Object class provides some common behaviors to all the objects
such as object can be compared, object can be cloned, object can be
notified etc.
The clone() method saves the extra processing task for creating the
exact copy of an object.
If we perform it by using the new keyword, it will take a lot of
processing time to be performed that is why we use object cloning.
p u b l i c c l a s s Te {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Te t = new Te ( ) ;
System . o u t . p r i n t l n ( t . hashCode ( ) ) ;
t = null ;
// c a l l i n g g a r b a g e c o l l e c t o r
System . gc ( ) ;
System . o u t . p r i n t l n ( ” end ” ) ; }
protected void f i n a l i z e ()
{
System . o u t . p r i n t l n ( ” f i n a l i z e method c a l l e d ” ) ;
}}
An inner class can also be static, which means that you can access it
without creating an object of the outer class:
c l a s s OuterClass {
i n t x = 10;
static class InnerClass {
i n t y = 5;}}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s . I n n e r C l a s s m y I n n e r = new O u t e r C l a s s . I n n e r C l
System . o u t . p r i n t l n ( m y I n n e r . y ) ;
}}
c l a s s OuterClass {
i n t x = 10;
class InnerClass {
p u b l i c i n t myInnerMethod ( ) {
return x ;
} }}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s myOuter = new O u t e r C l a s s ( ) ;
O u t e r C l a s s . I n n e r C l a s s m y I n n e r = myOuter . new I n n e r C l a s s
System . o u t . p r i n t l n ( m y I n n e r . myInnerMethod ( ) ) ;
}}
c l a s s Outer Demo {
i n t num ;
// i n n e r c l a s s
p r i v a t e c l a s s Inner Demo {
public void print () {
System . o u t . p r i n t l n ( ” T h i s i s an i n n e r c l a s s ” ) ;
} }
// A c c e s s i n g t h e i n n e r c l a s s from t h e method w i t h i n
void d i s p l a y I n n e r () {
Inner Demo i n n e r = new Inner Demo ( ) ;
inner . print ();
}}
c l a s s Outer Demo {
// p r i v a t e v a r i a b l e o f t h e o u t e r c l a s s
p r i v a t e i n t num = 1 7 5 ;
// i n n e r c l a s s
p u b l i c c l a s s Inner Demo {
p u b l i c i n t getNum ( ) {
System . o u t . p r i n t l n ( ” T h i s i s t h e getnum method o f t h
r e t u r n num ;
} }}