=============================================
Lecture note,
Monday 19 September 2016 07:00:48 AM IST
====================================================
OVERLOADING OF METHODS
We can add another method with same name but different parameter list
Advantage is using the meaningful name as method name.
appropriate method is called with matching arguments.
Program - find area of circle and rectangle using method overloading
-------------------------------------------------------
public class Overload {
/**
* @param
*/
public static void main(String[] args) {
// TODO overload area
System.out.println("Area of Circle is " +area(10.5));
System.out.println("Area of rectangle is "+area(10,15.5));
}
static double area(double r){
return r*r*Math.PI;
}
static double area(double a, double b){
return (a*b);
}
}
-----------------------------------------------------------------
Area of Circle is 346.3605900582747
Area of rectangle is 155.0
----------------------------------------------------------
CONSTRUCTORS
USED to INITIALIZE objects
SAME METHOD NAME AS CLASS NAME.
no RETURN TYPE
Invoked automatically.
Look at Circle c = new Circle();
Here c object is created and a constructor is called. This is default constructor. This is provided automatically.
Program
----------------------------------------
public class ConstructorDemo {
double radius;
ConstructorDemo(){
radius = 1.0;
}
ConstructorDemo(double r){
radius = r;
}
double area(){
return radius*radius*Math.PI;
}
public static void main(String[] args) {
// TODO simple constructor
ConstructorDemo c = new ConstructorDemo();
System.out.println( c.area());
ConstructorDemo c2 = new ConstructorDemo(10.5);
System.out.println( c2.area());
}
}
3.141592653589793
346.3605900582747
---------------------------------------------------------------------
---------------------------------------------------------------------
It is MANDATORY to use a default constructor when ANY OTHER TYPE OF CONSTRUCTOR used.
===========================================================
Wednesday 21 September 2016 07:02:31 AM IST
//////////////////Lecture Note
===========================================================
Practice Problems
1. Create a class called Triangle with
* Three sides as double types
* A no-arg constructor with default side = 1
* A constructor with specified values of sides
* Method getArea() returns area.
* Method getPerimeter() returns perimeter.
Write Java Program to implement this class.
2. Create a class called FixedDeposit with
* DATA fields amount , interest, years.
* a no-arg cosntructor having default values 1000, 5.0, 1 respectvelly.
* a constructor having specified values.
* method totalReturn() returns the total return value.
Implement and Test this class .
------------------------------------------------------------
this REFERENCE
___________________
THIS KEYWORD IS used to refer the object that refers to it. For ex we can refer the instance varable radius as this.radius . This is required if the method refers with same name as parameter list.
For Ex - void getRadius(double radius)
{
this.radius = radius; //where radius is a data field as well as a parameter.
}
==================================================
COMPARE TWO OBJECTS
----------------------
How to compare radius of 2 circle objects
public class CompareObj {
/**
* @param args Author S K Nayak
*/
double radius;
CompareObj(){
}
CompareObj(double radius){
this.radius = radius;
}
static void greater( CompareObj c1, CompareObj c2){
if ( c1.radius > c2.radius)
System.out.println("greater is "+c1.radius);
else
System.out.println("greater is "+c2.radius);
}
public static void main(String[] args) {
// TODO Compare 2 objects
CompareObj obj1 = new CompareObj(15.5);
CompareObj obj2 = new CompareObj(20.5);
greater(obj1, obj2);
}
}greater is 20.5
------------------------------------------------------------------------
------------------------------------------------------------------------
INTERNAL QUESTIONS SET A
------------------------------------------------------------------------
1. Create a class called FacultyGuide with data fields -
private String facultyName
private String[] students - max 30 no of students
private int noOfStudents
use constructor to initialize facultyName and noOfStudents. use method addStudent to add a student name to facultyName. Test this class to add some students and display the names.
2. Describe relationship between the object and class. Describe object reference variable and object with example.
SET - B
1. Create a class called Election with data fields -
private String partyName
private String[] candidates - max 10 no of candidates
private int noOfCandidates.
use constructor to initialize partyName and noOfCanndidates. use method addCandidates to add a candidate name to partyName. Test this class to add some candidates and display names.
2. Explain four different visibility modes in java with examples.
---------------------------------------------------------------
=================================================================
Wednesday 19 October 2016 08:14:28 AM IST
Lecture note
=================================================================
INHERITANCE
New classes from existng classes.
Powerful feature of java for reusing the code.
In fact every class is inherited from Object class.
super class/ sub class,, base/ derived,, parent class/child class
private members are not inherited.
super class contains common set of attributes and methods that are applied to all sub classes. For ex a vehicle class contains regdno and ownerName which is applied to all vehicles.
keyword ---- extends
class Box {
double length;
double breadth;
double height;
Box()
{
}
Box (double l, double b, double h){
length = l;
breadth = b;
height = h;
}
double volume()
{
return length * breadth * height;
}
}
class Boxw extends Box{
double weight;
Boxw()
{
}
Boxw(double l, double b, double h, double w)
{
length = l;
breadth = b;
height = h;
weight = w;
}
}
public class BoxDemo {
/**
* @param args
*/
public static void main(String[] args) {
// box volume demo
Boxw bw = new Boxw(10.1,15.2, 20.2,5.2);
System.out.println("volume of box is "+bw.volume());
}
}
-----------------------------------------------------
volume of box is 3101.1039999999994
------------------------------------------------------
Tuesday 25 October 2016 07:26:50 AM IST
========================================================
using super to call super class constructors
super(arg list);
super --- first statement . what happens when Boxw(){ weight = 1;} used .
So constructors are executed on the order of derivation from super class to sub class,, called CONSTRUCTOR chaining.
SUPER can be used to refer super class member
OVERRIDING
A method in a sub class is said to override the superclass method having defined with same name ,same signature and same return type.
class A{
void show();
}
class B extends A{
void show();
}
super class object reference can refer sub class object and can achieve polymorphism.
Ex A r;
A a;
r=a
r=b
Protected data members accessed from subclass of same or different package.
Default is accessed from same package only.
============================================
Wednesday 26 October 2016 08:27:04 AM IST
============================================
WHY OVERRIDDEN METHODS ARE USED
To achieve RUN-TIME POLYMORPHISM or DYNAMIC BINDING.It is a powerful principle having ability of reusing the code during run time without recompiling the class. Hence to achieve flexibility.
--------------------------------------------
class Figure{
double x;
double y;
Figure(){
}
Figure(double d1,double d2){
x = d1;
y = d2;
}
double area(){
System.out.println("Area not defined");
return 0;
}
}
class Rectangle extends Figure{
Rectangle(){
}
Rectangle(double a, double b){
super(a,b);
}
double area(){
return ( x * y);
}
}
class Triangle extends Figure{
Triangle(){
}
Triangle(double a,double b){
super(a,b);
}
double area(){
return(0.5 * x * y);
}
}
public class Overriding {
/**
* Author SKNayak
*/
public static void main(String[] args) {
// TODO overriding example
Figure f;
Rectangle r = new Rectangle(10,20);
f = r;
System.out.println("Rectangle Area = "+f.area());
Triangle t = new Triangle(15,12);
f= t;
System.out.println("Triangle Area = "+f.area());
}
}
-----------------------------------
Rectangle Area = 200.0
Triangle Area = 90.0
----------------------------------
Tuesday 01 November 2016 07:13:51 AM IST
==================================================
Lecture Note
==================================================
PREVENTING EXTENDING BY using final class ,, ex Math class is a final class ,,can not be extended.
PREVENTING OVERRIDING - by using final method.
ABATRACT CLASSES
------------------
from sub class to super class -- classes are more general and less specific and
from super class to sub class -- classes are more specific
Sometimes super class is so general that and abstract that can not have specific instance. These classes are called
abstract classes.
Abstract classes can not be instantiated. For Ex - GeometricObject having area method.
Abstract class contains abstract methods.
Abstract method is a method signature without IMPLEMENTATION.
Program
--------
abstract class GeometricObject4 {
GeometricObject4(){
}
public abstract double findArea();
}
class Circle4 extends GeometricObject4{
double radius;
Circle4(){
}
Circle4(double r){
radius = r;
}
public double findArea(){
return radius*radius*Math.PI;
}
}
class Rectangle4 extends GeometricObject4{
double a;
double b;
Rectangle4(){
}
Rectangle4(double a, double b){
this.a = a;
this.b = b;
}
public double findArea(){
return a*b;
}
}
class TestGeometric {
/**
* Test compare area geometric objects
dynamically
*/
public static void main(String[] args) {
// TODO Abstract methods
GeometricObject4 ob1;
GeometricObject4 ob2;
ob1 = new Circle4(10);
ob2 = new Circle4(10);
GeometricObject4 ob3;
ob3 = new Rectangle4(10,20);
System.out.println("ob1 == ob2 "+compareArea(ob1,ob2));
System.out.println("ob1 == ob3 "+compareArea(ob1,ob3));
}
static boolean compareArea(GeometricObject4 obj1, GeometricObject4 obj2){
return (obj1.findArea() == obj2.findArea());
}
}
------------------
ob1 == ob2 true
ob1 == ob3 false
Abstract class contains abstract methods
Abstract methods can not be defined in non abstract class
Abstract class can contain concrete methods.
Subclass must implement abstract methods of its superclass or it must be declared as abstract.
Abstract class can not be instantiated using new operator.
Abstract class can have constructors.
A class that contain abstract methods must be declared abstract.
It is possible that a abstract class contains no abstract methods. In this case can not be instantiated.
Abstract class can have object reference.
A subclass can be abstract even if super class is concrete.
Lecture note,
Monday 19 September 2016 07:00:48 AM IST
====================================================
OVERLOADING OF METHODS
We can add another method with same name but different parameter list
Advantage is using the meaningful name as method name.
appropriate method is called with matching arguments.
Program - find area of circle and rectangle using method overloading
-------------------------------------------------------
public class Overload {
/**
* @param
*/
public static void main(String[] args) {
// TODO overload area
System.out.println("Area of Circle is " +area(10.5));
System.out.println("Area of rectangle is "+area(10,15.5));
}
static double area(double r){
return r*r*Math.PI;
}
static double area(double a, double b){
return (a*b);
}
}
-----------------------------------------------------------------
Area of Circle is 346.3605900582747
Area of rectangle is 155.0
----------------------------------------------------------
CONSTRUCTORS
USED to INITIALIZE objects
SAME METHOD NAME AS CLASS NAME.
no RETURN TYPE
Invoked automatically.
Look at Circle c = new Circle();
Here c object is created and a constructor is called. This is default constructor. This is provided automatically.
Program
----------------------------------------
public class ConstructorDemo {
double radius;
ConstructorDemo(){
radius = 1.0;
}
ConstructorDemo(double r){
radius = r;
}
double area(){
return radius*radius*Math.PI;
}
public static void main(String[] args) {
// TODO simple constructor
ConstructorDemo c = new ConstructorDemo();
System.out.println( c.area());
ConstructorDemo c2 = new ConstructorDemo(10.5);
System.out.println( c2.area());
}
}
3.141592653589793
346.3605900582747
---------------------------------------------------------------------
---------------------------------------------------------------------
It is MANDATORY to use a default constructor when ANY OTHER TYPE OF CONSTRUCTOR used.
===========================================================
Wednesday 21 September 2016 07:02:31 AM IST
//////////////////Lecture Note
===========================================================
Practice Problems
1. Create a class called Triangle with
* Three sides as double types
* A no-arg constructor with default side = 1
* A constructor with specified values of sides
* Method getArea() returns area.
* Method getPerimeter() returns perimeter.
Write Java Program to implement this class.
2. Create a class called FixedDeposit with
* DATA fields amount , interest, years.
* a no-arg cosntructor having default values 1000, 5.0, 1 respectvelly.
* a constructor having specified values.
* method totalReturn() returns the total return value.
Implement and Test this class .
------------------------------------------------------------
this REFERENCE
___________________
THIS KEYWORD IS used to refer the object that refers to it. For ex we can refer the instance varable radius as this.radius . This is required if the method refers with same name as parameter list.
For Ex - void getRadius(double radius)
{
this.radius = radius; //where radius is a data field as well as a parameter.
}
==================================================
COMPARE TWO OBJECTS
----------------------
How to compare radius of 2 circle objects
public class CompareObj {
/**
* @param args Author S K Nayak
*/
double radius;
CompareObj(){
}
CompareObj(double radius){
this.radius = radius;
}
static void greater( CompareObj c1, CompareObj c2){
if ( c1.radius > c2.radius)
System.out.println("greater is "+c1.radius);
else
System.out.println("greater is "+c2.radius);
}
public static void main(String[] args) {
// TODO Compare 2 objects
CompareObj obj1 = new CompareObj(15.5);
CompareObj obj2 = new CompareObj(20.5);
greater(obj1, obj2);
}
}greater is 20.5
------------------------------------------------------------------------
------------------------------------------------------------------------
INTERNAL QUESTIONS SET A
------------------------------------------------------------------------
1. Create a class called FacultyGuide with data fields -
private String facultyName
private String[] students - max 30 no of students
private int noOfStudents
use constructor to initialize facultyName and noOfStudents. use method addStudent to add a student name to facultyName. Test this class to add some students and display the names.
2. Describe relationship between the object and class. Describe object reference variable and object with example.
SET - B
1. Create a class called Election with data fields -
private String partyName
private String[] candidates - max 10 no of candidates
private int noOfCandidates.
use constructor to initialize partyName and noOfCanndidates. use method addCandidates to add a candidate name to partyName. Test this class to add some candidates and display names.
2. Explain four different visibility modes in java with examples.
---------------------------------------------------------------
=================================================================
Wednesday 19 October 2016 08:14:28 AM IST
Lecture note
=================================================================
INHERITANCE
New classes from existng classes.
Powerful feature of java for reusing the code.
In fact every class is inherited from Object class.
super class/ sub class,, base/ derived,, parent class/child class
private members are not inherited.
super class contains common set of attributes and methods that are applied to all sub classes. For ex a vehicle class contains regdno and ownerName which is applied to all vehicles.
keyword ---- extends
class Box {
double length;
double breadth;
double height;
Box()
{
}
Box (double l, double b, double h){
length = l;
breadth = b;
height = h;
}
double volume()
{
return length * breadth * height;
}
}
class Boxw extends Box{
double weight;
Boxw()
{
}
Boxw(double l, double b, double h, double w)
{
length = l;
breadth = b;
height = h;
weight = w;
}
}
public class BoxDemo {
/**
* @param args
*/
public static void main(String[] args) {
// box volume demo
Boxw bw = new Boxw(10.1,15.2, 20.2,5.2);
System.out.println("volume of box is "+bw.volume());
}
}
-----------------------------------------------------
volume of box is 3101.1039999999994
------------------------------------------------------
Tuesday 25 October 2016 07:26:50 AM IST
========================================================
using super to call super class constructors
super(arg list);
super --- first statement . what happens when Boxw(){ weight = 1;} used .
So constructors are executed on the order of derivation from super class to sub class,, called CONSTRUCTOR chaining.
SUPER can be used to refer super class member
OVERRIDING
A method in a sub class is said to override the superclass method having defined with same name ,same signature and same return type.
class A{
void show();
}
class B extends A{
void show();
}
super class object reference can refer sub class object and can achieve polymorphism.
Ex A r;
A a;
r=a
r=b
Protected data members accessed from subclass of same or different package.
Default is accessed from same package only.
============================================
Wednesday 26 October 2016 08:27:04 AM IST
============================================
WHY OVERRIDDEN METHODS ARE USED
To achieve RUN-TIME POLYMORPHISM or DYNAMIC BINDING.It is a powerful principle having ability of reusing the code during run time without recompiling the class. Hence to achieve flexibility.
--------------------------------------------
class Figure{
double x;
double y;
Figure(){
}
Figure(double d1,double d2){
x = d1;
y = d2;
}
double area(){
System.out.println("Area not defined");
return 0;
}
}
class Rectangle extends Figure{
Rectangle(){
}
Rectangle(double a, double b){
super(a,b);
}
double area(){
return ( x * y);
}
}
class Triangle extends Figure{
Triangle(){
}
Triangle(double a,double b){
super(a,b);
}
double area(){
return(0.5 * x * y);
}
}
public class Overriding {
/**
* Author SKNayak
*/
public static void main(String[] args) {
// TODO overriding example
Figure f;
Rectangle r = new Rectangle(10,20);
f = r;
System.out.println("Rectangle Area = "+f.area());
Triangle t = new Triangle(15,12);
f= t;
System.out.println("Triangle Area = "+f.area());
}
}
-----------------------------------
Rectangle Area = 200.0
Triangle Area = 90.0
----------------------------------
Tuesday 01 November 2016 07:13:51 AM IST
==================================================
Lecture Note
==================================================
PREVENTING EXTENDING BY using final class ,, ex Math class is a final class ,,can not be extended.
PREVENTING OVERRIDING - by using final method.
ABATRACT CLASSES
------------------
from sub class to super class -- classes are more general and less specific and
from super class to sub class -- classes are more specific
Sometimes super class is so general that and abstract that can not have specific instance. These classes are called
abstract classes.
Abstract classes can not be instantiated. For Ex - GeometricObject having area method.
Abstract class contains abstract methods.
Abstract method is a method signature without IMPLEMENTATION.
Program
--------
abstract class GeometricObject4 {
GeometricObject4(){
}
public abstract double findArea();
}
class Circle4 extends GeometricObject4{
double radius;
Circle4(){
}
Circle4(double r){
radius = r;
}
public double findArea(){
return radius*radius*Math.PI;
}
}
class Rectangle4 extends GeometricObject4{
double a;
double b;
Rectangle4(){
}
Rectangle4(double a, double b){
this.a = a;
this.b = b;
}
public double findArea(){
return a*b;
}
}
class TestGeometric {
/**
* Test compare area geometric objects
dynamically
*/
public static void main(String[] args) {
// TODO Abstract methods
GeometricObject4 ob1;
GeometricObject4 ob2;
ob1 = new Circle4(10);
ob2 = new Circle4(10);
GeometricObject4 ob3;
ob3 = new Rectangle4(10,20);
System.out.println("ob1 == ob2 "+compareArea(ob1,ob2));
System.out.println("ob1 == ob3 "+compareArea(ob1,ob3));
}
static boolean compareArea(GeometricObject4 obj1, GeometricObject4 obj2){
return (obj1.findArea() == obj2.findArea());
}
}
------------------
ob1 == ob2 true
ob1 == ob3 false
Abstract class contains abstract methods
Abstract methods can not be defined in non abstract class
Abstract class can contain concrete methods.
Subclass must implement abstract methods of its superclass or it must be declared as abstract.
Abstract class can not be instantiated using new operator.
Abstract class can have constructors.
A class that contain abstract methods must be declared abstract.
It is possible that a abstract class contains no abstract methods. In this case can not be instantiated.
Abstract class can have object reference.
A subclass can be abstract even if super class is concrete.