Sie sind auf Seite 1von 41

Returning Objects:

A method can return any type of data

class Test
{
int a;
Test(int i)
{
a = i;
}
Test incrByTen()
{
Test temp = new Test(a+10);
return temp;
}
}
Returning Objects contd

class RetOb
{
public static void main(String args[])
{
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "+ ob2.a);
}
}

Output:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22
Recursion:

It is the process of defining something in


terms of itself. As it relates to Java
programming, recursion is the attribute
that allows a method to call itself. A
method that calls itself is said to be
recursive.
class Factorial
{
int fact(int n)
{
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class Recursion
{
public static void main(String args[])
{
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
Nested and Inner Classes

1. It is possible to define a class within another class; such classes are


known as nested classes.

2. The scope of a nested class is bounded by the scope of its enclosing


class.

3. if class B is defined within class A, then B is known to A, but not outside of


A.
class Outer
{
int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();
}
class Inner
{
void display()
{
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.test();
}
}
String Class

String is probably the most commonly used class in Javas class library

String are immutable; once a String object is created, its contents cannot be
altered. While this may seem like a serious restriction, it is not, for two reasons:

If you need to change a string, you can always create a new one that contains the
modifications.

Java defines a peer class of String, called StringBuffer, which allows strings to
be altered, so all of the normal string manipulations are still available in Java.

String myString = "this is a test";

System.out.println(myString);

String myString = "I" + " like " + "Java.";


class StringDemo
{
public static void main(String args[])
{
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;

System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}

output :

First String

Second String

First String and Second String


class StringDemo2
{
public static void main(String args[])
{
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " +strOb1.length());
System.out.println("Char at index 3 in strOb1: " +strOb1.charAt(3));

if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}
Length of strOb1: 12
Char at index 3 in strOb1: s
strOb1 != strOb2
strOb1 == strOb3
class StringDemo3
{
public static void main(String args[])
{
String str[] = { "one", "two", "three" };
for(int i=0; i<str.length; i++)
{
System.out.println("str[" + i + "]: " +str[i]);
}
}
}
Output:
str[0]: one
str[1]: two
str[2]: three
Command-Line Arguments

class CommandLine
{
public static void main(String args[])
{
for(int i=0; i<args.length; i++)
{
System.out.println("args[" + i + "]: " +args[i]);
}
}
}

Output:

java CommandLine this is a test 100 -1


When you do, you will see the following output:
args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: -1
Inheritance:
Allows the creation of hierarchical classifications
A class that is inherited is called a superclass
The class that does the inheriting is called a subclass
(i.e) subclass is a specialized version of a superclass
class A
{
int i, j; // public by default
void showij()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k: " + k);
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance
{
public static void main(String args[])
{
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();

subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
Example: Inheritance with Constructors

class Box
{
double width,height,depth;
Box(Box ob)
{
width = ob.width; height = ob.height; depth = ob.depth;
}
Box(double w, double h, double d)
{
width = w; height = h; depth = d;
}
Box()
{
width = -1; height = -1; depth = -1;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
Example: contd

class BoxWeight extends Box


{
double weight;

BoxWeight(double w, double h, double d, double m)


{
width = w; height = h; depth = d; weight = m;
}
}
class DemoBoxWeight
{
public static void main(String args[])
{
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}
Example: A Superclass Variable Can Reference a Subclass Object

class RefDemo
{
public static void main(String args[])
{
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();//super class

double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " +weightbox.weight);
System.out.println();

// assign BoxWeight reference to Box reference


plainbox = weightbox;
vol = plainbox.volume();
System.out.println("Volume of plainbox is " + vol);
}
}
Using super
Whenever a subclass needs to refer to its immediate superclass, it
can do so by use
of the keyword super.

super has two general forms


The first calls the superclass constructor.
The second is used to access a member of the superclass
that has been hidden by a member of a subclass

Using super to Call Superclass Constructors


A subclass can call a constructor method defined by its superclass
by use of the following form of super:
super(parameter-list);
parameter-list specifies any parameters needed by the constructor in the
super class

Using super to access Superclass members


super.member
member can be either a method or an instance variable
Example : 1
class A
{
A() { }
A(String str)
{
System.out.println(str);
}
}
class B extends A
{
B(String str)
{
super(str);
System.out.println(str);
}
}
public class Mains
{
public static void main(String[] args)
{
B objb= new B("Welcome from class B");
}
}
Example : 2
class A
{
int i;
}
class B extends A
{
int i;
B(int a, int b)
{
super.i = a; // i in A
i = b; // i in B
}
void show()
{
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper
{
public static void main(String args[])
{
B subOb = new B(1, 2);
subOb.show();
}
}
Creating a Multilevel Hierarchy

one-to-one ladder increases


Multiple classes are involved in inheritance, but one class extends only one
The lowermost subclass can make use of all its super classes' members
Example:

class A
{
public void Addition(int a, int b)
{
System.out.println(a+b);
}
}
class B extends A
{
public void Subtraction(int a,int b)
{
System.out.println(a-b);
}
}
class C extends B
{
public void Multiplication(int a,int b)
{
System.out.println(a*b);
}
}
public class demo extends C
{
public static void main(String args[])
{
int x=10,y=5;
C p1 = new C();
p1.Addition(x, y);
p1.Subtraction(x,y);
p1.Multiplication(x,y);
}
}

Output:
15
5
50
Method Overriding:

A method in a subclass has the same name and type


signature as a method in its superclass, then the method
in the subclass is said to override the method in the
superclass
Example: Method overriding
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);
}
}
Example: Method overriding

class Override
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Example: Method overriding Solution thru super keyword
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()
{
super.show(); System.out.println("k: " + k);
}
}
Example: Method overriding Solution thru super keyword

class Override
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in A and in B
}
}
Abstract Classes:

A superclass that declares the structure of a given


abstraction without
providing a complete implementation of every method.

To declare an abstract method, use this general form:

abstract type name(parameter-list);


Note: no method body is present.
Example: Abstract Class and Method
abstract class A
{
abstract void callme();
void callmetoo()
{
System.out.println("This is a concrete method.");
}
}
class B extends A
{
void callme()
{
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
b.callme();
b.callmetoo();
}
}
Using final with Inheritance:

The keyword final has three uses.

First, it can be used to create the equivalent of a


named constant.

The other two uses of final apply to inheritance.


Using final with Inheritance: contd..

Using final to Prevent Overriding

To disallow a method from being overridden, specify final


as a modifier at the start of its declaration.
Methods declared as final cannot be overridden.

Example:
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Using final with Inheritance: contd..

Using final to Prevent Inheritance

Example:

final class A
{
// ...
}

class B extends A { // ERROR! Can't subclass A


// ...
}
Packages and Interfaces
Packages are containers for classes that are used to keep the class name
space compartmentalized

Defining a Package
To create a package is quite easy:

Simply include a package command as the first statement in a Java source


file.

Any classes declared within that file will belong to the specified package.

The package statement defines a name space in which classes are


stored.

This is the general form of the package statement:


package pkg;

Example: package MyPack;


Packages and Interfaces
Package Example

package student;
import java.io.*;
public class mark
{
int rno,m1,m2,total;
String stu_name;
public mark(String a1,int r,int m,int mm)
{
stu_name=a1;
rno=r;
m1=m;
m2=mm;
}
public void calctotal()
{
total=m1+m2;
System.out.println("NAME=+ stu_name);
System.out.println("TOTAL:"+total);
}
}
Packages and Interfaces
import student.mark;
class hybrid
{
public static void main(String a[])
{
System.out.print(PACKAGE-MARK LIST");

mark m=new mark("aaa",10,80,90);

m.calctotal();
}
}
Packages and Interfaces

Importing Packages

This is the general form of the import statement:

import pkg1[.pkg2].(classname|*);

Example:

import java.util.Date;

import java.io.*;
Packages and Interfaces

Interfaces
Interfaces are designed to support dynamic method resolution at run time.

Defining an Interface
An interface is defined much like a class. This is the general form of an interface:

access interface name


{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Interface example:
interface A
{
void meth1();
void meth2();
}
class MyClass implements A
{
public void meth1()
{
System.out.println("Implement meth1().");
}
}
class IFExtend
{
public static void main(String arg[])
{
MyClass ob = new MyClass();
ob.meth1();
}
}
Interface example:
interface A
{
void meth1();
void meth2();
}
interface B extends A
{
void meth3();
}
class MyClass implements A
{
public void meth1()
{
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
Interface example:
class IFExtend
{
public static void main(String arg[])
{
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

Das könnte Ihnen auch gefallen