Object Oriented Programming Lab
Object Oriented Programming Lab
Object Oriented Programming Lab
Programming Lab
CSE 1206
Course Teacher
Obj1 Obj2
i=1 i=1
i=5
Obj3
i=1
Obj1
Output:
1
Obj2 i=5
i=1
5
5
Obj3
Methods
public void printName(String name){
System.out.println(name);
}
► A static method can access static data member and can change the value of it.
► A static method can be called without the need for creating an instance of a class.
Project Name: TestEmployee (main class)
Class Name: Employee
Variables in Employee:
static String organization;
private int eid;
private String name;
private String designation;
private int age;
Usually static methods are used to change static
variables
Calling methods in Main method
Giving an error.
You will need to create object
Or you can make the method static
Or call it directly without the Class name
Similar case for variables
Or declare the variable static
Introduction to
Inheritance
Inheritance in Java is a mechanism in
which one object acquires all the
properties and behaviors of a parent
object.
Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Single Level Inheritance
Super
Class
Sub
Class
Create a project with whatever name you
want to. It will be the main class. Then
create the following inheritance.
Super Class Name: Shape
Variables: color (String), filled (Boolean)
Two constructors:
- a no-argument constructor that initializes color =“white” and filled = true -
parameterized constructor.
Sub
Class
Testing the Inheritance in Main Class
Hierarchical Inheritance
Super Class : Shape